Java gridbaglayout problems

别说谁变了你拦得住时间么 提交于 2019-12-24 03:07:51

问题


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
}


回答1:


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);


来源:https://stackoverflow.com/questions/16785901/java-gridbaglayout-problems

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!