Java Reference
In-Depth Information
can then change the properties of the layout manager through the variable. For example, the
following code creates a layout manager and sets its properties:
// Create a layout manager
FlowLayout flowLayout = new FlowLayout();
// Set layout properties
flowLayout.setAlignment(FlowLayout.RIGHT);
flowLayout.setHgap( 10 );
flowLayout.setVgap( 20 );
12.6
Will the program work if ShowFlowLayout in line 23 in Listing 12.3 is replaced by
JFrame ?
Will the program work if ShowGridLayout in line 23 in Listing 12.4 is replaced by
JFrame ?
Will the program work if ShowBorderLayout line 20 in Listing 12.5 is replaced by
JFrame ?
Check
Point
12.7
Why do you need to use layout managers? What is the default layout manager for a
frame? How do you add a component to a frame?
12.8
Describe FlowLayout . How do you create a FlowLayout manager? How do you
add a component to a FlowLayout container? Is there a limit to the number of com-
ponents that can be added to a FlowLayout container? What are the properties for
setting the horizontal and vertical gaps between the components in the container?
Can you specify alignment?
12.9
Describe GridLayout . How do you create a GridLayout manager? How do you
add a component to a GridLayout container? Is there a limit to the number of com-
ponents that can be added to a GridLayout container? What are the properties for
setting the horizontal and vertical gaps between the components in the container?
12.10
Describe BorderLayout . How do you create a BorderLayout manager? How do
you add a component to a BorderLayout container? What are the properties for set-
ting the horizontal and vertical gaps between the components in the container?
12.11
The following program is supposed to display a button in a frame, but nothing is dis-
played. What is the problem?
1 public class Test extends javax.swing.JFrame {
2 public Test() {
3 add( new javax.swing.JButton( "OK" ));
4 }
5
6
public static void main(String[] args) {
7
8 frame.setSize( 100 , 200 );
9 frame.setVisible( true );
10 }
11 }
javax.swing.JFrame frame = new javax.swing.JFrame();
12.6 Using Panels as Subcontainers
A container can be placed inside another container. Panels can be used as
subcontainers to group GUI components to achieve the desired layout.
Suppose that you want to place ten buttons and a text field in a frame. The buttons are placed
in grid formation, but the text field is placed on a separate row. It is difficult to achieve the
desired look by placing all the components in a single container. With Java GUI program-
ming, you can divide a window into panels. Panels act as subcontainers to group user-
interface components. You add the buttons in one panel, then add the panel to the frame.
Key
Point
VideoNote
Use panels as subcontainers
 
 
Search WWH ::




Custom Search