Java 网格布局对齐问题
我使用Java 网格布局对齐问题,java,swing,layout-manager,gridbaglayout,Java,Swing,Layout Manager,Gridbaglayout,我使用GridBagLayout创建面板 我想要面板右角的删除图标。但是,如果未将JTextArea添加到面板中,删除图标将向左移动 如何做好这件事 这是我的GridBagLayout面板设置代码 public void setting() { try { this.setLayout(new GridBagLayout()); c = new GridBagConstraints(); c.anchor = GridBagConstr
GridBagLayout
创建面板
我想要面板右角的删除图标。但是,如果未将JTextArea
添加到面板中,删除图标将向左移动
如何做好这件事
这是我的GridBagLayout面板设置代码
public void setting() {
try {
this.setLayout(new GridBagLayout());
c = new GridBagConstraints();
c.anchor = GridBagConstraints.WEST;
c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0; // **** comment this line out to see effect ****
c.weighty = 1.0; // **** comment this line out to see effect ****
this.setBorder(BorderFactory.createLineBorder(Color.black));
addMouseListener(this);
buildTopJPane();
setVisible(true);
} catch (Exception err) {
Utility.DisplayErrorMsg(pageErrorPrefix + err.getMessage().toString());
}
}
有将组件添加到面板中的代码
private void buildTopJPane() {
try {
c.gridx = 0;
c.gridy = 0;
c.fill = GridBagConstraints.NONE;
this.add(setAttonIconCreator(), c);
c.gridx = 5;
c.gridy = 0;
c.gridwidth = 4;
this.add(setDeleteIcon(), c);
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 4;
this.add(setPageDateLabel(), c);
if (thisComment.content != null) {
if (thisComment.content.length() > 0)
c.gridx = 0;
c.gridy = 2;
c.gridwidth = 4;
this.add(setContent(), c);
}
} catch (Exception err) {
Utility.DisplayErrorMsg(pageErrorPrefix + err.getMessage().toString());
}
}
在删除图标上结合使用
weightx
和anchor
例如,尝试使用weightx=1
和anchor=GridBagConstraints.WEST
。注意,这也会将其他内容推到左侧
您也可以尝试在注释面板上使用weightx=1
您还可以更好地使用gridwidth
,单元放置允许您跨列重叠组件,从而获得更好的结果
在下一个组件上使用约束之前,不要忘记重置约束。在删除图标上结合使用
weightx
和anchor
例如,尝试使用weightx=1
和anchor=GridBagConstraints.WEST
。注意,这也会将其他内容推到左侧
您也可以尝试在注释面板上使用weightx=1
您还可以更好地使用gridwidth
,单元放置允许您跨列重叠组件,从而获得更好的结果
在下一个组件上使用约束之前,不要忘记重置约束。谢谢您的建议。我在删除图标上设置了GridBagConstraints.NORTHEAST,并将其放置在右上角。谢谢您的建议。我在删除图标上设置了GridBagConstraints.NORTHEAST,并将其放置在右上角。