awt
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了awt相关的知识,希望对你有一定的参考价值。
摘自:http://www.ntu.edu.sg/home/ehchua/programming/java/J4a_GUI.html
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) { }
}
以上是关于awt的主要内容,如果未能解决你的问题,请参考以下文章