为什么 GridBagLayout 将我的组件居中而不是放在角落?
到目前为止,我设法尽可能避免使用 GridBagLayout
(手动代码),但这次我无法避免它,我正在阅读 SUN 的教程 GridBagLayout 到目前为止进展并不顺利。我想我误解了一些东西。
例如,我尝试使用以下代码(类似于 SUN 帖子中的代码):
public class MainFrame extends JFrame {
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainFrame frame = new MainFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame
*/
public MainFrame() {
super();
setBounds(100, 100, 500, 375);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container mainContainer = getContentPane();
mainContainer.setLayout(new GridBagLayout());
//add label
JLabel someLabel = new JLabel("Label 1:");
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = 0;
constraints.gridy = 0;
//constraints.anchor = GridBagConstraints.FIRST_LINE_START;
//constraints.weightx = 0.5;
mainContainer.add(someLabel, constraints);
JTextField someText = new JTextField(30);
constraints = new GridBagConstraints();
constraints.gridx = 1;
constraints.gridy = 0;
constraints.weightx = 0.5;
mainContainer.add(someText, constraints);
//
}
}
我在框架的中心中获得了一个相邻的标签和文本字段。
但我期望它们会出现在左上角,因为标签的 gridx 和 gridy 为 0。
即使我设置 constraints.anchor = GridBagConstraints.FIRST_LINE_START;
仍然是相同的结果。
我这里错了吗?
来自太阳报的帖子:
指定组件左上角的行和列。这 最左边的列有地址 gridx=0,顶行有地址 网格=0。
So far I managed to avoid using the GridBagLayout
(by hand code) as much as possible, but I could not avoid it this time and I am reading the SUN's tutorial GridBagLayout
So far it is not going well. I think I am missunderstanding something.
For example I try the following code (similar to the one in SUN's post):
public class MainFrame extends JFrame {
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainFrame frame = new MainFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame
*/
public MainFrame() {
super();
setBounds(100, 100, 500, 375);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container mainContainer = getContentPane();
mainContainer.setLayout(new GridBagLayout());
//add label
JLabel someLabel = new JLabel("Label 1:");
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = 0;
constraints.gridy = 0;
//constraints.anchor = GridBagConstraints.FIRST_LINE_START;
//constraints.weightx = 0.5;
mainContainer.add(someLabel, constraints);
JTextField someText = new JTextField(30);
constraints = new GridBagConstraints();
constraints.gridx = 1;
constraints.gridy = 0;
constraints.weightx = 0.5;
mainContainer.add(someText, constraints);
//
}
}
I get the label and the textfield one next to the other in the center of the frame.
But I was expecting that they would show up in the top left corner since the gridx and gridy is 0 for the label.
Even if I set constraints.anchor = GridBagConstraints.FIRST_LINE_START;
still the same result.
Am I wrong here?
From the SUN's post:
Specify the row and column at the upper left of the component. The
leftmost column has address gridx=0 and the top row has address
gridy=0.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
将
constraints.weighty = 1;
添加到 JLabel 约束,并将constraints.anchor = GridBagConstraints.NORTHWEST;
添加到 TextField 约束。编辑:
来自 Oracle 的 GridBagLayout 指南:
Add
constraints.weighty = 1;
to the JLabel constraints andconstraints.anchor = GridBagConstraints.NORTHWEST;
to the TextField constraints.EDIT:
From Oracle's GridBagLayout guide:
您需要在 Swing 教程中进一步阅读有关
weightX/weightY
的部分,其中指出:除非您为weightx或weighty指定至少一个非零值,否则所有组件都会聚集在一起一起放在容器的中心。
您指定了weightX,但没有指定weightY。
编辑一下,比我想象的要复杂。看来您还需要指定:
除了权重之外,还需要为两个组件指定:
You need to read further down in the Swing tutorial for the section on
weightX/weightY
where it states:Unless you specify at least one non-zero value for weightx or weighty, all the components clump together in the center of their container.
You specified a weightX but not a weightY.
Edit, it's more complicated than I thought. It appears you also need to specify:
for both components in addiation to the weighty.
您可以通过使用一个技巧来实现这一点,在行后面添加一个虚拟组件并将其扩展以填充垂直空间。您也可以重复使用约束,无需创建新对象:
编辑:好吧,忘记技巧了:(正确的方法是 Deon Botha 和 BenCole 所说的,我已经使用锚点更新了我的代码,
请DON 'T 接受这个答案,接受 Deon 或 Ben 的答案
You can achieve this by using a trick, add a dummy component after your row and expand it to fill the vertical space. Also you can re-use the constraints, no need to create a new object:
EDIT: ok forget the trick :( The right way is as Deon Botha and BenCole said, I've updated my code using the anchor
Please DON'T accept this answer, accept either Deon's or Ben's
我可能不会直接回答你的问题,但相信我,你应该使用 IDE 在布局上进行尝试和错误。我个人建议 Netbeans。您可以在那里拖放,然后查看属性。首先,您会在属性检查器中给出一些默认值,因此自动生成的代码会较少。但是,一旦您开始使用布局,您就可以看到代码并很好地了解您要做什么。
I may not be answering your question directly, but trust me you should do your trial and erros on the layouts with an IDE. I personally suggests Netbeans. There you can drag and drop and then take a look over the properties. At first you would have some default values given in the property inspector and hence less auto generated code for that. But then, once you start playing with the layouts, you can see the code and get a nice understanding of knowing what you do how you do.
这对我有用:
This worked for me: