import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class focus_panel extends Applet implements ActionListener, WindowListener {

Frame frame;
BoxPanel boxPanelA, boxPanelB;
boolean panelAShowing;

public void init() 
{
	frame = new Frame("A frame");
	frame.setLayout(new GridBagLayout());
	frame.setBounds(100, 100, 600, 300);
	boxPanelA = new BoxPanel("A");
	boxPanelB = new BoxPanel("B");
	boxPanelA.button.addActionListener(this);
	boxPanelB.button.addActionListener(this);
	frame.validate();
	frame.show();
	frame.addWindowListener(this);
	displayProperPanel();
}

void displayProperPanel()
{
	if (panelAShowing)
	{
		frame.remove(boxPanelA);
		constrain(frame, boxPanelB, 0, 0, 1, 1, GridBagConstraints.WEST, 0, 0, 0, 0, 
			GridBagConstraints.NONE, 0, 0);
		panelAShowing = false;
	}
	else
	{
		frame.remove(boxPanelB);
		constrain(frame, boxPanelA, 0, 0, 1, 1, GridBagConstraints.WEST, 0, 0, 0, 0, 
			GridBagConstraints.NONE, 0, 0);
		panelAShowing = true;
	}
	frame.invalidate();
	frame.validate();
	if (panelAShowing) boxPanelA.panelTextField.requestFocus();
	else boxPanelB.panelTextField.requestFocus();
}

static final void constrain(Container container, Component component, int grid_x, int grid_y, 
		int grid_width, int grid_height, int anchor, int topPad, int leftPad, int bottomPad, 
		int rightPad, int fill, double weightx, double weighty)
{
	GridBagConstraints c = new GridBagConstraints();
	c.gridx = grid_x;
	c.gridy = grid_y;
	c.gridwidth = grid_width;
	c.gridheight = grid_height;
	c.anchor = anchor;
	c.insets.top   = topPad;
	c.insets.left  = leftPad;
	c.insets.bottom= bottomPad;
	c.insets.right = rightPad;
	c.fill = fill;
	c.weightx = weightx;
	c.weighty = weighty;
	((GridBagLayout)container.getLayout()).setConstraints(component, c);
	container.add(component);
}

public void windowOpened(WindowEvent we){}

public void windowClosed(WindowEvent we){}

public void windowIconified(WindowEvent we){}

public void windowDeiconified(WindowEvent we){}

public void windowActivated(WindowEvent we){}

public void windowDeactivated(WindowEvent we){}

public void windowClosing(WindowEvent we)
{
	if (we.getSource() == frame)
	{
		frame.setVisible(false);
		frame.dispose();
	}
}

public void paint(Graphics g)
{
	g.drawString("A frame should pop up", 10 , 20);
}

public void actionPerformed(ActionEvent ae)
{
	if (ae.getSource() instanceof Button)
	{
		displayProperPanel();
	}
}
}  // END OF Class focus_panel



class BoxPanel extends Panel  {  // panel with a box around it
int width = 0;
int height = 0;
TextField panelTextField;
Button button; 

BoxPanel(String s)
{
	setLayout(new GridBagLayout());
	Label panelLabel = new Label(s);
	focus_panel.constrain(this, panelLabel, 0, 0, 1, 1, GridBagConstraints.WEST, 20, 20, 20, 20, 
		GridBagConstraints.VERTICAL, 0, 1);
	panelTextField = new TextField(5);
	focus_panel.constrain(this, panelTextField, 0, 1, 1, 1, GridBagConstraints.WEST, 20, 20, 20, 20, 
		GridBagConstraints.VERTICAL, 0, 1);
	button = new Button("Change panel");
	focus_panel.constrain(this, button, 0, 2, 1, 1, GridBagConstraints.WEST, 0, 20, 0, 0, 
		GridBagConstraints.NONE, 0, 0);
}

public void addNotify()
{
	super.addNotify();
	repaint();
}

public final void paint(Graphics g)
{
	width = getSize().width;
	height = getSize().height;
	g.setColor(Color.black);
	g.drawRect(0, 0, width -1, height -1); // out 
	g.drawRect(1, 1, width -3, height -3); // in 
}
}  // END OF Class BoxPanel

