GridLayout
You must read How to Use GridLayout.
GridTest.java
You can get the program here .
After you resize it, you can get
 |
 |
1:import java.awt.*;
2:import javax.swing.*;
3:
4:public class GridTest extends JFrame {
5: JButton[] buttons = new JButton[5];
6: JLabel[] labels = new JLabel[5];
7: String[] strings = {"zero", "one", "two", "three", "four"};
8: Container contentPane;
9: public GridTest() {
10: super("GridTest");
11: contentPane = getContentPane();
12: int row = 5;
13: int col = 2;
14: contentPane.setLayout(new GridLayout(row,col));
15: // add labels and buttons
16: // the components are added from left to right, top to bottom
17: for(int i = 0; i < buttons.length; i++) {
18: // create labels and add to contentPane
19: labels[i] = new JLabel(strings[i], JLabel.CENTER);
20: contentPane.add(labels[i]);
21: // create buttons and add to contentPane
22: // Integer.toString(i) changes integer i to string
23: buttons[i] = new JButton(Integer.toString(i));
24: contentPane.add(buttons[i]);
25: }
26: }
27:
28: public static void main(String[] args) {
29: GridTest f = new GridTest();
30: f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
31: f.setSize(100,200);
32: f.setVisible(true);
33: }
34:}
Explanation
- A
GridLayout places components in a grid of cells.
- Each component takes all the available space within its cell,
and each cell is exactly the same size.
- If you resize the
GridLayout window, you'll see that
the GridLayout changes the cell size so that the cells are
as large as possible, given the space available to the container.
- The components are added from left to right, top to bottom.
Constructor
public GridLayout(int rows, int columns)
public GridLayout(int rows, int columns, int horizontalGap, int verticalGap)
- At least one of the
rows and columns arguments
must be nonzero.
- The
horizontalGap and verticalGap arguments
to the second constructor allow you to specify the number of pixels between cells.
- If you don't specify gaps, their values default to zero.
Add components
add(Componenet comp)
The components are added from left to right till a row is filled. Then they are
added to a new row.
Insert components
add(Componenet comp, int index)
Add the component at index.
Remove components
remove(int index)
Remove the component at index.
GridTest2.java
Get the program here.
 |

After uncomment line 17 and line 19. |
1:import java.awt.*;
2:import javax.swing.*;
3:
4:public class GridTest2 extends JFrame {
5: JButton[] buttons = new JButton[10];
6: Container contentPane;
7: public GridTest2() {
8: super("GridTest2");
9: contentPane = getContentPane();
10: int row = 4, col = 3, hgap = 10, vgap = 5;
11: contentPane.setLayout(new GridLayout(row, col, hgap, vgap));
12: for(int i = 0; i < buttons.length; i++) {
13: buttons[i] = new JButton(Integer.toString(i));
14: contentPane.add(buttons[i]);
15: }
16: // add a new Button at the 4th position
17: // contentPane.add(new JButton("insert"), 4);
18: // remove the 1st button
19: // contentPane.remove(1);
20: }
21:
22: public static void main(String[] args) {
23: GridTest2 f = new GridTest2();
24: f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
25: f.setSize(250,250);
26: f.setVisible(true);
27: }
28:}
Highlight of the program
- Use another constructor at line 11.
- add and remove components at line 17,19. The programs should be tested by
uncommenting them.
GridTest3.java
Get the program here .
1:import java.awt.*;
2:import javax.swing.*;
3:
4:public class GridTest3 extends JFrame {
5: JButton[] buttons = new JButton[10];
6: Container contentPane;
7: public GridTest3() {
8: super("GridTest3");
9: contentPane = getContentPane();
10: int row = 3;
11: // the layout will try to put the components in 3 rows
12: contentPane.setLayout(new GridLayout(row, 0));
13: for(int i = 0; i < buttons.length; i++) {
14: buttons[i] = new JButton(Integer.toString(i));
15: contentPane.add(buttons[i]);
16: }
17: }
18:
19: public static void main(String[] args) {
20: GridTest3 f = new GridTest3();
21: f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
22: f.setSize(250,250);
23: f.setVisible(true);
24: }
25:}
Highlight of the program
Line 12 set the number of columns = 0, components will be put in row rows.
GridTest4.java
Get the program here .
1:import java.awt.*;
2:import javax.swing.*;
3:
4:public class GridTest4 extends JFrame {
5: JButton[] buttons = new JButton[20];
6: Container contentPane;
7: public GridTest4() {
8: super("GridTest4");
9: contentPane = getContentPane();
10: int col = 6;
11: // the layout will try to put the components in 6 columns
12: contentPane.setLayout(new GridLayout(0, col));
13: for(int i = 0; i < buttons.length; i++) {
14: buttons[i] = new JButton(Integer.toString(i));
15: contentPane.add(buttons[i]);
16: }
17: }
18:
19: public static void main(String[] args) {
20: GridTest4 f = new GridTest4();
21: f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
22: f.setSize(300,100);
23: f.setVisible(true);
24: }
25:}
Highlight of the porgrams
By setting the number of rows equals to 0 at line 12. The components will be put in
col columns.