程序7-15:
import java.awt.*;
import javax.swing.*;
public class ActionEventDemo2 {
public static void main(String args[]) {
JFrame frame = new JFrame ("ActionEvent Demo2");
//创建自定义组件MyButton的实例
MyButton b = new MyButton("Close");
frame.getContentPane().add(b,BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
}
//下面是MyButton类的定义:
import javax.swing.*;
import java.awt.event.*;
public class MyButton extends JButton implements ActionListener {
public MyButton(String text) {
super(text);
//注册事件的监听程序
addActionListener(this);
}
//出现ActionEvent事件时,将结束程序的运行
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
import java.awt.*;
import javax.swing.*;
public class ActionEventDemo2 {
public static void main(String args[]) {
JFrame frame = new JFrame ("ActionEvent Demo2");
//创建自定义组件MyButton的实例
MyButton b = new MyButton("Close");
frame.getContentPane().add(b,BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
}
//下面是MyButton类的定义:
import javax.swing.*;
import java.awt.event.*;
public class MyButton extends JButton implements ActionListener {
public MyButton(String text) {
super(text);
//注册事件的监听程序
addActionListener(this);
}
//出现ActionEvent事件时,将结束程序的运行
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}