import java.awt.*;
import java.awt.event.*;
public class MyAdapterDemo extends Frame{
private final Label label;
private final Dialog dialog;
public MyAdapterDemo(){
super();
dialog = new Dialog(this,"hello",true);
label = new Label();
dialog.add(label);
dialog.setSize(100,50);
requestFocus();
WindowAdapter windowAdapter = new WindowAdapter(){
public void windowClosing(WindowEvent e){
if(e.getSource().equals(dialog))
dialog.setVisible(false);
else if(e.getSource().equals(this))
{System.exit(0);}
}
};
dialog.addWindowListener(windowAdapter);
// dialog.addWindowListener(new WindowAdapter(){
// public void windowClosing(WindowEvent e){
//// System.exit(0);
// if(e.getSource().equals(dialog))
// System.out.println(e.getSource().toString());
// dialog.setVisible(false);
// }
// });
addKeyListener(new KeyAdapter(){
public void keyTyped(KeyEvent e){
label.setText("您键入了"+e.getKeyChar());
dialog.setVisible(true);
}
});
addWindowListener(windowAdapter);
// addWindowListener(new WindowAdapter(){
// public void windowClosing(WindowEvent e){
// System.exit(0);
//// dialog.setVisible(false);
// }
// });
// add(dialog,BorderLayout.CENTER);
setSize(200,200);
setVisible(true);
}
public static void main(String[] args){
new MyAdapterDemo();
}
}
为什么当点击关闭的时候Dialog能够关闭,而主窗口没有反应呢
------解决方案--------------------
此行
else if(e.getSource().equals(this))
修改为
else if (e.getSource().equals(Test.this)) {
这样就O了,希望可以帮到你