Positioning items with GridBagConstraints
Can anybody tell me why the following code sticks all of my items in the center of the window?
Code:
public static void mainScreen() {
JFrame frame = new JFrame();
pane = frame.getContentPane();
JButton addBtn = new JButton("Add Kid");
JLabel cabinLabel = new JLabel("Select a cabin");
String[] cabins = {"Cabin 1", "Cabin 2", "Cabin 3", "Cabin 4",
"Cabin 5", "Cabin 6", "Cabin 7", "Cabin 8", "Cabin 9", "Cabin 10"};
JComboBox cabinList = new JComboBox(cabins);
frame.setTitle("WOW");
frame.setSize(700,400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 0;
c.gridy = 0;
pane.add(cabinLabel, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 0;
c.gridy = 1;
c.insets = new Insets(0,0,0,200);
pane.add(cabinList, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 0;
c.gridy = 2;
pane.add(list, c);
pane.add(addBtn);
frame.validate();
addBtn.setMnemonic(KeyEvent.VK_A);
addBtn.setActionCommand("Add new kid");
I want to push them all up closer to the top. How do I accomplish this?