2013-05-28 56 views
2

我在調整我的組合框時遇到了問題,所以它更接近'band目錄'標籤。如何將組合框移動到左側,除標籤外僅爲5px。我曾嘗試爲我的標籤設置水平插入,爲我的組合框設置負插入,但仍然無法使用。Java gridbaglayout問題

這裏是我的代碼:

public void createGUI() 
{ 
    main_panel = new JPanel(); 
    main_panel.setLayout(new GridBagLayout()); 
    GridBagConstraints gc = new GridBagConstraints(); 

    label = new JLabel("Band Directory:"); 
    band_combobox = new JComboBox(); 
    members_panel = new JPanel(); 
    members_panel.setBorder(title); 
    members_list = new JLabel(); 
     members_panel.add(members_list); 

    gc.fill = GridBagConstraints.HORIZONTAL; 
    gc.gridx = 0; 
    gc.gridy = 0; 
    gc.insets = new Insets(0, 0, 10, 0); 
     main_panel.add(label, gc); 

    gc.fill = GridBagConstraints.HORIZONTAL; 
    gc.gridx = 1; 
    gc.gridy = 0; 
    gc.insets = new Insets(0, 0, 10, 0); 
     main_panel.add(band_combobox, gc); 

    gc.fill = GridBagConstraints.HORIZONTAL; 
    gc.gridx = 0; 
    gc.gridy = 1; 
    gc.insets = new Insets(0, 0, 10, 0); 
     main_panel.add(members_panel, gc); 

//more code 
} 

enter image description here

+0

就個人而言,我會避免GrindBagLayout,而使用多個嵌套實例的其他佈局經理。 –

回答

5

嘗試調整members_panel的溢出...

gc.fill = GridBagConstraints.HORIZONTAL; 
gc.gridx = 0; 
gc.gridy = 1; 
gc.insets = new Insets(0, 0, 10, 0); 
gc.gridwidth = 2; // Allows the members_panel to use 2 columns within the grid 
main_panel.add(members_panel, gc); 
+0

這個工程!非常感謝:) – user1352609

+0

這使得一個很好的變化:P – MadProgrammer