2011-01-30 64 views
2

內部垂直的JButton我有4個小組,北,南,東,西邊界佈局。例如在東邊,我有一個jpanel,它有四個按鈕。我的問題是,所有按鈕都對齊到頂部,我想對齊中心。在CSS例如邊緣頂部或頂部的例子:50%。中心的JPanel -java

任何想法?

JPanel contentor3 = new JPanel(); 
    contentor.add(contentor3, BorderLayout.EAST); 
    contentor3.setBackground(Color.green); 
    contentor3.setPreferredSize(new Dimension(120, 750)); 

    contentor3.add(btn[1]); 
    btn[1].setText("btn1"); 

回答

8

您需要更改contentor3的佈局。嘗試是這樣的:類似

contentor3.setLayout (new GridBagLayout()); 

GridBagConstraints gbc = new GridBagConstraints(); 

// next two lines will place the components top-to-bottom, rather than left-to-right 
gbc.gridx = 0; 
gbc.gridy = GridBagConstraints.RELATIVE; 

// add as many buttons as you want in a column 
contentor3.add (new JButton ("btn1"), gbc); 

contentor.add (contentor3, BorderLayout.EAST); 
+0

我已經嘗試過的東西,但隨後的按鈕不破線,所以他們創造了一個符合所有按鈕和部件都隱藏 – anvd 2011-01-30 18:24:09