62,623
社区成员
发帖
与我相关
我的任务
分享
public void paintComponent(Graphics g) {
// super.paintComponent(g);
// ...
}
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
public class TestDialog extends JDialog implements ActionListener {
JButton btnFillRect;
public TestDialog() {
this.setSize(640, 480);
btnFillRect = new JButton("Fill Rect");
btnFillRect.addActionListener(this);
this.add(btnFillRect, BorderLayout.SOUTH);
this.getGraphics();
}
public void actionPerformed(ActionEvent e) {
Graphics g = this.getGraphics();
g.setColor(Color.RED);
g.fillRect(50, 50, 200, 200);
}
public static void main(String[] args) {
TestDialog dialog = new TestDialog();
dialog.setVisible(true);
}
}