Week 9: Java GUI Part II


Overview of Events Handling


Some more Syntax - The Inner classes

Frame f = new Frame("My frame");
Button quit = new Button("Quit"); 
// Create a temp variable which must be final
final Frame f2 = f; 
// Create a anonymous class which act as a listner for the button
quit.addActionListener (new ActionListener ()
	public static void actionPerformed (ActionEvent event)


 * A frame that will close when user select to close the window

import java.awt.*;
import java.awt.event.*;

public class CloseableFrame extends Frame
	public CloseableFrame()
		addWindowListener(new WindowAdapter() 
			public void windowClosing(WindowEvent e) 
				{ System.exit(0); } 
		} );
      		setSize(300, 200);




// Here is a class that implements the ActionListener interface.
// There is only one noticication method.
class MyActionListener implements ActionListener
	public void actionPerformed (ActionEvent event)
		System.out.println("The button was pressed.");
// In another file with some other class, we
// create a button.
Button b = new Button("Hit me"); 
// Add our custom action listener to the button.
MyActionListener customActionListener = new MyActionListener ();
b.addActionListener (customActionListener); 
//Now, when ever the button is pressed,
// MyActionListener.actionPerformed() is called 


ActionEvent and ActionListener


ItemEvent and ItemListener


// Here is a class that implements the ItemListener Interface
class MyItemListener implements ItemListener
	public void itemStateChanged (ItemEvent event)
		System.out.println("Item " + event.getItem() + "was selected,");
// Here is a code snippet that creates a choice and 
// processes item selections (assume this is inside a main, etc.) 
// Create a choice.
Choice ch = new Choices();
ch.add("American Express"); 
// Add our custom item listener to the choice.
MyItemListen customItemListener = new MyItemListener();
ch.addItemListener (customItemListener); 
// Now, when ever an item is selected,
// MyItemListener.itemStateChanged() is called. 


KeyEvent and KeyListener


// Here is a class that implements the KeyListener interface
// (by extending the KeyAdapter class).
class MyKeyListener extends KeyAdapter
	public void keyPressed (KeyEvent event)
		if (event.getKeyCode() == KeyEvent.VK_ENTER)
		System.out.println("Enter was pressed");
// Here is a code snippet that creates a text field and
// processes the enter key (assume this is inside a main, etc.) 
// Create a text field.
TextField tf = new TextField(30);
// Add our custom key listener to the text field.
MyKeyListener customKeyListener = new MyKeyListener();
tf.addKeyListener (customKeyListener); 
// Now, when ever the enter key is pressed in the text field,
// MyKeyListener.keyPressed() is called 


WindowEvent and WindowListener


// Here is a class that implements the WindowListener interface
// (by extending the WindowAdapter class).
class MyWindowListener extends WindowAdapter
	public void windowIconified (WindowEvent event)
		System.out.println("Window was iconified.");
	public void windowDeiconified (WindowEvent event)
		System.out.println("Window was deiconified.");
// Here is a code snippet that creates a frame and reports
// when it is iconified and deiconified
// (assume this is inside a main, etc) 
// Create a frame.
Frame f = new Frame("This is only a test"); 
// Add our custom window listener to the frame
MyWindowListener customWindowListener = new MyWindowListener ();
f.addWindowListener (customWindowListener); 
// Show the frame
// Now, when ever the window is iconified or deiconified,
// the custom window listener methods are called. 


What else ?



Create a GUI applicaton Week9 that has the following look and feel: