我用swing 写的 application 在运行后显示不出界面,需要点击一下或移动边框才显示界面... 我贴了个示例程序望高手帮我看看
这下面是一个示例,我刚学swing,求帮忙!Container我也试用过,还是不行...
这应该是个低级问题,可我实在解决不了了...
import javax.swing.JButton;
import javax.swing.JFrame;
public class helpme {
public static void main(String[] args) {
JFrame f = new JFrame();
f.setTitle("哪位大虾帮帮忙!");
f.setSize(300, 200);
f.setLocation(512, 100);
f.setVisible(true);
JButton b=new JButton("我运行后不出现,需要点一下边框才出现...");
f.add(b);
}
}
这下面是一个示例,我刚学swing,求帮忙!Container我也试用过,还是不行...
这应该是个低级问题,可我实在解决不了了...
import javax.swing.JButton;
import javax.swing.JFrame;
public class helpme {
public static void main(String[] args) {
JFrame f = new JFrame();
f.setTitle("哪位大虾帮帮忙!");
f.setSize(300, 200);
f.setLocation(512, 100);
f.setVisible(true);
JButton b=new JButton("我运行后不出现,需要点一下边框才出现...");
f.add(b);
}
}
import javax.swing.JFrame;
public class helpme {
public static void main(String[] args) {
JFrame f = new JFrame();
f.setTitle("哪位大虾帮帮忙!");
f.setSize(300, 200);
f.setLocation(512, 100);
JButton b=new JButton("我运行后不出现,需要点一下边框才出现...");
f.add(b);
f.setVisible(true);
}
}此方法就可以解决你的问题
不过我个人觉的,还是有问题,我也是初学者啊,
向JFame中添加组件应该用getConTentPane()方法或是建立一个中间容器
import javax.swing.JButton;
import javax.swing.JFrame;
public class helpme {
public static void main(String[] args) {
JFrame f = new JFrame();
f.setTitle("哪位大虾帮帮忙!");
f.setSize(300, 200);
f.setLocation(512, 100);
f.setVisible(true);
JButton b=new JButton("我运行后不出现,需要点一下边框才出现...");
f.getContentPane().add(b);
}
}
你在试试看行不行啊
但是用swing写,f.add(b);也可以,不知道为什么啊,请高手帮助啊
frame.setPreferredSize(new Dimension(500,500));
frame.getContentPane().add(new JButton("test"));
frame.pack();
frame.setVisible(true);
frame.setVisible(true);这句之后,最好不要再对frame有任何操作,
f.add(b);这种写法貌似也不是很好,最好通过frame.getContentPane()操作组件