Instead of positioning and resizing components explicitly a layout manager can be used to place objects (in the order they are added to the container. The following standard layout managers exist:
GridBagLayout gridbag = new GridBagLayout(); GridBagConstraints constraints = new GridBagConstraints(); constraints.fill = GridBagConstraints.BOTH; setLayout(gridbag); constraints.weightx = 1.0; constraints.weighty = 1.0; Button first = new Button("first"); gridbag.setConstraints(first, constraints); add(first); constraints.weightx = 2.0; // twice as wide constraints.weighty = 3.0; // three times as high Button second = new Button("second"); gridbag.setConstraints(second, constraints); add(second); // ...
constraints.insets = new Insets(5, 10, 15, 20);This tells the GridBagLayout to leave 5 pixels at the top, 10 on the left, 15 on the bottom and 20 on the right of the container. To create space around a component, the constraints ipadx and ipady are used.