在此JFrame
与 GridBagLayout
对于 4 列,棕色线应该是第 1 列和第 2 列之间的限制,“确定”和“取消”按钮应该位于该限制的每一侧:
问题:
- “确定”+“取消”设置未与其他按钮居中。
- 左右
JTextArea
宽度不同。
当我期望第 1 列和第 2 列相等时,第 1 列的宽度似乎为零。
使用的代码:
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
public class GblSO extends JFrame {
// Instance variables
GridBagConstraints gbc = new GridBagConstraints();
public GblSO() {
// Set frame
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new GridBagLayout());
// Text areas
JTextArea left = new JTextArea("Left!");
JTextArea right = new JTextArea("Right!");
setConstraints(1, 1, GridBagConstraints.BOTH, null);
addToFrame(left, 0, 1, 1, 5, GridBagConstraints.CENTER);
addToFrame(right, 3, 1, 1, 5, GridBagConstraints.CENTER);
// Transfer buttons
JButton addBtn = new JButton(">");
JButton rmvBtn = new JButton("<");
setConstraints(0, 0, GridBagConstraints.NONE, new Insets(3, 5, 3, 5));
addToFrame(addBtn, 1, 1, 2, 1, GridBagConstraints.CENTER);
addToFrame(rmvBtn, 1, 3, 2, 1, GridBagConstraints.CENTER);
// OK / Cancel buttons
JButton okBtn = new JButton("OK");
JButton canBtn = new JButton("Cancel");
setConstraints(0, 0, GridBagConstraints.NONE, new Insets(15, 4, 15, 4));
addToFrame(okBtn, 0, 6, 2, 1, GridBagConstraints.EAST);
addToFrame(canBtn, 2, 6, 2, 1, GridBagConstraints.WEST);
// Show
pack();
setVisible(true);
}
private void setConstraints(double weightx, double weighty, int fill, Insets insets) {
gbc.weightx = weightx; // how much cell resizes
gbc.weighty = weighty; // "
gbc.fill = fill; // how component fills cell
gbc.insets = (insets == null ? new Insets(0, 0, 0, 0) : insets);
}
private void addToFrame(Component comp,
int gridx, int gridy, int gridwidth, int gridheight, int anchor) {
gbc.gridx = gridx;
gbc.gridy = gridy;
gbc.gridwidth = gridwidth;
gbc.gridheight = gridheight;
gbc.anchor = anchor;
add(comp, gbc);
}
public static void main(String[] args) {
new GblSO();
}
}
仅用于测试:如果我添加 >
和<
按钮到JFrame
分别在第 1 列和第 2 列中,并且不跨越多个列,第 1 列和第 2 列被迫具有相同的宽度,并且底部按钮集现在居中。
代码已更改:
addToFrame(addBtn, 1, 1, 1, 1, GridBagConstraints.CENTER);
addToFrame(rmvBtn, 2, 3, 1, 1, GridBagConstraints.CENTER);
结果:
两个JTextArea
仍然有不同的宽度:-(,显然 >
和 <
不会对齐!
如何解决这个问题,使按钮居中,两个JTextArea
有相同的宽度吗?预先感谢您的帮助。
(此代码的灵感来自于 tutorial )
最佳答案
这是因为您没有在每一列上添加任何组件,而 GridBagLayout
在某种程度上需要它才能按预期工作。
现在几点说明:
您是否注意到在链接到的教程中,确定和取消按钮与JTextArea
对齐,而不是与整个上部组件对齐?
使用 GridBagLayout
时,最好为要添加的每个组件实例化一个新的 GridBagConstraint
。它可以防止您忘记在某处重置属性,而这很难排除故障。
然后,如果您希望确定和取消按钮与上部组件居中对齐,最简单的方法是创建两个JPanel
:一个带有上部组件,另一个带有按钮。您仍然可以对上部面板使用 GridBagLayout
,对按钮使用默认布局 (FlowLayout
)。您可以将它们放入 BorderLayout
位置 CENTER
和 SOUTH
即可。请注意,面板将居中对齐,而不是确定/取消按钮和<之间的空间/> 按钮。
回到问题的解决方案:您必须添加“空”组件(Container
、JPanel
、emtpy JLabel
、.. .) 在第一行(或最后一行)将 weightx
设置为 1.0
,因此单元格实际上是在 X 方向填充,并将 weighty
设置为0.0
因此它们在 Y 方向上不可见(如果您想使用 gridheight
而不是 gridwidth
,反之亦然)。
...
setLayout(new GridBagLayout());
setConstraints(1, 0, GridBagConstraints.BOTH, null);
addToFrame(new Container(), 0, 0, 1, 1, GridBagConstraints.CENTER);
addToFrame(new Container(), 1, 0, 1, 1, GridBagConstraints.CENTER);
addToFrame(new Container(), 2, 0, 1, 1, GridBagConstraints.CENTER);
addToFrame(new Container(), 3, 0, 1, 1, GridBagConstraints.CENTER);
// Text areas
...
这样细胞就会存在,您就会得到预期的结果。
关于java - GridBagLayout:gridwidth > 1 时的对齐方式和宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25957796/