在此JFrame
中GridBagLayout
有4列,棕色线应该是第1列和第2列之间的限制,而OK和取消按钮应位于此限制的每一侧:
问题:
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();
}
}
仅限测试:如果我将>
和<
按钮分别添加到第1列和第2列中的JFrame
,并且不跨越多列,则第1列和第2列被迫具有相同的宽度,并且设置的底部按钮现在居中。
代码已更改:
addToFrame(addBtn, 1, 1, 1, 1, GridBagConstraints.CENTER);
addToFrame(rmvBtn, 2, 3, 1, 1, GridBagConstraints.CENTER);
结果:
两个JTextArea
仍有不同的宽度:-(显然>
和<
不会对齐!
如何解决此问题以使按钮居中,并且两个JTextArea
具有相同的宽度?谢谢你的帮助。
(此代码的灵感来自此tutorial)
答案 0 :(得分:0)
这是因为您没有在每列上添加任何组件,这在某种程度上需要GridBagLayout
按预期工作。
现在说几句话:
您是否注意到在链接到的教程中,确定和取消按钮与JTextArea
对齐,而不是整体上部组件?
使用GridBagLayout
时,最好为您要添加的每个组件设置新的GridBagConstraint
。它可以防止您忘记在某处重置属性,这很难排除故障。
然后,如果您希望 OK 和 Cancel 按钮与上部组件居中对齐,最简单的方法是创建两个JPanel
:一个带有上部组件,另一个带有按钮。您仍然可以使用GridBagLayout
作为上部面板,使用默认值(FlowLayout
)作为按钮。您可以将它们置于BorderLayout
位置CENTER
和SOUTH
,然后您就可以开始了。请注意,面板将是中心对齐的,而不是 OK / 取消按钮与&lt; / &gt; 按钮。
回到你的问题的解决方案:你必须添加&#34;空&#34;组件(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
...
这样细胞就会存在,你就会得到预期的结果。