import java.awt.*;
import java.awt.event.*;public class Test123 implements ActionListener{
Frame f = new Frame("窗口");
public void init(){
Button b = new Button("退出");
b.addActionListener(new Test123());
f.add(b);
f.setSize(434,353);
f.setVisible(true);
}
public void actionPerformed(ActionEvent e){
f.dispose();
System.exit(0);
}
public static void main(String[] args){
Test123 t = new Test123();
t.init();
}
}这是《Java就业培训教程》上的一个样例代码,书上说init()方法里的b.addActionListener(new Test123())和main()方法里的Test123 t = new Test123()分别创建了两个Test123对象,所以程序运行后按按钮“退出”没有退出。可是我在电脑上运行的没问题哇,求大神解决。
import java.awt.event.*;public class Test123 implements ActionListener{
Frame f = new Frame("窗口");
public void init(){
Button b = new Button("退出");
b.addActionListener(new Test123());
f.add(b);
f.setSize(434,353);
f.setVisible(true);
}
public void actionPerformed(ActionEvent e){
f.dispose();
System.exit(0);
}
public static void main(String[] args){
Test123 t = new Test123();
t.init();
}
}这是《Java就业培训教程》上的一个样例代码,书上说init()方法里的b.addActionListener(new Test123())和main()方法里的Test123 t = new Test123()分别创建了两个Test123对象,所以程序运行后按按钮“退出”没有退出。可是我在电脑上运行的没问题哇,求大神解决。
System.exit(0);
都执行了
运行下就是书上要表达的意思了。
不理解的话就把
b.addActionListener(new Test123());
改成
b.addActionListener(this);
再运行下,就能退出了。
书上大概是想说明事件注册对象的问题,
而System.exit(0)是直接终止java虚拟机,明显的笔误了。
System.exit(0)是直接终止java虚拟机,
java程序是靠java虚拟机才能运行的,都终止虚拟机了,程序当然退出了