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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 04:03:33  来源:igfitidea点击:

Put JButton in bottom right

javaswingjbutton

提问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 BoxLayoutand size/alignment hintsto achieve this.

您可以使用BoxLayout大小/对齐提示的组合来实现这一点。