摘自:http://www.ntu.edu.sg/home/ehchua/programming/java/J4a_GUI.html
主要内容:
1.ActionListener
2.WindowEvent
3.MouseEvent, MouseMotionListener
4.KeyEvent, KeyListener
1.ActionListener
import java.awt.*; // Using AWT container and component classes
import java.awt.event.*; // Using AWT event classes and listener interfaces
// An AWT program inherits from the top-level container java.awt.Frame
public class AWTCounter extends Frame implements ActionListener {
private Label lblCount; // Declare component Label
private TextField tfCount; // Declare component TextField
private Button btnCount; // Declare component Button
private int count = 0; // Counter's value
/** Constructor to setup GUI components and event handling */
public AWTCounter () {
setLayout(new FlowLayout());
// "super" Frame sets its layout to FlowLayout, which arranges the components
// from left-to-right, and flow to next row from top-to-bottom.
lblCount = new Label("Counter"); // construct Label
add(lblCount); // "super" Frame adds Label
tfCount = new TextField("0", 10); // construct TextField
tfCount.setEditable(false); // set to read-only
add(tfCount); // "super" Frame adds tfCount
btnCount = new Button("Count"); // construct Button
add(btnCount); // "super" Frame adds Button
btnCount.addActionListener(this);
// Clicking Button source fires ActionEvent
// btnCount registers this instance as ActionEvent listener
setTitle("AWT Counter"); // "super" Frame sets title
setSize(250, 100); // "super" Frame sets initial window size
// System.out.println(this);
// System.out.println(lblCount);
// System.out.println(tfCount);
// System.out.println(btnCount);
setVisible(true); // "super" Frame shows
// System.out.println(this);
// System.out.println(lblCount);
// System.out.println(tfCount);
// System.out.println(btnCount);
}
/** The entry main() method */
public static void main(String[] args) {
// Invoke the constructor to setup the GUI, by allocating an instance
AWTCounter app = new AWTCounter();
}
/** ActionEvent handler - Called back upon button-click. */
@Override
public void actionPerformed(ActionEvent evt) {
++count; // increase the counter value
// Display the counter value on the TextField tfCount
tfCount.setText(count + ""); // convert int to String
}
}
2.WindowEvent
import java.awt.*; // Using AWT containers and components
import java.awt.event.*; // Using AWT events and listener interfaces
// An AWT GUI program inherits the top-level container java.awt.Frame
public class WindowEventDemo extends Frame
implements ActionListener, WindowListener {
// This class acts as listener for ActionEvent and WindowEvent
// Java supports only single inheritance, where a class can extend
// one superclass, but can implement multiple interfaces.
private TextField tfCount;
private Button btnCount;
private int count = 0; // Counter's value
/** Constructor to setup the UI components and event handling */
public WindowEventDemo() {
setLayout(new FlowLayout()); // "super" Frame sets to FlowLayout
add(new Label("Counter")); // "super" Frame adds an anonymous Label
tfCount = new TextField("0", 10); // Allocate TextField
tfCount.setEditable(false); // read-only
add(tfCount); // "super" Frame adds tfCount
btnCount = new Button("Count"); // Declare and allocate a Button
add(btnCount); // "super" Frame adds btnCount
btnCount.addActionListener(this);
// btnCount fires ActionEvent to its registered ActionEvent listener
// btnCount adds "this" object as an ActionEvent listener
addWindowListener(this);
// "super" Frame fires WindowEvent to its registered WindowEvent listener
// "super" Frame adds "this" object as a WindowEvent listener
setTitle("WindowEvent Demo"); // "super" Frame sets title
setSize(250, 100); // "super" Frame sets initial size
setVisible(true); // "super" Frame shows
}
/** The entry main() method */
public static void main(String[] args) {
new WindowEventDemo(); // Let the construct do the job
}
/** ActionEvent handler */
@Override
public void actionPerformed(ActionEvent evt) {
++count;
tfCount.setText(count + "");
}
/** WindowEvent handlers */
// Called back upon clicking close-window button
@Override
public void windowClosing(WindowEvent e) {
System.exit(0); // Terminate the program
}
// Not Used, but need to provide an empty body
@Override
public void windowOpened(WindowEvent e) { }
@Override
public void windowClosed(WindowEvent e) { }
@Override
public void windowIconified(WindowEvent e) { }
@Override
public void windowDeiconified(WindowEvent e) { }
@Override
public void windowActivated(WindowEvent e) { }
@Override
public void windowDeactivated(WindowEvent e) { }
}
3.MouseEvent, MouseMotionListener
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
// An AWT GUI program inherits from the top-level container java.awt.Frame
public class MouseMotionDemo extends Frame
implements MouseListener, MouseMotionListener {
// This class acts as MouseListener and MouseMotionListener
// To display the (x, y) coordinates of the mouse-clicked
private TextField tfMouseClickX;
private TextField tfMouseClickY;
// To display the (x, y) coordinates of the current mouse-pointer position
private TextField tfMousePositionX;
private TextField tfMousePositionY;
/** Constructor to setup the GUI */
public MouseMotionDemo() {
setLayout(new FlowLayout()); // "this" frame sets to FlowLayout
add(new Label("X-Click: "));
tfMouseClickX = new TextField(10);
tfMouseClickX.setEditable(false);
add(tfMouseClickX);
add(new Label("Y-Click: "));
tfMouseClickY = new TextField(10);
tfMouseClickY.setEditable(false);
add(tfMouseClickY);
add(new Label("X-Position: "));
tfMousePositionX = new TextField(10);
tfMousePositionX.setEditable(false);
add(tfMousePositionX);
add(new Label("Y-Position: "));
tfMousePositionY = new TextField(10);
tfMousePositionY.setEditable(false);
add(tfMousePositionY);
addMouseListener(this);
addMouseMotionListener(this);
// "super" frame fires MouseEvent to all its registered MouseListener and MouseMotionListener
// "super" frame adds "this" object as MouseListener and MouseMotionListener
setTitle("MouseMotion Demo"); // "super" Frame sets title
setSize(400, 120); // "super" Frame sets initial size
setVisible(true); // "super" Frame shows
}
/** The entry main() method */
public static void main(String[] args) {
new MouseMotionDemo(); // Let the constructor do the job
}
/** MouseListener handlers */
// Called back when a mouse-button has been clicked
@Override
public void mouseClicked(MouseEvent e) {
tfMouseClickX.setText(e.getX() + "");
tfMouseClickY.setText(e.getY() + "");
}
// Not Used, but need to provide an empty body for compilation
@Override
public void mousePressed(MouseEvent e) { }
@Override
public void mouseReleased(MouseEvent e) { }
@Override
public void mouseEntered(MouseEvent e) { }
@Override
public void mouseExited(MouseEvent e) { }
/** MouseMotionEvent handlers */
// Called back when the mouse-pointer has been moved
@Override
public void mouseMoved(MouseEvent e) {
tfMousePositionX.setText(e.getX() + "");
tfMousePositionY.setText(e.getY() + "");
}
// Not Used, but need to provide an empty body for compilation
@Override
public void mouseDragged(MouseEvent e) { }
}
4.KeyEvent, KeyListener
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
// An AWT GUI program inherits from the top-level container java.awt.Frame
public class KeyEventDemo extends Frame implements KeyListener {
// This class acts as KeyEvent Listener
private TextField tfInput; // single-line TextField to receive tfInput key
private TextArea taDisplay; // multi-line TextArea to taDisplay result
/** Constructor to setup the GUI */
public KeyEventDemo() {
setLayout(new FlowLayout()); // "super" frame sets to FlowLayout
add(new Label("Enter Text: "));
tfInput = new TextField(10);
add(tfInput);
taDisplay = new TextArea(5, 40); // 5 rows, 40 columns
add(taDisplay);
tfInput.addKeyListener(this);
// tfInput TextField fires KeyEvent to its registered KeyListeners
// tfInput adds "this" object as a KeyEvent listener
setTitle("KeyEvent Demo"); // "super" Frame sets title
setSize(400, 200); // "super" Frame sets initial size
setVisible(true); // "super" Frame shows
}
/** The entry main() method */
public static void main(String[] args) {
new KeyEventDemo(); // Let the constructor do the job
}
/** KeyEvent handlers */
// Called back when a key has been typed (pressed and released)
@Override
public void keyTyped(KeyEvent e) {
taDisplay.append("You have typed " + e.getKeyChar() + "\n");
}
// Not Used, but need to provide an empty body for compilation
@Override
public void keyPressed(KeyEvent e) { }
@Override
public void keyReleased(KeyEvent e) { }
}