注册 登录
编程论坛 J2EE论坛

关闭窗口问题

fighter19 发布于 2008-11-21 13:11, 853 次点击
下面的代码,本来没有实现关闭窗口的的,但我想实现,为什么加了addWindowListener(new WindowAdapter()的关闭窗口代码进去还是无法关闭的,请大家指教一下小弟,感激不尽~


/*使用内部类处理鼠标移动事件和鼠标事件*/

import java.awt.*;
import java.awt.event.*;
import java.util.*;
 public class TowListenIner extends Frame implements ActionListener{
    private Frame f;
    private TextField tf;
    public TowListenIner(){
        addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e){
                dispose();
                System.exit(0);
            }
        });
    }
    public void actionPerformed(ActionEvent e){};
    public static void main(String[] args){
        TowListenIner that = new TowListenIner();
        that.go();
    }
    public void go(){
        f= new Frame("两个监听者的例子");
        f.add("North",new Label("Click and drag the mouse"));
        tf = new TextField(30);
        f.add("South",tf);
        f.addMouseMotionListener(new MouseMotionHandler());
        f.addMouseListener(new MouseEventHandler());
        f.setSize(300,300);
        f.setVisible(true);
    }
    public class MouseMotionHandler extends MouseMotionAdapter{
        public void mouseDragged(MouseEvent e){
            String s = "鼠标的拖动位置:X="+e.getX()+"Y="+e.getY();
            tf.setText(s);
        }
    }
    public class MouseEventHandler extends MouseAdapter{
        public void mouseEntered(MouseEvent e){
            String s = "鼠标进来";
            tf.setText(s);
        }
        public void mouseExited(MouseEvent e){
            String s= "鼠标离开";
            tf.setText(s);
        }
    }
    
    
}
1 回复
#2
majinfei2008-11-21 15:34
参考一下
在go()
添加窗口关闭函数.
 f.addWindowListener(new WindowAdapter(){
                public void windowClosing(WindowEvent e){
                    dispose();
                    System.exit(0);
                }
            });
对于你的编程方法来说,在构造方法中添加方法,只会给 new 后的对象加这个方法.

如果你的Frame是用构造方法new 出来的,则可以响应.
1