幼儿园人数标准:java小程序的窗口关闭问题

来源:百度文库 编辑:中科新闻网 时间:2024/05/14 03:36:03
第一个小程序:
import java.awt.*;
import java.awt.event.*;
public class TwoListen implements MouseListener,MouseMotionListener,WindowListener {
private Frame f;
private TextField tf;

public static void main(String argv[]){
TwoListen two=new TwoListen();
two.go();
}
public void go(){
f=new Frame("Two Listeners example");
f.add(new Label("click and drag the mouse"),"North");
tf=new TextField(30);
f.add(tf,"South");

f.addMouseListener( this);
f.addMouseMotionListener(this);
f.addWindowListener(this);
f.setSize(300,200);
f.setVisible(true);
}
public void mouseDragged(MouseEvent e){
String s="Mouse dragging:X="+e.getX()+"Y="+e.getY();
tf.setText(s);
}
public void mouseMoved(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){
String s="The mouse entered";
tf.setText(s);
}
public void mouseExited(MouseEvent e){
String s="The mouse has left the building";
tf.setText(s);
}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void windowClosing(WindowEvent e){
System.exit(1);
}
public void windowOpened(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
public void windowClosed(WindowEvent e){}
public void windowActivated(WindowEvent e){}
public void windowDeactivated(WindowEvent e){}

}
*************************************************************
第二个:
import java.awt.*;
import java.awt.event.*;
public class Interfaceforme extends WindowAdapter{
private Frame f;
private Button b;

public static void main(String argv[]){
Interfaceforme face=new Interfaceforme();
face.go();
}
public void go(){
f=new Frame("Two Listeners example");
f.add(new Label("click and drag the mouse"),"North");
b=new Button("hi");
f.add(b,"South");

f.setSize(300,200);
f.setVisible(true);
}
public void windowClosing(WindowEvent e){
System.exit(1);
}
public void windowClosed(WindowEvent e){}

}

************************************************************
其中第一个程序运行后,点窗口的“X”可以正常关闭,而第二个却不能,一定要用任务管理器才行。请问到底是为什么??为什么第二个不能正常关闭呢?
第一个是可以关闭的!第二个不行,我想知道为什么,然后把第二个也变成可关闭的

 
 
 
第二个不能正常关闭的原因是程序显示的窗口(private Frame f)的 windowClosing 事件没有和终止程序的语句挂钩。

其实万事俱备只欠东风,因为程序里的 Interfaceforme 类是个截取 windowClosing 事件后马上终止程序的窗口事件监听器,
所以在 go( ) 方法里实例化 Frame 之后的任何地方直接加一句 f.addWindowListener( this ); 即解决了问题。
 
 
 

本来就不能啊

import java.awt.*;
import java.awt.event.*;
public class Interfaceforme extends WindowAdapter{
private Frame f;
private Button b;

public static void main(String args[]){
Interfaceforme face=new Interfaceforme();
face.go();
}
public void go(){
f=new Frame("Two Listeners example");
f.add(new Label("click and drag the mouse"),"North");
b=new Button("hi");
f.add(b,"South");

f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(1);
}
}
);
f.setSize(300,200);
f.setVisible(true);
}
}
这样就能了,你的main方法中只有go()方法,但是go 方法中又没有提到关闭方法,所以你的窗口关不掉了.确切的说你的frame没有注册关闭,应该给frame添加windowslistener.