java 将 JButton 放在右下角
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11165807/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Put JButton in bottom right
提问by Lucas
I need to put a button in the bottom right of an otherwise empty JPanel
我需要在一个空的 JPanel 的右下角放一个按钮
+-----------------------------------+
| |
| |
| |
| |
| |
| |
| |
| |
| +-----------+|
| | Click Me! ||
| +-----------+|
+-----------------------------------+
How do I do that? It should be easy right? I would like to find the correct layout manager rather than using a sequence of nested panels.
我怎么做?应该很容易吧?我想找到正确的布局管理器,而不是使用一系列嵌套面板。
JPanel panel = new JPanel();
panel.setLayout(new SomeKindOfLayoutManagerThatDoesThis());
panel.add(new JButton("Click Me!"), SETTINGS);
回答by henry bemis
I would suggest using the Border Layout manager with Flow Layout.
我建议使用带有 Flow Layout 的 Border Layout manager。
something like:
就像是:
this.setLayout(new BorderLayout());
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
JButton clickmeButton = new JButton("Click Me");
buttonPanel.add(clickmeButton);
this.add(buttonPanel,BorderLayout.SOUTH);
回答by Jeffrey
You can use a combination of BoxLayout
and size/alignment hintsto achieve this.