菜鸟提问,protected void paintComponent(Graphics g)问题
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class color1 extends JFrame
{
Color ccc;
JMenuBar jmb = new JMenuBar();
JMenu jm1 = new JMenu("File");
JMenu jm2 = new JMenu("View");
JMenuItem jmi1 = new JMenuItem("Exit");
JRadioButton jmi2 = new JRadioButton("red");
JRadioButton jmi3 = new JRadioButton("green");
JRadioButton jmi4 = new JRadioButton("blue");
ButtonGroup bg = new ButtonGroup();
public color1()
{
this.setJMenuBar(jmb);
jmb.add(jm1);jmb.add(jm2);
jm1.add(jmi1);
jm2.add(jmi2);jm2.add(jmi3);jm2.add(jmi4);
bg.add(jmi2);bg.add(jmi3);bg.add(jmi4);
mypanel myp = new mypanel();
this.getContentPane().add(myp);
this.setSize(400,450);
this.setVisible(true);
}
class mypanel extends JPanel
{
protected void paintComponent(Graphics g)
{
g.drawRect(100,100,200,200);
g.drawLine(100,200,300,200);
g.drawLine(200,100,200,300);
}
}
public static void main(String[] agrs)
{
color1 c = new color1();
c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
写了个头就开始迷茫了,
protected void paintComponent(Graphics g)不是构造函数,为什么程序一启动,它就会去画那个框??请高手帮忙解释一下,谢谢.