SpringLayout可以在Windows 7上运行,但不能在NetBSD上运行? [英] SpringLayout works on Windows 7 but not NetBSD?
问题描述
我正在使用SpringLayout使用以下代码制作GUI:
I am making a GUI using SpringLayout using the following code:
private void createAndShowGUI() {
frame = new JFrame("A GUI");
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
frame.setResizable(false);
Container contentPane = frame.getContentPane();
SpringLayout layout = new SpringLayout();
contentPane.setLayout(layout);
contentPane.add(this);
layout.putConstraint(SpringLayout.WEST, this, 300, SpringLayout.WEST, contentPane);
layout.putConstraint(SpringLayout.NORTH, this, 0, SpringLayout.NORTH, contentPane);
JLabel startLabel = new JLabel("Start Node:");
contentPane.add(startLabel);
layout.putConstraint(SpringLayout.WEST, startLabel, 5, SpringLayout.WEST, contentPane);
layout.putConstraint(SpringLayout.NORTH, startLabel, 5, SpringLayout.NORTH, contentPane);
startNodes = new JComboBox();
contentPane.add(startNodes);
layout.putConstraint(SpringLayout.WEST, startNodes, 15, SpringLayout.WEST, contentPane); // THIS LINE
layout.putConstraint(SpringLayout.EAST, startNodes, -10, SpringLayout.WEST, this); // AND THIS ONE
layout.putConstraint(SpringLayout.NORTH, startNodes, 5, SpringLayout.SOUTH, startLabel);
layout.putConstraint(SpringLayout.EAST, contentPane, 0, SpringLayout.EAST, this);
layout.putConstraint(SpringLayout.SOUTH, contentPane, 0, SpringLayout.SOUTH, this);
frame.pack();
frame.setVisible(true);
}`
当我在NetBSD上运行它时,两条注释行似乎相互冲突. 当我只有第一个时,它会按预期的方式向左对齐,但是当我添加第二个时,它会向右对齐,而不是像我期望的那样向右拉伸.
When I run it (on NetBSD), the two commented lines seem to fight with each other. When I just have the first one, it aligns to the left as expected, but when I add the second one, it aligns to the right, as opposed to stretching to the right as I would expect.
奇怪的是,它可以在Windows 7 Pro 32bit上正常运行.
The weird thing is that it runs just fine on Windows 7 Pro 32bit.
有人可以告诉我我做错了什么吗?
Can anyone tell me what I am doing wrong?
推荐答案
事实证明,在Java 1.5中,始终必须在WEST/NORTH约束之前指定EAST/SOUTH约束.否则,将无法正确放置组件. 该错误已在1.6中修复,这就是为什么我在Windows计算机上没有注意到它的原因.
It turns out that in Java 1.5, you always have to specify the EAST/SOUTH constraints before the WEST/NORTH constraints. Otherwise it doesn't position the components properly. This bug is fixed in 1.6, which is why I didn't notice it on my Windows machine.
这篇关于SpringLayout可以在Windows 7上运行,但不能在NetBSD上运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!