GUI 窗口中组件的对齐方式
我有一个看起来像 window1 的窗口,我希望它看起来像 window2:
这是我的代码:
String q = "Have you used GUI before?";
JLabel textLabel2 = new JLabel(
"<html><div style=\"text-align: center;\">" + q + "</html>", SwingConstants.CENTER);
add(textLabel2, BorderLayout.NORTH);
JPanel radioPanel = new JPanel();
add(radioPanel, BorderLayout.CENTER);
JPanel btnPanel = new JPanel();
add(btnPanel, BorderLayout.SOUTH);
对于单选按钮,我尝试使用 GridLayout,但它破坏了“是”和“否”的位置。显然,对于“后退”和“下一步”按钮,水平对齐不起作用(btnPanel.setAlignmentX(RIGHT_ALIGNMENT);
)。任何解决方案都将受到高度赞赏,我在这方面坚持得太久了。谢谢
--编辑--
这工作得很好:
btnPanel.setLayout(new BoxLayout(btnPanel, BoxLayout.LINE_AXIS));
btnPanel.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));
btnPanel.add(Box.createHorizontalGlue());
所以按钮问题就解决了。
但是,仍然无法修复单选按钮。
--编辑2--
使用 setOpaque(false);
修复了单选按钮的背景
I have got a window that looks like window1 and I would like it to look like window2:
This is my code:
String q = "Have you used GUI before?";
JLabel textLabel2 = new JLabel(
"<html><div style=\"text-align: center;\">" + q + "</html>", SwingConstants.CENTER);
add(textLabel2, BorderLayout.NORTH);
JPanel radioPanel = new JPanel();
add(radioPanel, BorderLayout.CENTER);
JPanel btnPanel = new JPanel();
add(btnPanel, BorderLayout.SOUTH);
For the radio-buttons, I tried to use GridLayout, but it broke the position of "Yes" and "No". And for the "back" and "next" buttons, horizontal alignment did not work (btnPanel.setAlignmentX(RIGHT_ALIGNMENT);
), apparently. Any solutions will be highly appreciated, I'm stuck with this bit way too long. Thanks
--EDIT--
That is working perfectly fine:
btnPanel.setLayout(new BoxLayout(btnPanel, BoxLayout.LINE_AXIS));
btnPanel.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));
btnPanel.add(Box.createHorizontalGlue());
so the buttons problem is solved.
However, still can't get the radio-buttons fixed.
--EDIT 2--
Fixed the background for the radio-buttons using setOpaque(false);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它“破坏”了“是”和“否”的位置是什么意思,因为 GridLayout 应该可以正常工作。我将通过
new GridLayout(0, 1)
为其指定 1 列和 2 行(或 0 行,表示可变数量)。通过执行radioPanel.setOpaque(false);
确保其 opaque 属性设置为 false。这样它将显示它所在容器的背景颜色。您可能还需要使 JRadioButtons 不透明,我不确定。您的 btnPanel 可以使用 BoxLayout 并使用 Box.createGlue() 将按钮推到右侧。
最重要的是——如果您还没有这样做,请阅读有关使用 Swing 布局管理器的教程,您可以在其中找到 此处。
What do you mean by it "broke" the position of "yes" and "no" as a GridLayout should work just fine. I'd give it 1 column and 2 (or 0 for variable number of) rows via
new GridLayout(0, 1)
. Be sure that its opaque property is set as false by doingradioPanel.setOpaque(false);
. This way it will show the background color of the container that it sits in. You may need to make the JRadioButtons non-opaque as well, I'm not sure.Your btnPanel could use a BoxLayout and use Box.createGlue() to push the buttons over to the right side.
Most importantly -- if you haven't yet done so, please read the tutorials on use of the Swing layout managers which you can find here.
您可以为此做一些事情。您需要更改布局管理器。对于 BorderLayout 来说,这不是一项艰巨的任务。你可以做嵌套的 BoxLayouts。一个垂直框,具有垂直固定高度支柱、标签、垂直固定高度支柱、是单选、垂直固定支柱、无单选、垂直胶水和最终按钮面板。然后使用按钮面板中的编辑来水平对齐它们。这是一种选择,但面板的嵌套很烦人。
另一种选择是获取 TableLayout 并学习如何使用它。 TableLayout 是最好的布局管理器之一。它易于使用,经过严格测试,并且它使 Swing 再次变得有趣。您将永远不会再使用 GridBagLayout。
http://java.sun.com/products/jfc/tsc/articles/ tablelayout/
最后一个选项是使用新的 GroupLayout。我不太熟悉它,但看起来很简单。而且,它不像 Box 那样需要太多代码或嵌套不必要的面板。
A couple of things you can do about this. You need to change your LayoutManager. This is not a great task for BorderLayout. You could do nested BoxLayouts. A vertical box that has the vertical fixed height strut, label, vertical fixed height strut, yes radio, vertical fixed strut, no radio, Vertical glue, and the final button panel. Then use your edit in the button panel to horizontally align them. That's one option, but the nesting of the panels is annoying.
Another option go get TableLayout and learn how to use it. TableLayout is one of the best LayoutManagers. It's easy to use, solidly tested, and it makes Swing fun again. You'll never use GridBagLayout ever ever ever again.
http://java.sun.com/products/jfc/tsc/articles/tablelayout/
The final option is use the new GroupLayout. I'm not terribly familiar with it, but it looks pretty easy. And, it doesn't take as much code or nesting unnecessary panels like Box does.