关于GUI中panel与Frame的一点小问题

来源:互联网 发布:美国超级计算机和知乎 编辑:程序博客网 时间:2024/05/28 23:22

初学者遇到的小问题,记录一下。


在学习GUI时,仿造示例代码编写代码,实现以下图形(即在Frame中嵌入一个panel):



初次编写代码如下:

//test frame & panelimport java.awt.*;public class Test{public static void main(String[] args){Myframe3 f = new Myframe3(100,100,400,400,Color.BLUE);}}class Myframe3 extends Frame{private Panel p;Myframe3(int x,int y, int w,int h,Color c){super("Gino :Frame with panel");//对super的调用必须是构造函数中的第一个语句p = new Panel(null);p.setBounds(w/4,h/4, w/2,h/2);//不能直接设置,应用参数设置p.setBackground(Color.YELLOW);add(p);//把panel加入到frame中setBounds(x,y,w,h);setBackground(c);setVisible(true);setLayout(null);}}

javac Test.java

java Test

结果如下图所示:



反复对比检查,终于发现

在frame的属性设置时,setLayout(null)务必出现在setVisible(true)之前否者就会显示出问题!



原创粉丝点击