import java.awt.*;public class MyFrame extends Frame{
public static void main(String args[]){
MyFrame fr=new MyFrame("练习7.1");
fr.setSize(640,480);
fr.setBackground(Color.red);
Panel p1;
p1=new Panel();
p1.setLayout(null);
p1.setSize(200,300);
p1.setBackground(Color.yellow);
fr.setLayout(null);
fr.add(p1);
Button b1,b2,b3;
b1=new Button("按扭1");
b2=new Button("按扭2");
b3=new Button("按扭3");
b1.setLocation(100,120);
b1.setSize(80,20);
p1.add(b1);
p1.add(b2);
p1.add(b3);
fr.setVisible(true);
};
public MyFrame(String str){
super(str);
}
}这样就可以了..你试一下就知道了.
public static void main(String args[]){
MyFrame fr=new MyFrame("练习7.1");
fr.setSize(640,480);
fr.setBackground(Color.red);
Panel p1;
p1=new Panel();
p1.setLayout(null);
p1.setSize(200,300);
p1.setBackground(Color.yellow);
fr.setLayout(null);
fr.add(p1);
Button b1,b2,b3;
b1=new Button("按扭1");
b2=new Button("按扭2");
b3=new Button("按扭3");
b1.setLocation(100,120);
b1.setSize(80,20);
p1.add(b1);
p1.add(b2);
p1.add(b3);
fr.setVisible(true);
};
public MyFrame(String str){
super(str);
}
}这样就可以了..你试一下就知道了.
这些零零碎碎的东西放在MyFrame的构造函数里多好啊!
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;public class MyFrame extends Frame{
public MyFrame(String str){
super(str);
setSize(640,480);
setBackground(Color.red);
Panel p1=new Panel();
p1.setLayout(null);
p1.setSize(200,300);
p1.setBackground(Color.yellow);
p1.setLayout(null);
Button b1,b2,b3;
b1=new Button("按扭1");
b2=new Button("按扭2");
b3=new Button("按扭3");
b1.setLocation(100,120);
b1.setSize(80,20);
b2.setLocation(0,0);
b2.setSize(80,20);
b3.setLocation(0,40);
b3.setSize(80,20);
p1.add(b1);
p1.add(b2);
p1.add(b3);
add(p1);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
);
setVisible(true);
} public static void main(String args[])
{
MyFrame fr=new MyFrame("练习7.1");
}
}
基础打好了才可以往深了学习。找本书看看写JFrame的程序都怎么写的才是正道。
好了,不说废话,解决问题:
因为设置了setLayout(null);是无布局管理,所以panel虽然add了三个JButton,但是并没有指定他们的位置,所以JVM是不知道要加在哪里的。必须用setLocation();或者setBounds()方法指定好他们的位置。