In this video, we will learn how to use Java.Awt.GridBagLayout & Java.AWT.GridBagConstraints. Here, you learn how to use Fill(Horizontal, Vertical, BOTH), WeightX, WeightY and GridWidth(1, REMINDER) properties.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
//Sample 01: Create Five Buttons & Give it to GridBagLayout Button b1 = new Button("B1"); Button b2 = new Button("B2"); Button b3 = new Button("B3"); Button b4 = new Button("B4"); Button b5 = new Button("B5"); setLayout(new GridBagLayout()); add(b1); add(b2); add(b3); add(b4); add(b5); ================================================================ //Example 02: Relative Size //Sample 01: Create 5 Buttons and Set Grid bag Layout Button b1 = new Button("B1"); Button b2 = new Button("B2"); Button b3 = new Button("B3"); Button b4 = new Button("B4"); Button b5 = new Button("B5"); GridBagLayout gbl = new GridBagLayout(); setLayout(gbl); //Sample 02: Set Grid bag Constraints GridBagConstraints gcon = new GridBagConstraints(); gcon.fill = GridBagConstraints.HORIZONTAL; gcon.weightx = 1; gbl.setConstraints(b1, gcon); add(b1); gcon.weightx = 2; gbl.setConstraints(b2, gcon); add(b2); gcon.weightx = 1; gbl.setConstraints(b3, gcon); add(b3); gbl.setConstraints(b4, gcon); add(b4); gbl.setConstraints(b5, gcon); add(b5); ================================================================ //Example 03: Multiple rows //Sample 3.1: Create 5 Buttons and Set Grid bag Layout Button b1 = new Button("B1"); Button b2 = new Button("B2"); Button b3 = new Button("B3"); Button b4 = new Button("B4"); Button b5 = new Button("B5"); GridBagLayout gbl = new GridBagLayout(); setLayout(gbl); //Sample 3.2: Add Button 1 GridBagConstraints gcon = new GridBagConstraints(); gcon.fill = GridBagConstraints.HORIZONTAL; gcon.weightx = 1; gbl.setConstraints(b1, gcon); add(b1); //Sample 3.3: Set Button 2 ends the Row gcon.gridwidth = GridBagConstraints.REMAINDER; gbl.setConstraints(b2, gcon); add(b2); //Sample 3.3: Add Button 3 & 4 in one row gcon.gridwidth = 1; gbl.setConstraints(b3, gcon); add(b3); gcon.gridwidth = GridBagConstraints.REMAINDER; gbl.setConstraints(b4, gcon); add(b4); //Sample 3.4: Add Button5 in a Separate Row gbl.setConstraints(b5, gcon); add(b5); ================================================================ a) Experiment Weighty = 1 b) Then Use gcon.fill = GridBagConstraints.VERTICAL; c) b) Then Use gcon.fill = GridBagConstraints.BOTH; ================================================================ |
Categories: AWT-Tube