我实际上是在用 Java 学习图形。我无法理解这个JFrame对象如何调用extends的paintComponent方法。MyDrawpanelJPanel
在frame.repaint()再次调用paintComponent但如何?为什么我不能使用like 的drawPanel对象;MyDrawPaneldrawPanel.repaint()
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SimpleGui3C implements ActionListener
{
JFrame frame;
public static void main(String[] args)
{
SimpleGui3C gui = new SimpleGui3C();
gui.go();
}
public void go()
{
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button = new JButton("Change Colors");
button.addActionListener(this);
MyDrawPanel drawPanel = new MyDrawPanel();
frame.getContentPane().add(BorderLayout.SOUTH, button);
frame.getContentPane().add(BorderLayout.CENTER, drawPanel);
frame.setSize(300,300);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent event)
{
frame.repaint();
}
}
class MyDrawPanel extends JPanel
{
public void paintComponent(Graphics g)
{
g.fillRect(0,0,this.getWidth(), this.getHeight());
int red = (int)(Math.random() * 255);
int green = (int)(Math.random() * 255);
int blue = (int)(Math.random() * 255);
Color randomColor = new Color(red,green,blue);
g.setColor(randomColor);
g.fillOval(70,70,100,100);
}
}
慕工程0101907
相关分类