Java Reference
In-Depth Information
NORTH
WEST CENTER EAST
SOUTH
FIGURE 12.1: The border layout.
public static void main(String [] args) {
MyFrame f = new MyFrame ( ) ;
f . setVisible( true );
}
}
class MyFrame extends JFrame
{
public MyFrame ( ) {
JPanel p1 = new JPanel () ;
p1. setBackground(Color .RED) ;
JPanel p2 = new JPanel () ;
p2. setBackground(Color .GREEN) ;
JPanel p3 = new JPanel () ;
p3. setBackground(Color .ORANGE) ;
JPanel p4 = new JPanel () ;
p4. setBackground(Color .WHITE) ;
JPanel p5 = new JPanel () ;
p5. setBackground(Color .BLACK) ;
add(p1 , BorderLayout .NORTH) ;
add(p2 , BorderLayout .WEST) ;
add(p3 , BorderLayout .CENTER) ;
add(p4 , BorderLayout .EAST) ;
add(p5 , BorderLayout .SOUTH) ;
pack() ;
}
}
Note that the non-center areas are small. The reason is that their size is automatically
determined by the components in them. Since in this case they are empty panels, the default
minimal size is used. As the window is resized, only the center panel changes size. One can
specify that a panel is to use the border layout. For example, the code p1.setLayout(new
BorderLayout()) will make panel p1 use the border layout.
12.4 Text Fields and Labels
Next, we will write a program that converts between degrees Celsius and degrees Fahren-
heit. The program will have the design that is shown in Figure 12.2. We will create three
panels: in the north, center, and south. We will make the frame non-resizable so that the
 
Search WWH ::




Custom Search