swing中frame里设置setLayout(null),setSize(x,y)组件仍不显示 本帖最后由 itnumone 于 2011-09-20 19:08:01 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 setLayout()就是布局管理器,可以设置容器的布局方式。 具体的布局方式很多,有FlowLayout、BorderLayout、GridLayout、BoxLayout、GridBagLayout、 SpringLayout,另外还有无布局和自定义布局,可以看相关的书。设置为null,button不知道显示在哪,设置为null则你自己定位,大小坐标等即可显示 例如: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); } } 我设置了setBounds(),仍然不显示 给button 设置一下大小btn.setBounds(?,?,?,?); 给Button和Panel都设置了大小,仍然不显示 btn.setLocation(2, 3);btn.setSize(50, 50); public TestgetSourceMethod() { Button btn=new Button("ok"); btn.setBackground(new Color(0,0,255)); btn.setLocation(2, 3); btn.setSize(50, 50); add(btn); setLayout(null); // setBounds(20,20,45,60); pack(); this.setVisible(true); } 这样写还不成 楼主你想问什么呢?就是想问非要setLayout(null)的时候还要让Button显示出来么? 在不使用布局情况下把这个 pack(); 去掉 这是一个在setLayout(null)情况下可以跑的例子setBound()方法跟pack()方法是矛盾的,所以只能保留一个import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.event.*;public class TestsetLayout extends JFrame { /** * Leaning * TestsetLayout .java * TODO * 2011-9-20 */ private static final long serialVersionUID = 1L; /** * */ public TestsetLayout () { Button btn=new Button("ok"); btn.setBounds(0,0,200,200); btn.setBackground(new Color(0,0,255)); add(btn); setLayout(null); setBounds(20,20,300,300); this.setSize(300,300);// pack(); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } /** * @param args */ public static void main(String[] args) { TestsetLayout t=new TestsetLayout (); }} 你是指button不能正常显示还是jframe啊 // this.setBounds(20,20,30,30); // pack(); setSize(100,200);jframe就设置下大小至于pack() 为什么不管用 我就不清楚了 [Quote=引用 14 楼 ioe_gaoyong 的回复:]你跑了我在10楼给的代码没,这个是可以跑的哦你的能跑通的,呵呵,谢谢啦,下面是我经过你的指导总结的: FlowLayout BorderLayout和pack()一块用,pack()用于显示Frame里的容器和组件 setLayout()不能和pack()方法一起用,并且设置容器和各个组件的大小,并且都要设置大小,缺一不 可 新手求助!! 基本概念问题:接口中的抽象方法为什么不加abstract? hibernate 映射的问题。 脑袋都大了。 itext 怎么访问一个PDF文件啊 关于this关键字的理解 有没有java去混淆的的工具,介绍一下,谢谢。不是加淆。 求购:短信平台!价格可商量! ========求个正则======== 請問如何運行bat文件??? 怎样把回车写入文件中? java 怎样处理 int 类型溢出? 新手关于输入日期,函数Date的问题
具体的布局方式很多,有FlowLayout、BorderLayout、GridLayout、BoxLayout、GridBagLayout、 SpringLayout,另外还有无布局和自定义布局,可以看相关的书。设置为null,button不知道显示在哪,设置为null则你自己定位,大小坐标等即可显示
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);
}
}
btn.setBounds(?,?,?,?);
btn.setSize(50, 50);
Button btn=new Button("ok");
btn.setBackground(new Color(0,0,255));
btn.setLocation(2, 3);
btn.setSize(50, 50);
add(btn);
setLayout(null);
// setBounds(20,20,45,60);
pack();
this.setVisible(true);
}
这样写还不成
就是想问非要setLayout(null)的时候还要让Button显示出来么?
setBound()方法跟pack()方法是矛盾的,所以只能保留一个import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;public class TestsetLayout extends JFrame { /**
* Leaning
* TestsetLayout .java
* TODO
* 2011-9-20
*/
private static final long serialVersionUID = 1L; /**
*
*/
public TestsetLayout () {
Button btn=new Button("ok");
btn.setBounds(0,0,200,200);
btn.setBackground(new Color(0,0,255)); add(btn);
setLayout(null);
setBounds(20,20,300,300);
this.setSize(300,300);
// pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
} /**
* @param args
*/
public static void main(String[] args) {
TestsetLayout t=new TestsetLayout ();
}
}
// this.setBounds(20,20,30,30);
// pack();
setSize(100,200);
jframe就设置下大小
至于pack() 为什么不管用 我就不清楚了
你跑了我在10楼给的代码没,这个是可以跑的哦
你的能跑通的,呵呵,谢谢啦,下面是我经过你的指导总结的:
FlowLayout BorderLayout和pack()一块用,pack()用于显示Frame里的容器和组件
setLayout()不能和pack()方法一起用,并且设置容器和各个组件的大小,并且都要设置大小,缺一不 可