首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

Java的几种事件监听步骤

2012-11-08 
Java的几种事件监听方法这几天在看Java的图形界面编程,由于上课也没书,几乎没学什么,于是想自己做个堆积木

Java的几种事件监听方法

这几天在看Java的图形界面编程,由于上课也没书,几乎没学什么,于是想自己做个堆积木来好好学一下。写完之后,被监听弄得出了好几个问题,幸好学长帮我的忙才对Java的事件驱动模型有了个基本的了解,看来用Java做GUI真的不是件蛮爽的事。后来自己好好总结了一下,想清了下面四种常用的监听方法:

import javax.swing.*;import java.awt.event.*;//The first method//该方法有缺陷,如果本类要继承于另外一个类,那么由于java的单继承机制,该类就不能再继承于适配器类//使用该方法时需要注意适配器类中的方法名字要写正确,否则jdk会认为是新增的方法而不会报错public class JFrameDemo extends WindowAdapter {    JFrame jfr;    public JFrameDemo(String name)    {        jfr = new JFrame(name);        jfr.setSize(200,100);        jfr.setVisible(true);        jfr.addWindowListener(this);    }    public void windowClosing(WindowEvent e)    {        System.exit(0);    }    public static void main(String[] args)    {        new JFrameDemo("jframedemo");    }}//The second methodpublic class JFrameDemo extends JFrame{    public JFrameDemo(String name)    {        super(name);        setSize(200,100);        setVisible(true);        addWindowListener(new WindowOP());    }    public static void main(String[] args)    {        new JFrameDemo("jramedemo");    }}class WindowOP implements WindowListener{    public void windowClosed(WindowEvent e)            {}    public void windowActivated(WindowEvent e)        {}    public void windowClosing(WindowEvent e)     {                System.exit(0);    }    public void windowDeactivated(WindowEvent e)    {}    public void windowDeiconified(WindowEvent e)    {}    public void windowIconified(WindowEvent e)        {}    public void windowOpened(WindowEvent e)            {}}//The third method//通常而言,这种方法是较好的//将实现接口WindowListener的部份放在class JFrameDemo中,这样就只用一个类//这时在构造方法中添加监听的对象就是this本身了,也就是addWindowListener(this);public class JFrameDemo extends JFrame implements WindowListener{    public JFrameDemo(String name)    {        super(name);        setSize(200,100);        setVisible(true);        addWindowListener(this);    }    public void windowClosed(WindowEvent e)            {}    public void windowActivated(WindowEvent e)        {}    public void windowClosing(WindowEvent e)     {                System.exit(0);    }    public void windowDeactivated(WindowEvent e)    {}    public void windowDeiconified(WindowEvent e)    {}    public void windowIconified(WindowEvent e)        {}    public void windowOpened(WindowEvent e)            {}}//The forth method//采用内部类的方法来实现WindowListener接口,即addWindowListener(new WindowListener(){    public void windowCloing(WindowEvent e) {}    public void windowActivated(WindowEvent e) {}    public void windowClosing(WindowEvent e)     {                System.exit(0);    }    public void windowDeactivated(WindowEvent e) {}    public void windowDeiconified(WindowEvent e) {}    public void windowIconified(WindowEvent e)  {}    public void windowOpened(WindowEvent e)  {}});//但这种方法通常只能用于较小的程序中

?


<!-- -->import?javax.swing.*;
import?java.awt.event.*;


//The?first?method
//该方法有缺陷,如果本类要继承于另外一个类,那么由于java的单继承机制,该类就不能再继承于适配器类
//使用该方法时需要注意适配器类中的方法名字要写正确,否则jdk会认为是新增的方法而不会报错
public?class?JFrameDemo?extends?WindowAdapter?
{
????JFrame?jfr;
????public?JFrameDemo(String?name)
????{
????????jfr?=?new?JFrame(name);
????????jfr.setSize(200,100);
????????jfr.setVisible(true);
????????jfr.addWindowListener(this);
????}

????public?void?windowClosing(WindowEvent?e)
????{
????????System.exit(0);
????}

????public?static?void?main(String[]?args)
????{
????????new?JFrameDemo("jframedemo");
????}
}



//The?second?method
public?class?JFrameDemo?extends?JFrame
{
????public?JFrameDemo(String?name)
????{
????????super(name);
????????setSize(200,100);
????????setVisible(true);
????????addWindowListener(new?WindowOP());
????}

????public?static?void?main(String[]?args)
????{
????????new?JFrameDemo("jramedemo");
????}
}

class?WindowOP?implements?WindowListener
{
????public?void?windowClosed(WindowEvent?e)????????????{}
????public?void?windowActivated(WindowEvent?e)????????{}
????public?void?windowClosing(WindowEvent?e)?
????{????????
????????System.exit(0);
????}
????public?void?windowDeactivated(WindowEvent?e)????{}
????public?void?windowDeiconified(WindowEvent?e)????{}
????public?void?windowIconified(WindowEvent?e)????????{}
????public?void?windowOpened(WindowEvent?e)????????????{}

}




//The?third?method
//通常而言,这种方法是较好的
//将实现接口WindowListener的部份放在class?JFrameDemo中,这样就只用一个类
//这时在构造方法中添加监听的对象就是this本身了,也就是addWindowListener(this);
public?class?JFrameDemo?extends?JFrame?implements?WindowListener
{
????public?JFrameDemo(String?name)
????{
????????super(name);
????????setSize(200,100);
????????setVisible(true);
????????addWindowListener(this);
????}

????public?void?windowClosed(WindowEvent?e)????????????{}
????public?void?windowActivated(WindowEvent?e)????????{}
????public?void?windowClosing(WindowEvent?e)?
????{????????
????????System.exit(0);
????}
????public?void?windowDeactivated(WindowEvent?e)????{}
????public?void?windowDeiconified(WindowEvent?e)????{}
????public?void?windowIconified(WindowEvent?e)????????{}
????public?void?windowOpened(WindowEvent?e)????????????{}
}




//The?forth?method
//采用内部类的方法来实现WindowListener接口,即
addWindowListener(new?WindowListener()
{
????public?void?windowCloing(WindowEvent?e)?{}
????public?void?windowActivated(WindowEvent?e)?{}
????public?void?windowClosing(WindowEvent?e)?
????{????????
????????System.exit(0);
????}
????public?void?windowDeactivated(WindowEvent?e)?{}
????public?void?windowDeiconified(WindowEvent?e)?{}
????public?void?windowIconified(WindowEvent?e)??{}
????public?void?windowOpened(WindowEvent?e)??{}
});
//但这种方法通常只能用于较小的程序中

热点排行