2012-01-15 67 views
1

組件我試圖讓GridBagLayout的兩個板爲40%和框架的60%,同時能夠讓他們的內部組件,它是被麻煩。的Java的GridBagLayout裏面

當我不要放置在面板裏面的按鈕,它的工作原理就像我想它。

不太清楚我在做什麼錯了,我已經試過移動按鈕,創建於GridBagLayout的面板,其中的創作,但它仍然沒有奏效。

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 

public class Test{ 

public void display(){ 
    JFrame frame = new JFrame(); 

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setSize(900,650); 
    frame.setVisible(true); 

    JPanel test = new JPanel(); 
    test.setLayout(new GridBagLayout()); 
    GridBagConstraints c= new GridBagConstraints(); 

    JPanel left= new JPanel(); 
    JPanel right= new JPanel(); 

    c.fill = GridBagConstraints.VERTICAL - GridBagConstraints.HORIZONTAL; 
    c.weightx = 0.4; 
    c.gridx = 1; 
    c.weighty = 1; 
    test.add(left,c); 
    c.weightx = .6; 
    c.gridx = 2; 
    test.add(right,c); 

    JButton button= new JButton("A button"); 
    left.add(button,c);//If I do not add this, then it shows how I want it to be 

    frame.add(test); 
    } 
} 
+0

什麼是'createVector'? – 2012-01-15 01:34:48

+0

不好意思,把它改名爲button,來自別的東西。 – user1062898 2012-01-15 01:37:05

+0

當我運行它時,看起來像兩個40/60%分割的面板,有或沒有按鈕。這到底是什麼問題? – Ash 2012-01-15 02:08:40

回答

1

與權重的東西是它們描述哪些如何處理額外的空間。組件具有佈局管理器在計算佈局時使用的首選,最小和最大尺寸。 GridBagLayout然後使用這些權重分割額外的空間。在你的情況下,我認爲被分割的空間等於900-button.getPreferredSize()。width。要拆分也許800個像素到320和480

0

這裏是GridBagLayout的創建面板的一個例子:(不要打擾左右搖擺的工廠,只需要創建一個組件,而不是)

private void buildSourcePanel() { 

    JPanel pnlSource = new JPanel(); 

    GridBagLayout gbl_pnlSource = new GridBagLayout(); 
    gbl_pnlSource.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0}; 
    gbl_pnlSource.columnWidths = new int[]{0, 0, 100, 100, 25}; 
    pnlSource.setLayout(gbl_pnlSource); 

    final JLabel lblFolderMask = swingFactory.createLabel(" SOURCE DIRECTORY ", null, null, SwingConstants.LEFT, SwingConstants.CENTER, true); 
    pnlSource.add(lblFolderMask, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, 10, 1, new Insets(5, 5, 5, 0), 0, 0)); 

    txtSource = swingFactory.createTextField(null, "txtSource", null, SystemColor.textHighlight, SwingConstants.LEFT, false, true, "Source Directory"); 
    pnlSource.add(txtSource, new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0, 10, 1, new Insets(5, 0, 5, 5), 0, 0)); 

    final JButton btnBrowse = new JButton("Browse..."); 
    btnBrowse.setBorder(new CompoundBorder(new LineBorder(new Color(0, 0, 0), 0, false), new EmptyBorder(5, 5, 5, 5))); 
    btnBrowse.setFont(new Font("Verdana", Font.BOLD, 14)); 
    pnlSource.add(btnBrowse, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0, 10, 1, new Insets(5, 0, 5, 5), 0, 0)); 

    final JButton btnClear = new JButton("Clear..."); 
    btnClear.setBorder(new CompoundBorder(new LineBorder(new Color(0, 0, 0), 0, false), new EmptyBorder(5, 5, 5, 5))); 
    btnClear.setFont(new Font("Verdana", Font.BOLD, 14)); 
    pnlSource.add(btnClear, new GridBagConstraints(3, 0, 1, 1, 0.0, 0.0, 10, 1, new Insets(5, 0, 5, 5), 0, 0)); 

    lblStatus = swingFactory.createLabel(null, null, null, SwingConstants.CENTER, SwingConstants.CENTER, false); 
    pnlSource.add(lblStatus, new GridBagConstraints(4, 0, 1, 1, 0.0, 0.0, 10, 1, new Insets(5, 5, 5, 5), 0, 0)); 
}