2011-01-30 62 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