import java.awt.*;
import java.awt.event.*;
public class LayoutTest extends Frame {
Button bNorth=new Button("North");
Button bSouth = new Button("South");
Button bEast = new Button("East");
Button bWest = new Button("West");
Button bCenter = new Button("Center");
//Initilizer
{bNorth.addActionListener(new ActionListener() {public void actionPerformed (ActionEvent ae) {System.exit(0);}}); }
//constructor
public LayoutTest() {
super("Reset layout manager testing");
add(bNorth,BorderLayout.NORTH);
add(bSouth,BorderLayout.SOUTH);
add(bWest,BorderLayout.WEST);
add(bEast,BorderLayout.EAST);
add(bCenter,BorderLayout.CENTER);
//reset layout manager
setLayout(new BorderLayout()); //compile and run, but present an empty frame.
//setLayout(new FlowLayout()); show all the five buttons in FlowLayout
//setLayout(new GridLayout()); show all the five buttons in GridLayout
//setLayout(new CardLayout()); just show bNorth button
//setLayout(new GridBagLayout()); show all the five butons in GridBagLayout
setSize(300,300);
setVisible(true);
}
public static void main(
String args[]){
LayoutTest t = new LayoutTest();
}
}
For the above program, when reset layout manager to BorderLayout, none of the five buttons will be shown. But reset to one of the other four layout managers in java.awt package will show all or some of the button(s).
Jane Griscti ever explained that: "BorderLayout implements LayoutManager2. All layouts implementing this interface keep track of their components; they do not query the container. In this example, no components were added while BorderLayout was active so it thinks it has none and therefore displays nothing."
But CardLayout and GridBagLayout also implements LayoutManager2, but after reset layout manager to either of them, some components have been displayed. I'm confused here. Can somebody explain?
Thanks.