Java GridBagLayout - 组件脱离了一边

时间:2014-01-14 22:17:31

标签: java swing gridbaglayout windowbuilder

我正在创建一个IRC客户端,并且我正在使用GridBagLayout,以便在调整窗口大小时,显示消息的JTextArea也会调整大小。但每当我尝试改变数字时,组件总是在屏幕的一侧(图片:http://imgur.com/BoJ84pS)。这是代码:

setSize(750, 550);
setLocationRelativeTo(null);
contentPane = new JPanel();
contentPane.setBackground(Color.LIGHT_GRAY);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
GridBagLayout gbl_contentPane = new GridBagLayout();
gbl_contentPane.columnWidths = new int[]{15, 680, 40, 15};
gbl_contentPane.rowHeights = new int[]{15, 480, 25, 30};
gbl_contentPane.columnWeights = new double[]{0.0, 0.0, 0.0, Double.MIN_VALUE};
gbl_contentPane.rowWeights = new double[]{0.0, 0.0, 0.0, Double.MIN_VALUE};
contentPane.setLayout(gbl_contentPane);

图片有点不清楚,但JTextArea,JButton和JTextField都部分在窗外。顺便说一下,我正在使用windowbuilder插件来制作GUI。

1 个答案:

答案 0 :(得分:3)

不要设置窗口的大小。您不可能(或至少,很容易)在所有可能的平台上预测窗口的大小。

相反,将所有组件添加到窗口,然后调用pack,这会将窗口大小调整为其内容的首选大小