java四种方法事件监听(匿名类,外部类,内部类,自身)
java事件监听器--java

产生事件 (ActionEvent) 用户操作 (单击按钮) 事件源 (Button) 注册 事件监听者 委托执行 事件监听程序 (执行ActionListener接口 actionPerformed方法)
JDK1.1的委托事件模型
为按钮注册事件监听程序
public void addActionListener(ActionListener handler)
单击按钮, 文本行中单击回车键, 双击列表框 选择菜单项时 文本行、文本区中修改内 容 选择复选框、选择框, 单击列表框, 选中带复选框的菜单项 鼠标 鼠标 单击鼠标时 鼠标进入 鼠标离开 按下鼠标时 放开鼠标时 按下键盘时 释放键盘时 获得焦点时 失去焦点时
textValueChanged(TextEvent e)
6.2.4 事件适配器
适配器类实现一个对应的所有接口,只是方法为空。 适配器类实现一个对应的所有接口,只是方法为空。
public abstract class WindowAdapter implements WindowListener { public void windowOpened(WindowEvent e) {} public void windowActivated(WindowEvent e) {} public void windowDeactivated(WindowEvent e) {} public void windowClosed(WindowEvent e) {} public void windowClosing(WindowEvent e) {} public void windowIconified(WindowEvent e) {} public void windowDeiconified(WindowEvent e) {} }
Java的监听器种类

Java的监听器种类网上整理!Listener是Servlet的监听器,它可以监听客户端的请求、服务端的操作等。
(通过监听器,可以自动激发一些操作,比如监听在线的用户的数量。
当增加一个HttpSession时,就激发 sessionCreated(HttpSessionEvent se)方法,这样就可以给在线人数加1。
)Servlet 监听器用于监听一些重要事件的发生,监听器对象在事情发生前、发生后可以做一些必要的处理。
目前 Servlet2.4 和 JSP2.0 常用的有7 个监听器接口,分为3 类:1. Servlet上下文进行监听(Application级):用于监听 ServletContext 对象的创建和删除以及属性的添加、删除和修改等操作,该监听器需要用到如下两个接口类:(1) ServletContextAttributeListener:监听对 ServletContext 属性的操作,比如增加、删除、修改attributeAdded(ServletContextAttributeEvene) 添加属性时调用attributeReplaced(ServletContextAttributeEvente) 修改属性时调用attributeRemoved(ServletContextAttributeEvente) 删除属性时调用(2) ServletContextListener:监听对 ServletContext 对象的创建和删除contextInitialized(ServletContextEventsce) 初始化时调用contextDestroyed(ServletContextEvent sce) 销毁时调用,即当服务器重新加载时调用2. 监听HTTP会话(Session级):用于监听 HTTP 会话活动情况和 HTTP 会话中的属性设置情况,也可以监听HTTP 会话的 active 和 passivate 情况等,该监听器需要用到如下多个接口类: (1) HttpSessionListener:监听 HttpSession 的操作sessionCreate(HttpSessionEvent se) 初始化时调用;sessionDestroyed(httpSessionEvent se) 销毁时调用,即当用户注销时调用Java代码1. package com.wl.listener.test;2.3.import javax.servlet.http.HttpSessionEvent;4.import javax.servlet.http.HttpSessionListener;5.6.public class HttpSessionListenerTest implements HttpSessionListener {7.8. public void sessionCreated(HttpSessionEvent arg0) {9.10. System.out.println("SSSSSSSSSSSSSSSSSS");11. }12.13. public void sessionDestroyed(HttpSessionEvent arg0) {14.15. System.out.println("EEEEEEEEEEEEEEEEEEE");16. }17.18.}在Web.xml的配置如下:Java代码1.<listener>2. <listener-class>com.wl.listener.test.HttpSessionListenerTest</listener-class>3. </listener>(2) HttpSessionActivationListener:用于监听 HTTP 会话的 active 和passivate 情况(3) HttpSessionAttributeListener:监听 HttpSession 中的属性操作attributeAdded(HttpSessionBindingEvent se) 添加属性时调用attributeRemoved(HttpSessionBindingEvent se) 删除属性时调用attributeReplaced(HttpSessionBindingEvent se) 修改属性时调用3. 对客户端请求进行监听(Requst级):用于对客户端的请求进行监听是在 Servlet2.4 规范中新添加的一项新技术,使用的接口如下:(1) ServletRequestListener 接口类requestDestroyed(ServletRequestEvent e) 对销毁客户端进行监听,即当执行 request.removeAttribute("xxx") 时调用requestInitialized(ServletRequestEvent e) 对实现客户端的请求进行监听Java代码1.package com.wl.listener.test;2.3.import javax.servlet.ServletRequestEvent;4.import javax.servlet.ServletRequestListener;5.6.public class ServletRequestListenerTest implements ServletRequestListener {7.8. public void requestDestroyed(ServletRequestEvent arg0) {9.10. System.out.println("ServletRequestListenerTest is destroyed .......");11. }12.13. public void requestInitialized(ServletRequestEvent arg0) {14.15. System.out.println("ServletRequestListenerTest is start.......");16. }17.18.}在Web.xml中添加如下配置:Java代码1. <listener>2. <listener-class>com.wl.listener.test.ServletRequestListenerTest</listener-class>3.</listener>(2) ServletRequestAttributeListener 接口类attributeAdded(ServletRequestAttributeEvent e) 对属性添加进行监听attributeRemoved(ServletRequestAttributeEvent e) 对属性删除进行监听attributeReplaced(ServletRequestAttributeEvent e) 对属性替换进行监听。
Java添加事件监听的四种方法代码实例

Java添加事件监听的四种⽅法代码实例Java添加事件的⼏种⽅式(转载了codebrother的⽂章,做了稍微的改动):/*** Java事件监听处理——⾃⾝类实现ActionListener接⼝,作为事件监听器** @author codebrother*/class EventListener1 extends JFrame implements ActionListener {private JButton btBlue, btDialog;public EventListener1() {setTitle("Java GUI 事件监听处理");setBounds(100, 100, 500, 350);setLayout(new FlowLayout());btBlue = new JButton("蓝⾊");btDialog = new JButton("弹窗");// 将按钮添加事件监听器btBlue.addActionListener(this);btDialog.addActionListener(this);add(btBlue);add(btDialog);setVisible(true);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}// ***************************事件处理***************************@Overridepublic void actionPerformed(ActionEvent e) {if (e.getSource() == btBlue) {Container c = getContentPane();c.setBackground(Color.BLUE);}else if (e.getSource() == btDialog) {JDialog dialog = new JDialog();dialog.setBounds(300, 200, 400, 300);dialog.setVisible(true);}}}/*** Java事件监听处理——内部类处理** @author codebrother*/class EventListener3 extends JFrame {private JButton btBlue, btDialog;// 构造⽅法public EventListener3() {setTitle("Java GUI 事件监听处理");setBounds(100, 100, 500, 350);setLayout(new FlowLayout());btBlue = new JButton("蓝⾊");btDialog = new JButton("弹窗");// 添加事件监听器对象(⾯向对象思想)btBlue.addActionListener(new ColorEventListener());btDialog.addActionListener(new DialogEventListener());add(btBlue);add(btDialog);setVisible(true);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}// 内部类ColorEventListener,实现ActionListener接⼝class ColorEventListener implements ActionListener {@Overridepublic void actionPerformed(ActionEvent e) {Container c = getContentPane();c.setBackground(Color.BLUE);}}// 内部类DialogEventListener,实现ActionListener接⼝ class DialogEventListener implements ActionListener { @Overridepublic void actionPerformed(ActionEvent e) {JDialog dialog = new JDialog();dialog.setBounds(300, 200, 400, 300);dialog.setVisible(true);}}}/*** Java事件监听处理——匿名内部类处理** @author codebrother*/class EventListener2 extends JFrame {private JButton btBlue, btDialog;public EventListener2() {setTitle("Java GUI 事件监听处理");setBounds(100, 100, 500, 350);setLayout(new FlowLayout());btBlue = new JButton("蓝⾊");btDialog = new JButton("弹窗");// 添加事件监听器(此处即为匿名类)btBlue.addActionListener(new ActionListener() {// 事件处理@Overridepublic void actionPerformed(ActionEvent e) {Container c = getContentPane();c.setBackground(Color.BLUE);}});// 并添加事件监听器btDialog.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {JDialog dialog = new JDialog();dialog.setBounds(300, 200, 400, 300);dialog.setVisible(true);}});add(btBlue);add(btDialog);setVisible(true);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }}/*** Java事件监听处理——外部类处理** @author codebrother*/class EventListener4 extends JFrame {private JButton btBlue, btDialog;public EventListener4() {setTitle("Java GUI 事件监听处理");setBounds(100, 100, 500, 350);setLayout(new FlowLayout());btBlue = new JButton("蓝⾊");btDialog = new JButton("弹窗");// 将按钮添加事件监听器btBlue.addActionListener(new ColorEventListener(this)); btDialog.addActionListener(new DialogEventListener());add(btBlue);add(btDialog);setVisible(true);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }}// 外部类ColorEventListener,实现ActionListener接⼝class ColorEventListener implements ActionListener {private EventListener4 el;ColorEventListener(EventListener4 el) {this.el = el;}@Overridepublic void actionPerformed(ActionEvent e) {Container c = el.getContentPane();c.setBackground(Color.BLUE);}}// 外部类DialogEventListener,实现ActionListener接⼝class DialogEventListener implements ActionListener { @Overridepublic void actionPerformed(ActionEvent e) {JDialog dialog = new JDialog();dialog.setBounds(300, 200, 400, 300);dialog.setVisible(true);}}public class ActionListenerTest{public static void main(String args[]){new EventListener2();}}。
Android实现监听的四种方法详解实例代码

Android实现监听的四种⽅法详解实例代码直接上代码,⼤家可以参考下(1)⾃⾝类作为事件监听器package .gdmec.s0*******.work5;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.Toast;public class MainActivity extends AppCompatActivity implements View.OnClickListener{@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(yout.activity_main);Button btn1 = (Button) findViewById(R.id.btn1);btn1.setText("单击");btn1.setOnClickListener(this);}@Overridepublic void onClick(View v) {Toast.makeText(this,"已单击我...",Toast.LENGTH_SHORT).show();System.out.println("已单击我...");}}(2)外部类作为事件监听器:package .gdmec.s0*******.work5;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;public class Act2 extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(yout.activity_act2);Button btn2 = (Button) findViewById(R.id.btn2);btn2.setText("单击");btn2.setOnClickListener(new OuterClass("已单击我..."));}}class OuterClass implements View.OnClickListener{private String str="已单击我....";public OuterClass(String str){super();this.str=str;}@Overridepublic void onClick(View v) {System.out.println(str);}}(3)内部类作为事件监听器:package .gdmec.s0*******.work5;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.Toast;public class Act3 extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(yout.activity_act3);Button btn3 = (Button) findViewById(R.id.btn3);btn3.setText("单击");btn3.setOnClickListener(new OuterClass());}class OuterClass implements View.OnClickListener{@Overridepublic void onClick(View v) {Toast.makeText(Act3.this,"已单击我...",Toast.LENGTH_SHORT).show();System.out.println("已单击我...");}}}(4)匿名类作为事件监听器:package .gdmec.s0*******.work5;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.Toast;public class Act4 extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(yout.activity_act4);Button btn4 = (Button) findViewById(R.id.btn4);btn4.setText("单击");btn4.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Toast.makeText(Act4.this,"已单击我...",Toast.LENGTH_SHORT).show();System.out.println("已单击我...");}});}}以上四种⽅法之后,在xml⽂件中给每⼀个事件即activity⾥⾯定义多⼀个android label即可。
使用Java Swing组件进行事件处理方法的分析与比较

使用Java Swing组件进行事件处理方法的分析与比较摘要:Swing组件是Java基本类库的一部分,是用于开发Java应用程序用户界面的开发工具包。
通过具体实例分析了多种使用Swing 组件进行事件处理的方法,并对这些方法进行了比较。
关键词:Java;Swing;监听;内部类;事件0引言Swing组件是Java语言中用来编写图形化界面的工具包,它使用灵活、功能强大,使得开发人员利用其提供的各种类与方法能够快速地创建出符合用户需求的各种美观、实用的图形界面。
Swing组件具有自动对各种用户的动作产生响应的功能,例如当某一用户点击button按钮或选择菜单栏时,该动作将会被监听处理。
在代码编写的过程中注册监听器是必须完成的工作,通过它对事件源进行监听,这样在程序中就可以十分方便地对用户行为进行响应处理。
1使用Swing组件进行事件处理过程使用Java Swing组件进行事件处理的过程分为如下几个步骤:首先,创建一个新的Swing组件(例如按钮或者菜单栏等);其次,将刚刚创建的组件添加到定义的面板中去;然后,通过注册监听器对事件源进行监听(例如对用户点击按钮或者点击鼠标的动作);最后,在程序中编写响应事件源产生事件的各种方法。
上述几个步骤的实现可以通过多种方法来完成。
第一种可使用的方法是通过注册单个监听器并使用多个if条件判断语句来决定响应的事件源;第二种方法使用Java语言中的匿名内部类来进行相关的事件处理过程;第三种方法是通过一般内部类来进行事件的响应。
第二种、第三种方法从本质上来说,都是在进行事件处理的过程中使用了多个Java的内部类,而不同之处在于具体实现有较大变化。
下面通过一个简单的Java 应用程序(创建两个按钮,当用户点击按钮后产生不同的显示结果)展示怎样通过以上3种方法来实现Swing组件的事件处理过程。
1.1单个监听器方式该方法使用单个监听器来进行实现。
具体完成该方法的过程为:建立一个名为Example的类,当用户点击button按钮后,监听器ExampleListenner会对该动作进行监听并交由actionPerformed()方法来进行相应的事件处理。
Java事件处理机制-+事件监听器的四种实现方式+

Java事件处理机制- 事件监听器的四种实现方式⏹自身类作为事件监听器⏹外部类作为事件监听器⏹匿名内部类作为事件监听器⏹内部类作为事件监听器自身类作为事件监听器:Java代码import javax.swing.*;import java.awt.*;import java.awt.event.*;class ThisClassEvent extends JFrame implements ActionListener{JButton btn;public ThisClassEvent(){super("Java事件监听机制");setLayout(new FlowLayout());setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);btn=new JButton("点击");btn.addActionListener(this);getContentPane().add(btn);setBounds(200,200,300,160);setVisible(true);}public void actionPerformed (ActionEvent e){Container c=getContentPane();c.setBackground(Color.red);}public static void main(String args[]){new ThisClassEvent();}}外部类作为事件监听器:Java代码import java.awt.*;import java.awt.event.*;import javax.swing.*;class OuterClassEvent extends JFrame{JButton btn;public OuterClassEvent(){super("Java事件监听机制");setLayout(new FlowLayout());setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);btn=new JButton("点击");btn.addActionListener(new OuterClass(this));getContentPane().add(btn);setBounds(200,200,300,160);setVisible(true);}public static void main(String args[]){new OuterClassEvent();}}class OuterClass implements ActionListener{OuterClassEvent oce;public OuterClass(OuterClassEvent oce){this.oce = oce;}public void actionPerformed(ActionEvent e){Container c=oce.getContentPane();c.setBackground(Color.red);}}匿名内部类作为事件监听器:Java代码import java.awt.*;import java.awt.event.*;import javax.swing.*;class AnonymousEvent extends JFrame{JButton btn;public AnonymousEvent(){super("Java事件监听机制");setLayout(new FlowLayout());setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);btn=new JButton("点击");btn.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){Container c=getContentPane();c.setBackground(Color.red);}});getContentPane().add(btn);setBounds(200,200,300,160);setVisible(true);}public static void main(String args[]){new AnonymousEvent();}}内部类作为事件监听器:Java代码import java.awt.*;import java.awt.event.*;import javax.swing.*;class InnerClassEvent extends JFrame{JButton btn;public InnerClassEvent(){super("Java事件监听机制");setLayout(new FlowLayout());setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);btn=new JButton("点击");btn.addActionListener(new InnerClass());getContentPane().add(btn);setBounds(200,200,300,160);setVisible(true);}class InnerClass implements ActionListener{public void actionPerformed (ActionEvent e){Container c=getContentPane();c.setBackground(Color.red);}}public static void main(String args[]){new InnerClassEvent();}}。
java监听机制模拟(JButton按钮监听机制)

java监听机制模拟(JButton按钮监听机制)⼀、概念1.事件监听器事件监听器就我个⼈的理解就是:被外部事件(键盘、⿏标)引发的程序,这段程序是被嵌⼊到事件源的类⾥⾯,它负责监听事件列表。
⽽很多⼈把事件监听器理解成是实现EventListener接⼝的类。
⽽我的理解是实现EventListener接⼝的类是事件处理器。
⾥边有处理事件的⽅法。
从逻辑上看是这样的,但是⼈家既然这样来命名了,那也没有办法。
因为程序员只要知道这么去添加监听器就⾏了,不必理解内部的处理流程,但是作为⼀个热爱计算机的程序员来说,必须要理解其过程。
事件监听器的功能:负责监听事件注册类表,有相关事件注册就⽴马 new ⼀个事件,然后调⽤事件处理器⾥的事件处理⽅法,完成事件处理。
然后移除事件注册列表的相关事件。
2.事件源:事件源是事件的起源,可以称作事件触发源。
其主要的功能是,介绍外边事件,⽐如键盘、⿏标等,当有事件时就会触发事件监听器。
主成分:主要由事件监听器、注册事件⽅法(如:addActionListener)构成。
3.事件对象:实现EventObject接⼝的类。
⾥⾯封装了对事件源进⾏操作的⽅法。
⽐如:getActionCommand()⽅法。
4.事件处理器事件处理器是对事件进⾏处理的类,这类实现EventListener接⼝。
此类由程序员编写。
⽐如事件处理器中的处理程序:⼆、模拟程序以JButton按钮为列,看⼀下程序:import java.util.EventListener;import java.util.EventObject;import java.util.Vector;//⾃定义⼀个按钮事件class ActionEvent extends EventObject{String actionCommand;public ActionEvent(Object source,String command){super(source);this.actionCommand = command;System.out.println("按钮点击事件产⽣!");}public String getActionCommand() {return actionCommand;}}// 弄⼀个在按钮⾥⾯使⽤的接⼝,通过继承此接⼝可以完成事件处理的类interface ActionListener extends EventListener{//这⾥是当事件发⽣后的响应过程public void actionPerformed(ActionEvent me);}//再⾃定义⼀个监听器class MyListener implements ActionListener{//这⾥是当事件发⽣后的响应过程public void actionPerformed(ActionEvent me){{String str=me.getActionCommand();if(str.equals("hello"))System.out.println("按钮点击事件被处理");}}//以下这个类为触发事件的事件源class JButton { //⽐如button按钮String s;JButton(String s){this.s=s;}String getAt(){return s;}private Vector vectorListeners=new Vector(); //定义⼀个向量public synchronized void addActionListener(ActionListener actionlistener) //注册监听器{vectorListeners.addElement(actionlistener); // 将指定元素添加到此向量的末尾System.out.println("按钮上注册监听器");}public synchronized void removeActionListener(ActionListener actionlistener) //移除监听器{vectorListeners.removeElement(actionlistener); //从此向量中移除变量的第⼀个(索引最⼩的)匹配项。
Java设计模式——观察者模式(事件监听)

Java设计模式——观察者模式(事件监听)最近在看Tomcat和Spring的源码,在启动的时候注册了各种Listener,事件触发的时候就执⾏,这⾥就⽤到了设计模式中的观察者模式。
引-GUI中的事件监听想想以前在学Java的GUI编程的时候,就⽤到了事件的注册监听,然后写了⼀个⼩程序试验⼀下:点击按钮触发相应的事件public class ButtonTest extends JFrame {ButtonTest() {JPanel panel = new JPanel();JButton button1 = new JButton("按钮⼀");JButton button2 = new JButton("按钮⼆");panel.add(button1);panel.add(button2);this.getContentPane().add(panel);this.setVisible(true);button1.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {System.out.println("你按了按钮⼀");}});button2.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {System.out.println("你按了按钮⼆");}});}public static void main(String args[]) {new ButtonTest();}}嗯,写起来确实很简单,这是封装好的,我们只需要注册事件,并实现对应的事件响应就⾏了。
那么这种神奇的模式是怎么实现的呢?以下为我的分析过程。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
JAVA四种方法实现事件监听1.外部类实现事件监听:import java.awt.*;import java.awt.event.*;import javax.swing.*;publicclass Listener1extends JFrame {JButton button1,button2;JPanel pane1,pane2,p1,p2;CardLayout card1=new CardLayout();/*CardLayout布局方式将容器中的每个组件看作一张卡片。
一次只能看到一张卡片,容器则充当卡片的堆栈*/Listener1(){this.setTitle("外部类实现事件监听");this.setBounds(200,200,300,200);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setVisible(true);init();}publicvoid init(){p1=new JPanel();p1.add(new JLabel("第一个面板"));p1.setBackground(Color.red);p2=new JPanel();p2.add(new JLabel("第二个面板"));p2.setBackground(Color.green);pane1=new JPanel(card1);pane1.add("红色", p1);pane1.add("绿色", p2);button1=new JButton("红色");button2=new JButton("绿色");pane2=new JPanel();pane2.add(button1);pane2.add(button2);this.add(pane1,BorderLayout.CENTER);this.add(pane2,BorderLayout.SOUTH);button1.addActionListener(new ColorEvent(card1,pane1));button2.addActionListener(new ColorEvent(card1,pane1)); }publicstaticvoid main(String[] args) {// TODO Auto-generated method stubnew Listener1();}}class ColorEvent implements ActionListener{CardLayout card1=new CardLayout();JPanel pane1=new JPanel();ColorEvent(CardLayout card1,JPanel pane1){this.card1=card1;this.pane1=pane1;}@Overridepublicvoid actionPerformed(ActionEvent e) {// TODO Auto-generated method stubif(e.getActionCommand().equals("红色")){//getActionCommand()返回与此动作相关的命令字符串card1.show(pane1, "红色");}elseif(e.getActionCommand().equals("绿色")){card1.show(pane1, "绿色");}}}2.内部类实现事件监听:import java.awt.*;import java.awt.event.*;import javax.swing.*;publicclass Listener2extends JFrame {JButton button1,button2;JPanel pane1,pane2,p1,p2;CardLayout card1=new CardLayout();/*CardLayout布局方式将容器中的每个组件看作一张卡片。
一次只能看到一张卡片,容器则充当卡片的堆栈*/Listener2(){this.setTitle("内部类实现事件监听");this.setBounds(200,200,300,200);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setVisible(true);init();}publicvoid init(){p1=new JPanel();p1.add(new JLabel("第一个面板"));p1.setBackground(Color.red);p2=new JPanel();p2.add(new JLabel("第二个面板"));p2.setBackground(Color.green);pane1=new JPanel(card1);pane1.add("红色", p1);pane1.add("绿色", p2);button1=new JButton("红色");button2=new JButton("绿色");pane2=new JPanel();pane2.add(button1);pane2.add(button2);this.add(pane1,BorderLayout.CENTER);this.add(pane2,BorderLayout.SOUTH);button1.addActionListener(new ColorEvent());button2.addActionListener(new ColorEvent()); }class ColorEvent implements ActionListener{@Overridepublicvoid actionPerformed(ActionEvent e) { // TODO Auto-generated method stubif(e.getSource()==button1){//返回发生事件的对象。
card1.show(pane1, "红色");}elseif(e.getSource()==button2){card1.show(pane1, "绿色");}}}publicstaticvoid main(String[] args) {// TODO Auto-generated method stubnew Listener2();}}3.自身类实现事件监听:import java.awt.*;import java.awt.event.*;import javax.swing.*;publicclass Listener3extends JFrame implements ActionListener { JButton button1,button2;JPanel pane1,pane2,p1,p2;CardLayout card1=new CardLayout();/*CardLayout布局方式将容器中的每个组件看作一张卡片。
一次只能看到一张卡片,容器则充当卡片的堆栈*/Listener3(){this.setTitle("自身类实现事件监听");this.setBounds(200,200,300,200);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setVisible(true);init();}publicvoid init(){p1=new JPanel();p1.add(new JLabel("第一个面板"));p1.setBackground(Color.red);p2=new JPanel();p2.add(new JLabel("第二个面板"));p2.setBackground(Color.green);pane1=new JPanel(card1);pane1.add("红色", p1);pane1.add("绿色", p2);button1=new JButton("红色");button2=new JButton("绿色");pane2=new JPanel();pane2.add(button1);pane2.add(button2);this.add(pane1,BorderLayout.CENTER);this.add(pane2,BorderLayout.SOUTH);button1.addActionListener(this);button2.addActionListener(this);}@Overridepublicvoid actionPerformed(ActionEvent e) {// TODO Auto-generated method stubif(e.getActionCommand().equals("红色")){//getActionCommand()返回与此动作相关的命令字符串card1.show(pane1, "红色");}elseif(e.getActionCommand().equals("绿色")){card1.show(pane1, "绿色");}}publicstaticvoid main(String[] args) {// TODO Auto-generated method stubnew Listener3();}}4.匿名类实现事件监听:import java.awt.*;import java.awt.event.*;import javax.swing.*;publicclass Listener4extends JFrame{JButton button1,button2;JPanel pane1,pane2,p1,p2;CardLayout card1=new CardLayout();/*CardLayout布局方式将容器中的每个组件看作一张卡片。