2012-06-21 83 views
2

我想要得到一個垂直,頂端對齊的佈局工作。這是我有:如何使BoxLayout垂直但兒童流頂端對齊?

JPanel pane = new JPanel(); 
pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS)); 

MyImagePanel panelImage = new MyImagePanel(); 
panelImage.setSize(400, 400); 

pane.add(new JComboBox()); 
pane.add(panelImage); 
pane.add(new JButton("1")); 
pane.add(new JButton("2")); 
pane.add(new JButton("3")); 

JFrame frame = new JFrame("Title"); 
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
frame.setSize(800, 600); 
frame.add(pane); 
frame.pack(); 
frame.setVisible(true); 

所有控件出現,但它看起來像在其頂部和底部之間的運行時間被應用於填充所以他們有些垂直居中。這是我要去的:

----------------------------------------------------- 
| ComboBox |          | 
------------          | 
|   |          | 
| Image |          | 
|   |          | 
------------          | 
| Button 1 | Any additional space fills the right | 
------------          | 
| Button 2 |          | 
------------          | 
| Button 3 |          | 
------------          | 
|             | 
| Any additional space fills the bottom   | 
|             | 
----------------------------------------------------- 

我該如何讓BoxLayout做到這一點?

謝謝

-------------------------更新--------------- ----------

能夠使用這樣的:

Box vertical = Box.createVerticalBox(); 
frame.add(vertical, BorderLayout.NORTH); 
vertical.add(new JComboBox()); 
vertical.add(new JButton("1")); 
... 

得到我想要的東西。

+0

你說的意思是「任何額外的空間填充底部」你的意思是組件應該擴大,以填補空白,或者什麼都不應該出現,並且所有組件都應該保持其preferredSize? –

+1

是啊組件會保持它們的首選大小 - 如果父窗口恰好高於所有組件的高度組合,那隻會填充空白空間。 – user291701

+1

不要用答案更新問題。發表答案作爲答案,並接受它。 ;-) – npe

回答

1

一個更合適的佈局管理是GridBagLayout的,但也有其他的選擇:

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 

import javax.swing.JButton; 
import javax.swing.JComboBox; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 

public class Test3 { 

    protected static void initUI() { 
     JPanel pane = new JPanel(new GridBagLayout()); 
     GridBagConstraints gbc = new GridBagConstraints(); 
     gbc.anchor = GridBagConstraints.NORTHWEST; 
     gbc.fill = GridBagConstraints.NONE; 
     gbc.weightx = 0.0; 
     gbc.weighty = 0.0; 
     gbc.gridwidth = GridBagConstraints.REMAINDER; 
     JPanel panelImage = new JPanel() { 
      @Override 
      protected void paintComponent(Graphics g) { 
       super.paintComponent(g); 
       g.setColor(Color.RED); 
       g.fillRect(0, 0, 400, 400); 
      } 
     }; 
     panelImage.setPreferredSize(new Dimension(400, 400)); 

     pane.add(new JComboBox(), gbc); 
     pane.add(panelImage, gbc); 
     pane.add(new JButton("1"), gbc); 
     pane.add(new JButton("2"), gbc); 
     // We give all the extra horizontal and vertical space to the last component 
     gbc.weightx = 1.0; 
     gbc.weighty = 1.0; 
     pane.add(new JButton("3"), gbc); 

     JFrame frame = new JFrame("Title"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.add(pane); 
     frame.pack(); 
     frame.setVisible(true); 

    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       initUI(); 
      } 
     }); 
    } 
}