【发布时间】:2015-02-03 21:09:26
【问题描述】:
我正在尝试使用一些 JLabels 图标(框图标)和退出按钮创建一个界面。问题是,当我将按钮放在标签下时,它们会根据按钮的位置分开,如下所示:
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.VERTICAL; // Adding space before the labels
c.gridy = 0;
c.weighty = 1;
add(new JLabel(" "), c);
for(int i = 0; i < label.length; i++){ // Adding the JLabels
c.fill = GridBagConstraints.NONE;
c.weighty = 0;
c.gridy = 1;
c.gridx++;
add(label[i], c);
}
// Adding the button
c.gridy ++;
c.gridx = 2; // Changing the value of gridx, will move the button to that location and it will split another jlabel.
c.weighty = 0.09;
add(eButton, c);
revalidate();
repaint();
【问题讨论】:
-
为什么不将
GridBagLayout嵌套在一个面板中,该面板位于borderLayout的中心,然后使用flowLayout在另一个面板中添加退出按钮,然后可以放置borderLayout以南 -
将按钮放在 gridx 0 处,将网格宽度设置为 REMAINDER 并将锚点设置为 CENTRE
-
同意@MadProgrammer 的评论,如果您想在任何内容之前/之后/上方/下方添加空格,只需修改插图即可。
-
谢谢你们。 @MadProgrammer 为什么我不能在 jlabels 上使用锚 NORTH 或 CENTER ?
标签: java swing layout-manager gridbaglayout