Page 740 - Introduction to Programming with Java: A Problem Solving Approach
P. 740

                706 Chapter 17 GUI Programming—Component Layout, Additional GUI Components
actual number of columns. That’s what we did in our previous example. We knew we wanted a 2-row by 3-column table with six buttons, so we specified 2 for the rows argument and 3 for the columns argument.
Case two:
Sometimes, you might want a row-oriented display. In other words, you want a certain number of rows displayed, and you don’t care about or aren’t sure about the number of columns. If that’s the case, call the GridLayout constructor with the actual number of rows for the rows argument and 0 for the columns argument. A 0 for the columns argument indicates that you’re leaving it up to the GridLayout manager to determine the number of columns.
The code fragment below generates a two-row GridLayout with five buttons. Since the setLayout call does not specify gap values, the GridLayout displays no gaps between the buttons.
setLayout(new GridLayout(2, 0));
add(new JButton("1"));
add(new JButton("2"));
add(new JButton("3"));
add(new JButton("4"));
add(new JButton("5"));
Assume the above code fragment is part of a complete, working program. Here’s what the program displays:
            Apago PDF Enhancer
Case three:
Sometimes, you might want a column-oriented display. In other words, you want a certain number of col- umns displayed, and you don’t care about or aren’t sure about the number of rows. If this is the case, call the GridLayout constructor with the actual number of columns for the columns argument and 0 for the rows argument. A 0 for the rows argument indicates that you’re leaving it up to the GridLayout manager to determine the number of rows.
The code fragment below generates a four-column GridLayout with five buttons. setLayout(new GridLayout(0, 4));
add(new JButton("1"));
add(new JButton("2"));
add(new JButton("3"));
add(new JButton("4"));
add(new JButton("5"));
Assume the above code fragment is part of a complete, working program. Here’s what the program displays:
 












































































   738   739   740   741   742