------------------------------------------------- |FIRST_LINE_START PAGE_START FIRST_LINE_END| | | | | |LINE_START CENTER LINE_END| | | | | |LAST_LINE_START PAGE_END LAST_LINE_END| -------------------------------------------------
| 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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | import javax.swing.*; import java.awt.*; import java.awt.Color; public class GridBagLayoutExample{ public JPanel createContentPane (){ JPanel totalGUI = new JPanel(); // We create a JPanel with the GridBagLayout. // We also create a GridBagConstraints Object. JPanel mainPanel = new JPanel(); mainPanel.setLayout(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); // For each item we create to add to the mainPanel... JPanel red = createSquareJPanel(Color.red, 50); // We need to define the GridBagConstraints. // This defines the widget to go in grid (0,0) c.gridx = 0; c.gridy = 0; // This tells the widget not to change size // to fit the cell. c.fill = GridBagConstraints.NONE; // Once we are happy with the constraints we set. // We can add the widget to the JPanel. // Remember to add the constraints as the second argument. mainPanel.add(red, c); // The constraints we used for the previous one are still in c // so we can use them again for the next widget if we wish. JPanel blue = createSquareJPanel(Color.blue, 60); c.gridx = 1; // c.gridy = 0 is still set from the previous constraints. // c.fill = GridBagConstraints.NONE; is still set too. mainPanel.add(blue, c); JPanel green = createSquareJPanel(Color.green, 70); c.gridx = 2; mainPanel.add(green, c); JPanel orange = createSquareJPanel(Color.orange, 80); c.gridx = 3; mainPanel.add(orange, c); JPanel yellow = createSquareJPanel(Color.yellow, 90); c.gridx = 4; mainPanel.add(yellow, c); // We finish by adding the mainPanel to the totalGUI // and returning the JPanel to be set as the content pane. totalGUI.add(mainPanel); totalGUI.setOpaque(true); return totalGUI; } // In this method, we create a square JPanel of a colour and set size // specified by the arguments. private JPanel createSquareJPanel(Color color, int size) { JPanel tempPanel = new JPanel(); tempPanel.setBackground(color); tempPanel.setMinimumSize(new Dimension(size, size)); tempPanel.setMaximumSize(new Dimension(size, size)); tempPanel.setPreferredSize(new Dimension(size, size)); return tempPanel; } private static void createAndShowGUI() { JFrame.setDefaultLookAndFeelDecorated(true); JFrame frame = new JFrame("[=] GridBagLayout [=]"); GridBagLayoutExample demo = new GridBagLayoutExample(); frame.setContentPane(demo.createContentPane()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); } public static void main(String[] args) { //Schedule a job for the event-dispatching thread: //creating and showing this application's GUI. SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } } |
| 14 15 16 | JPanel mainPanel = new JPanel(); mainPanel.setLayout(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); |
| 25 26 | c.gridx = 0; c.gridy = 0; |
| 31 | c.fill = GridBagConstraints.NONE;
|
| 37 | mainPanel.add(red, c); |
| 42 43 44 45 46 47 | JPanel blue = createSquareJPanel(Color.blue, 60); c.gridx = 1; // c.gridy = 0 is still set from the previous constraints. // c.fill = GridBagConstraints.NONE; is still set too. mainPanel.add(blue, c); |
| 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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | import javax.swing.*; import java.awt.*; import java.awt.Color; public class GridBagLayoutExample_Extended{ public JPanel createContentPane (){ JPanel totalGUI = new JPanel(); // We create a JPanel with the GridBagLayout. // We also create a GridBagConstraints Object. JPanel mainPanel = new JPanel(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); // For each item we create to add to the mainPanel // we set constraints. // This one is cell (0,0) and will not resize. JPanel red = createSquareJPanel(Color.red, 80); c.gridx = 0; c.gridy = 0; c.fill = GridBagConstraints.NONE; mainPanel.add(red, c); // This one is cell (1,0) and will not resize. JPanel blue = createSquareJPanel(Color.blue, 80); c.gridx = 1; mainPanel.add(blue, c); // This one is cell (2,0). // This should be a 40x40 square, but we set it to // stretch on the vertical. JPanel orange = createSquareJPanel(Color.orange, 40); c.gridx = 2; c.fill = GridBagConstraints.VERTICAL; mainPanel.add(orange, c); // This starts in cell (3,0) // but covers (3, 1) and (3,2) as it's height is 3. // This will stretch right down to the bottom because // the fill is still set to vertical. JPanel pink = createSquareJPanel(Color.pink, 80); c.gridx = 3; c.gridheight = 3; mainPanel.add(pink, c); // This one is in cell (0, 1) // It is now a simple green square. JPanel green = createSquareJPanel(Color.green, 80); c.gridx = 0; c.gridy = 1; c.gridheight = 1; c.fill = GridBagConstraints.NONE; mainPanel.add(green, c); // This one is cell (0,2). // This should be a 40x40 square, but we set it to // stretch on the horizontal. JPanel yellow = createSquareJPanel(Color.yellow, 40); c.gridy = 2; c.fill = GridBagConstraints.HORIZONTAL; mainPanel.add(yellow, c); // This starts in cell (0,3) // but covers (1,3) and (2,3) as it's width is 3. // This will stretch right along to the side because // the fill is still set to horizontal. JPanel cyan = createSquareJPanel(Color.cyan, 80); c.gridy = 3; c.gridwidth = 3; mainPanel.add(cyan, c); // This will place a JPanel at the bottom corner, small and positioned // to first_line_start in cell (3,3). JPanel black = createSquareJPanel(Color.black, 40); c.gridx = 3; c.gridwidth = 1; c.anchor = GridBagConstraints.FIRST_LINE_START; c.fill = GridBagConstraints.NONE; mainPanel.add(black, c); // This sets a gray JPanel in the middle. // it covers (1,1), (1,2), (2,1) and (2,2) because // of it's width and height. // We center it using the anchor. // Then make it larger by padding it out by 40px on the // vertical and horizontal (20px each side). JPanel gray = createSquareJPanel(Color.gray, 40); c.gridx = 1; c.gridy = 1; c.gridheight = 2; c.gridwidth = 2; c.anchor = GridBagConstraints.CENTER; c.ipadx = 40; c.ipady = 40; mainPanel.add(gray, c); // We finish by adding the mainPanel to the totalGUI // and returning the JPanel to be set as the content pane. totalGUI.add(mainPanel); totalGUI.setOpaque(true); return totalGUI; } // In this method, we create a square JPanel of a colour and set size // specified by the arguments. private JPanel createSquareJPanel(Color color, int size) { JPanel tempPanel = new JPanel(); tempPanel.setBackground(color); tempPanel.setMinimumSize(new Dimension(size, size)); tempPanel.setMaximumSize(new Dimension(size, size)); tempPanel.setPreferredSize(new Dimension(size, size)); return tempPanel; } private static void createAndShowGUI() { JFrame.setDefaultLookAndFeelDecorated(true); JFrame frame = new JFrame("[=] GridBagLayout [=]"); GridBagLayoutExample_Extended demo = new GridBagLayoutExample_Extended(); frame.setContentPane(demo.createContentPane()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); } public static void main(String[] args) { //Schedule a job for the event-dispatching thread: //creating and showing this application's GUI. SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } } |

