2015-03-30 48 views
1

在我的軟件的一部分中,我有一個底部佈局,其中包含JButton s和JLabel的幾個。我想讓按鈕位於面板的右側,並在左側標註。我可以設法將按鈕放在右側,但不知道如何將JLabel保留在左側。如何在FlowLayout的左邊和右邊有一個組件

下面是代碼:

bottomPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); 

ftpBack = new JButton("Back"); 
ftpNext = new JButton("Next"); 
label = new JLabel("Text); 

bottomPanel.add(label); 
bottomPanel.add(ftpBack); 
bottomPanel.add(ftpNext); 

mainPanel.add(bottomPanel, BorderLayout.SOUTH); 

這就是我想實現: enter image description here

不知道如何做呢?

回答

4

你不能用FlowLayout來做到這一點。

您可以使用水平BoxLayout

Box box = Box.createHorizontalBox(); 
box.add(label); 
box.add(Box.createHorizontalGlue()); 
box.add(backButton); 
box.add(Box.createHorizontalStrut(5)); 
box.add(nextButton); 

閱讀從How to Use BoxLayout Swing的教程部分獲取更多信息和示例。

或者另一種方法是窩佈局管理器:

JPanel main = new JPanel(new BorderLayout()); 
main.add(label, BorderLayout.WEST); 
JPanel buttonPanel= new JPanel(); 
buttonPanel.add(back); 
buttonPanel.add(next); 
main.add(buttonPanel, BorderLayout.EAST); 
相關問題