我创建了一个框架,想在上面加一个JPanel p1,p1上加了两个JButton,把p1加到JFrame上后,怎么不显示p1呢??
解决方案 »
- 问题
- 明文是8个字节的,密文却变成16字节的?
- socket和objectoutputstream的问题,debugger的话出问题,正常情况下没问题?
- 高分求一题解 急
- Timer和TimerTask类
- 我想问一下怎么样在Swing中潜入线程?
- 关于swing + spring 的可行性
- 简单问题1:接口可不可以实现接口?
- [求教]关于java的线程问题,谢谢帮忙 问题如下: ...........
- 怎么实现一个WEB页面的多个提交,即有多个提交按纽,提交到不同的动作页面。
- 如何在客户端访问远程文件系统(浏览,选择文件等)?
- Hibernate怎么处理事务呢?????????我看说什么用jta可我看了半天没看明白什
public class K
{
public JFrame frame = new JFrame("seal");
public JPanel p1;
public JPanel content;
public JPanel p2;
public JButton one;
public JButton two;
public ButtonGroup bg;
public K()
{
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
init();
}catch(Exception e)
{}
}
public void init()
{
// content = (JPanel) getContentPane();
frame.setSize(600,400);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height)
{
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width)
{
frameSize.width = screenSize.width;
}
frame.setLocation( (screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
frame.setVisible(true);
frame.setResizable(false);
one = new JButton("A");
two = new JButton("B");
p1 = new JPanel();
p1.setLayout(new FlowLayout());
p1.add(one);
p1.add(two);// p1.setSize(100,100);
frame.add(p1);
// content.add(p1);
}
public static void main(String[] args)
{
new K();
}
frame.getContentPane().add(p1);
frame.validate();
// content.add(p1);改成这样就可以了
public class K
{
public JFrame frame = new JFrame("seal");
public JPanel p1;
public JPanel content;
public JPanel p2;
public JButton one;
public JButton two;
public ButtonGroup bg;
public K()
{
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
init();
}catch(Exception e)
{}
}
public void init()
{
// content = (JPanel) getContentPane();
frame.setSize(600,400);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height)
{
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width)
{
frameSize.width = screenSize.width;
}
frame.setLocation( (screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
one = new JButton("A");
two = new JButton("B");
p1 = new JPanel();
p1.setLayout(new FlowLayout());
p1.add(one);
p1.add(two);// p1.setSize(100,100);
//改变一下语句顺序就可以了
content.add(p1);
frame.setVisible(true);
frame.setResizable(false);
//
}
public static void main(String[] args)
{
new K();
}
}
使用 validate 方法会使容器再次布置其子组件。已经布置容器后,在修改此容器的子组件的时候(在容器中添加或移除组件,或者更改与布局相关的信息),应该调用上述方法。