public class MyTest extends JFrame { public static void main(String[] args) {
MyTest test = new MyTest();
JPanel p = new JPanel();
p.setLayout(new SpringLayout()); //把这句注释掉,按钮就能正常显示
p.add(new JButton("你好"));
test.setContentPane(p);
test.pack();
test.setLocationRelativeTo(null);
test.setVisible(true);
}}上述代码采用了Springlayout布局,程序运行的结果是,窗口实际大小为0,按钮看不到,只有手动将窗口调大之后才能看到按钮。
如果注释掉springlayout,那么按钮就能正常显示这是springlayout的bug吗?

解决方案 »

  1.   

    Note: Unlike many layout managers, SpringLayout doesn't automatically set the location of the components it manages. If you hand-code a GUI that uses SpringLayout, remember to initialize component locations by constraining the west/east and north/south locations. 
    Depending on the constraints you use, you may also need to set the size of the container explicitly. 
      

  2.   

    jdk 自带的layoutmanager不是太简单,就是太难用。