使用setlayout(null)后,button不能够显示出来呢?

来源:百度知道 编辑:UC知道 时间:2024/05/17 19:24:52
import java.awt.*;
import javax.swing.*;
public class Userframe {

public static void main(String[] args)
{
Userframe01 uf=new Userframe01();
uf.setVisible(true);

}

}

class Userframe01 extends JFrame
{
private static int width=600;
private static int height=450;
private Button b1;
private Panel p;

public Userframe01()
{
super();
setLayout(null);//问题在这里,如果使用FlowLayout等布局,button就可以显示出来,为什么呢?谢谢了!
setSize(width,height);
this.setTitle("SYSTEMLOAD");
this.setResizable(false);
this.setVisible(true);

Container container = getContentPane();

p=new Panel();
Button b1=new Button("ok");

p.add(b1);
container.add(p);
}
}

使用绝对坐标定位时,必须得给组件的大小以及位置进行设置。
更改后如下
import java.awt.*;
import javax.swing.*;
public class Userframe {

public static void main(String[] args)
{
Userframe01 uf=new Userframe01();
uf.setVisible(true);

}

}

class Userframe01 extends JFrame
{
private static int width=600;
private static int height=450;
private Button b1;
private Panel p;

public Userframe01()
{
super();
setLayout(null);//问题在这里,如果使用FlowLayout等布局,button就可以显示出来,为什么呢?谢谢了!
setSize(width,height);
this.setTitle("SYSTEMLOAD");
this.setResizable(false);
this.setVisible(true);

Container container = getContentPane();

p=new Panel();
p.setBounds(0,0,200,200);//更改大小
p.setLocation(0, 0);//更改坐标
Button b1=new Button("ok");

p.add(b1);
container.add(p);
}
}