使用setlayout(null)后,button不能够显示出来呢?
importjava.awt.*;importjavax.swing.*;publicclassUserframe{publicstaticvoidmain(String...
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 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);
}
} 展开
1个回答
展开全部
使用绝对坐标定位时,必须得给组件的大小以及位置进行设置。
更改后如下
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);
}
}
更改后如下
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);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询