JFrame
withを指定するために使用できる絶対主義の最小コードは何Graphics
ですか? paint()
、たとえば、メソッドを取り除くことはできますか? 、電話する必要がありますsuper.update(g)
か?
2 に答える
0
paint() メソッドは、JFrame でオーバーライドするために引き続き使用できます。より良い解決策は、JPanel を拡張し、" paintComponent(Graphics g) " メソッドをオーバーライドして、シェイプを描画するためのキャンバスとして機能する小さなクラスを追加することです。次に、そのパネルを別のコンポーネントとして JFrame に追加します。コンテンツを更新するには、repaint() メソッドを呼び出します。例えば:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class DoNotEnterSign extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
Point center = new Point(getWidth() / 2, getHeight() / 2);
int radius = Math.min(getWidth() / 2, getHeight() / 2) - 5;
int innerRadius = (int)(radius * 0.9);
int barWidth = (int)(innerRadius * 1.4);
int barHeight = (int)(innerRadius * 0.35);
g.fillRect(center.x - barWidth/2, center.y - barHeight/2,
barWidth, barHeight);
}
public static void main(String[] args) {
JFrame frame = new JFrame("A simple graphics program");
frame.setSize(400, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new DoNotEnterSign();
panel.setBackground(Color.GREEN.darker());
frame.getContentPane().add(panel, BorderLayout.CENTER);
frame.setVisible(true);
}}
これは Swing クラスの基本コンポーネントです。もっと簡単な解決策があるかもしれませんが。たとえば、テキストを入力できるテキストエリアと大文字に変換するボタンがある場合は、 update() する必要がないかもしれません。
public void actionPerformed(ActionEvent e) {
area.setText(area.getText().toUpperCase());
setTextarea() が見つかるとすぐに自分自身を更新します
于 2013-05-21T17:24:10.373 に答える