java如何使用GridBagLayout维护jpanel的维度大小?
这是我的密码。当我在其他布局中调整程序的大小时,除了CheckPanel
(使用GridBagLayout
)之外,它工作正常;不知怎的,它变得更大了。我该怎么办
提前谢谢
JPanel CheckPanel = new JPanel();
CheckPanel.setBackground(Color.WHITE);
GridBagConstraints gbc_CheckPanel = new GridBagConstraints();
gbc_CheckPanel.gridwidth = 2;
gbc_CheckPanel.fill = GridBagConstraints.BOTH;
gbc_CheckPanel.gridx = 0;
gbc_CheckPanel.gridy = 1;
CenterPanel.add(CheckPanel, gbc_CheckPanel);
CheckPanel.setLayout(new GridLayout(0, 6, 0, 0));
# 1 楼答案
约束
告诉GridBagLayout允许该组件使用任何可用的额外空间(也取决于其首选大小和最大大小)。使用
GridBagConstraints.NONE
或GridBagConstraints.VERTICAL
可以解决这个问题,但您仍然需要决定哪个组件将接收额外的空间编辑:根据注释,仍然不清楚如何插入其他组件,以及如何指定额外空间的分布。在本例中,
weighty
字段用于表示表应该接收所有额外空间。也许这有助于识别与原始程序的差异: