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

public class LayoutTest extends JFrame 
{
    private JLabel eventStatus = new JLabel(" ");
    private JButton ok = new JButton("ok"); 
    private JButton cancel = new JButton("cancel"); 
    private JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER,5,5));
    private JLabel question = new JLabel("Save file?"); 
    private JTextField filename = new JTextField(20);
    private MyCanvas myCanvas = new MyCanvas();

    private WindowHandler wh;

    public static void main(String[] args) {
	LayoutTest lt = new LayoutTest();
    }	

    public LayoutTest() 
    {
	GridBagLayout layout = new GridBagLayout();
	getContentPane().setLayout(layout); 

	GridBagConstraints gbc = new GridBagConstraints();
	gbc.fill = GridBagConstraints.BOTH;
	gbc.anchor = GridBagConstraints.NORTHWEST;
	gbc.insets = new Insets(0, 0, 5, 5);

	applySettings();
	addListeners();

	buttonPanel.add(ok); 
	buttonPanel.add(cancel);

	addUsingGBL(question, gbc, 0, 0, 3, 1);
	addUsingGBL(filename, gbc, 0, 1, 3, 1); 
	addUsingGBL(myCanvas, gbc, 3, 0, 3, 3); 
	addUsingGBL(buttonPanel, gbc, 0, 2, 3, 1); 
	addUsingGBL(eventStatus, gbc, 0, 3, 6, 1);
	
	pack();
	setVisible(true); 
    }
    
    private void applySettings() {
	// frame settings
	getContentPane().setBackground(Color.lightGray);

	// component settings
	eventStatus.setBackground(Color.cyan);
	ok.setBackground(Color.green); 
	cancel.setBackground(Color.red);
	question.setBackground(Color.yellow);

	eventStatus.setOpaque(true);
	ok.setOpaque(true);
	cancel.setOpaque(true);
	question.setOpaque(true);
	filename.setOpaque(true);
    }

    private void addListeners() {
	// window quit button
	addWindowListener(new WindowHandler(this)); 

	ok.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent evt){ 
		    eventStatus.setText("ok");
		}	    
	    });
	
	cancel.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent evt){ 
		    eventStatus.setText("cancel");
		}	    
	    });


	filename.addKeyListener(new KeyAdapter() {
		public void keyTyped(KeyEvent evt){
		    eventStatus.setText((new Character(evt.getKeyChar())).toString());
		}
	    });

	filename.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent evt){
		    eventStatus.setText(((TextField) evt.getSource()).getText());
		} 
	    });

	myCanvas.addMouseListener(new MouseAdapter() {
		public void mouseClicked(MouseEvent evt){
		    eventStatus.setText("(" + evt.getX() + "," + evt.getY() + ")");
		}
	    });
    }

    private void addUsingGBL(Component comp, GridBagConstraints gbc, 
			     int x, int y, int w, int h) {
	gbc.gridx = x; 
	gbc.gridy = y;
	gbc.gridwidth = w; 
	gbc.gridheight = h;
	getContentPane().add(comp,gbc);
    }
}

class MyCanvas extends JPanel {
    public Dimension getPreferredSize() { return new Dimension(160,160); }

    public void paintComponent(Graphics g){
	super.paintComponent(g);
	setBackground(Color.white);
	setOpaque(true);
	g.drawString("test",10,20); 
    }
}

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); 
    }
}

