小妹初学java

来源:百度知道 编辑:UC知道 时间:2024/05/25 20:51:51
import javax.swing.*;
import java.awt.*;
public class hell
{
hell(){

JFrame frame=new JFrame("小软件");
Container cp=frame.getContentPane();
JPanel p2=new JPanel(new GridLayout(3,1,0,50));
JButton b1=new JButton("AAA");
JButton b2=new JButton("BBB");
JButton b3=new JButton("CCC");
jp p1=new jp();
p2.add(b1);p2.add(b2);p2.add(b3);
cp.add(p1);
frame.setVisible(true);

}

public static void main(String[] args)
{
new hell();
}
}
class jp extends JPanel
{
jp(){this.setVisible(true);
}
public void Paint(Graphics g)
{
g.drawString("EEE",50,50);this.setBackground(Color.BLUE) ;
}
}
上面是我编的一个界面,按道理应该是窗口右边是从上到下三个按钮。窗口左边是绿色的并且写有"EEE"在(50.50)的地方,可是我运行后窗口右边是有三个按钮没错,可是左边什么都没有,好像paint没运行一样。为什么呢

类JP里的方法不会被调用到。。
默认用的是class hell 里的paint
例如这样写:
class jp extends JPanel
{
JButton b;
jp(){ this.setVisible(true);
b=new JButton("hello");
this.add(b);}
}
就可以看到hello的BUTTON,想要在PANEL里显示文字的话就用JLabel吧,或者在class hell 里的paint方法里自己写

Paint你没有调用,怎么会自动运行了?
我记得好像应该用repaint