主题:在JPanel上画图显示不出来 求教!!!
代码如下(手动敲的 没有写头文件),在一个JFrame中添加一个JPanel,JPanel上添加自己的MyPanel,在MyPanel上画图,但是显示不出来。如果去掉中间的JPanel,直接在JFrame上添加MyPanel就能显示出。请高手指教这是怎么回事?如何在多个JPanel上画图?
public class TestDemo extends JFrame{
MyPanel mp;
JPanel jp1;
public TestDemo(){
mp = new MyPanel();
jp1 = new JPanel();
jp1.add(mp);
this.add(jp1);
this.setSize(200,200);
this.setVisible(true);
}
public static void main(String[] args){
new TestDemo();
}
}
class MyPanel extends JPanel{
public void paintComponent(Graphics g){
g.drawLine(20,20,50,50);
}
}
public class TestDemo extends JFrame{
MyPanel mp;
JPanel jp1;
public TestDemo(){
mp = new MyPanel();
jp1 = new JPanel();
jp1.add(mp);
this.add(jp1);
this.setSize(200,200);
this.setVisible(true);
}
public static void main(String[] args){
new TestDemo();
}
}
class MyPanel extends JPanel{
public void paintComponent(Graphics g){
g.drawLine(20,20,50,50);
}
}