To produce the display shown in Segment 1108, you first set
the minimum and preferred sizes to 0
in the definition of the
IndifferentButton
button component:
import java.awt.*; import javax.swing.*; public class IndifferentButton extends JButton { public IndifferentButton (String t) { super(t); } public Dimension getMinimumSize() {return new Dimension(0, 0);} public Dimension getPreferredSize() {return new Dimension(0, 0);} }
Then, you insert adjustments to the gridx
and gridy
parameters:
import java.awt.*; import javax.swing.*; public class TestPanel extends JPanel { public TestPanel () { GridBagLayout gbl = new GridBagLayout(); setLayout(gbl); GridBagConstraints gbc = new GridBagConstraints(); // Fill both ways: gbc.fill = GridBagConstraints.BOTH; // Allow expansion of all components: gbc.weightx = 2.0; gbc.weighty = 1.0; // Place components: placeComponent(0, 0, 1, 2, "A", gbl, gbc); placeComponent(0, 2, 1, 3, "C", gbl, gbc); gbc.weightx = 1.0; placeComponent(1, 0, 1, 1, "B", gbl, gbc); placeComponent(1, 1, 1, 4, "D", gbl, gbc); gbc.weightx = 3.0; placeComponent(2, 0, 1, 1, "E", gbl, gbc); placeComponent(2, 1, 1, 1, "F", gbl, gbc); gbc.weighty = 2.0; placeComponent(2, 2, 1, 1, "G", gbl, gbc); gbc.weighty = 1.0; placeComponent(2, 3, 1, 1, "H", gbl, gbc); placeComponent(2, 4, 1, 1, "I", gbl, gbc); } // Definition of placeComponent as in Segment 1106 }