2012-06-23 74 views
9

我需要把一个按钮,否则空的JPanel将JButton的在右下角

+-----------------------------------+ 
|         | 
|         | 
|         | 
|         | 
|         | 
|         | 
|         | 
|         | 
|      +-----------+| 
|      | Click Me! || 
|      +-----------+| 
+-----------------------------------+ 

如何做到这一点的右下角?它应该是很容易的权利?我想找到正确的布局管理器,而不是使用一系列嵌套面板。

JPanel panel = new JPanel(); 
panel.setLayout(new SomeKindOfLayoutManagerThatDoesThis()); 
panel.add(new JButton("Click Me!"), SETTINGS); 
+1

可能是有用的 - [如何投入使用GridBagLayout的右下角成分(http://stackoverflow.com/q/7905731/1048330) – tenorsax

+0

是它的唯一组件在容器中?对于不可调整大小的容器,可以使用按钮上的EmptyBorder来实现。 –

回答

17

我会建议在流布局中使用边界布局管理器。

类似:

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); 
+1

我在最后使用了这个,稍做修改:) – Lucas

+0

+1,请参阅这个[示例](http://stackoverflow.com/questions/10741506/jbutton-positioning-issues/10742228#10742228): - ) –

+1

为什么很难找到这个答案?我一直在使用谷歌搜索和半小时,这是我想要的。 – MikeTheLiar