Java gridbaglayout problems

2019-07-19 05:59发布

I'm having problems adjusting my combobox so it is closer to the 'band directory' label. How do i move the combobox to the left, just 5px besides the label. I have tried setting horizontal insets for my label and negative insets for my combobox but that still did not work.

Here is my code:

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

1条回答
看我几分像从前
2楼-- · 2019-07-19 06:12

Try adjusting the overflow of the 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);
查看更多
登录 后发表回答