import java.awt.*; 
import java.awt.event.*;
import javax.swing.*;

public class LayoutTest {
    public static void main(String[] args) {
	LayoutTestFrame test = new LayoutTestFrame(); 
	test.getContentPane().setLayout(new GridLayout(3,2,5,5)); 

	// add components
	JButton ok = new JButton("ok"); 
	ok.setBackground(Color.green); 
	ok.setOpaque(true);

	JButton cancel = new JButton("cancel"); 
	cancel.setBackground(Color.red);
	cancel.setOpaque(true);

	JLabel question = new JLabel("Save file?"); 
	question.setBackground(Color.yellow);
	question.setOpaque(true);
	
	JTextField filename = new JTextField(10);

	// nested class
	class MyCanvas extends JPanel {
	    public Dimension getPreferredSize() { return new Dimension(40,40); }

	    public void paintComponent(Graphics g){
		super.paintComponent(g);
		setBackground(Color.white);
		setOpaque(true);
		g.drawString("test",10,20); 
	    }
	}
	MyCanvas myCanvas = new MyCanvas();
	
	test.getContentPane().add(question);
	test.getContentPane().add(ok); 
	test.getContentPane().add(cancel); 
	test.getContentPane().add(filename); 
	test.getContentPane().add(myCanvas); 

	test.setVisible(true); 
    }
}

class LayoutTestFrame extends JFrame {
    private WindowHandler wh;

    public LayoutTestFrame() {
	setBackground(Color.lightGray);
	setSize(400,300);
	setLocation(100,100);

	wh = new WindowHandler(this); 
	addWindowListener(wh);
    }
}
 
class WindowHandler extends WindowAdapter {
    private Container parent; 

    public WindowHandler(Container parent){
	this.parent = parent; 
    }
    
    public void windowClosing(WindowEvent evt){ 
	((Frame) parent).dispose();
	System.exit(0); 
    }
}

