您的位置:首页 > 编程语言 > Java开发

java事件响应方法汇总(容器类监听、监听器类、AbstractAction、反射)

2014-08-19 09:50 477 查看
Java图形用户界面中,处理事件时所必须的步骤是:

1、创建接受响应的组件(控件)
2、实现相关事件监听接口
3、注册事件源的动作监听器
4、事件触发时的事件处理

相应的可以通过以下的集中方式来作出事件响应。

[java] view plaincopyprint?

<span style="font-size: 18px;">一、容器类监听

效果:单击窗体中的三个按钮,实现相应的相应时间。

</span><pre class="java" name="code">import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

//声明 类时,添加“implements ActionListener”实现监听的接口,如果要对多种监听方式进行监听,则用逗号间隔开

// 比如“implements ActionListener,KeyListener”

class ButtonListener extends JFrame implements ActionListener{

JButton ok, cancel,exit; //创建接受响应的组建,就是三个按钮

public ButtonListener(String title){

super(title);

this.setLayout(new FlowLayout());

ok = new JButton("确定");

cancel = new JButton("返回");

exit = new JButton("退出");

//下面三个语句 为按钮分别 注册监听器

ok.addActionListener(this);

cancel.addActionListener(this);

exit.addActionListener(this);

getContentPane().add(ok);

getContentPane().add(cancel);

getContentPane().add(exit);

}

//完成 事件触发时的事件处理

public void actionPerformed(ActionEvent e){

if(e.getSource()==ok)

System.out.println("确定");

if(e.getSource()==cancel)

System.out.println("返回");

if(e.getSource()==exit)

System.exit(0);;

}

public static void main(String args[]) {

ButtonListener pd=new ButtonListener("ActionEvent Demo");

pd.setSize(250,100);

pd.setVisible(true);

}

}

</pre><br>

<br>

<pre></pre>

<p><span style="font-size: 18px;">二、监听类实现</span><br>

<br>

<pre style="margin: 4px 0px; font-size: 18px; background-color: rgb(240, 240, 240);" class="java" name="code"><span style="font-size: 18px;">效果:单击窗体中的三个按钮,实现相应的相应时间。

</span></pre><p></p>

<div><span style="font-size: 18px;"><br>

</span></div>

<pre style="font-size: 18px;" class="html" name="code">import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

class ButtonListener1 extends JFrame { //这里没有实现监听

JButton ok, cancel,exit;

public ButtonListener1(String title){

super(title);

this.setLayout(new FlowLayout());

ok = new JButton("确定");

cancel = new JButton("返回");

exit = new JButton("退出");

ok.addActionListener(new MyListener());

cancel.addActionListener(new MyListener());;

exit.addActionListener(new MyListener());;

getContentPane().add(ok);

getContentPane().add(cancel);

getContentPane().add(exit);

}

public static void main(String args[]) {

ButtonListener pd=new ButtonListener("ActionEvent Demo");

pd.setSize(250,100);

pd.setVisible(true);

}

}

//监听动作事件

class MyListener implements ActionListener{

public void actionPerformed(ActionEvent e){

if(e.getActionCommand()=="确定")

System.out.println("确定");

if(e.getActionCommand()=="返回")

System.out.println("返回");

if(e.getActionCommand()=="退出")

System.exit(0);;

}

} </pre><br>

<span style="font-size: 18px;">三、使用 AbstractAction类实现监听</span><br>

<span style="font-size: 18px;"> 这个方法,我也没搞清,照着别人的例子做出来的<br>

效果:单击菜单,作出响应</span><br>

<pre style="font-size: 18px;" class="java" name="code">import java.awt.BorderLayout;

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;

import javax.swing.Action;

import javax.swing.JFrame;

import javax.swing.JMenu;

import javax.swing.JMenuBar;

import javax.swing.JMenuItem;

import javax.swing.JOptionPane;

//此类继承AbstractAction,必须实现actionPerformed()方法。

class AbstractEvent extends AbstractAction{

//private static final long serialVersionUID = 1L;

AbstractEvent(){

}

public void actionPerformed(ActionEvent e){

//弹出确认对话框

if (e.getActionCommand()=="open"){

JOptionPane.showMessageDialog(null, "打开");

}else if (e.getActionCommand()=="close"){

JOptionPane.showMessageDialog(null, "关闭");

}else if (e.getActionCommand()=="run"){

JOptionPane.showMessageDialog(null, "运行");

}else if (e.getActionCommand()=="stop"){

JOptionPane.showMessageDialog(null, "停止");

}

}

}

public class TestAbstractEvent {

private static JMenuBar menubar;

private static JFrame frame;

//指定MenuEvent的具体处理程序是AbstractEvent类完成的。

final Action MenuEvent=new AbstractEvent();

public TestAbstractEvent(){

frame=new JFrame("menu");

frame.getContentPane().setLayout(new BorderLayout());

menubar=new JMenuBar();

JMenu menuFile=new JMenu("file");

//实例化一个菜单项,并添加监听openAction,

JMenuItem menuItemopen=new JMenuItem("open");

menuItemopen.addActionListener(MenuEvent);

JMenuItem menuItemclose=new JMenuItem("close");

menuItemclose.addActionListener(MenuEvent);

menuFile.add(menuItemopen);

menuFile.add(menuItemclose);

JMenu menuTool=new JMenu("tool");

JMenuItem menuItemrun=new JMenuItem("run");

menuItemrun.addActionListener(MenuEvent);

JMenuItem menuItemstop=new JMenuItem("stop");

menuItemstop.addActionListener(MenuEvent);

menuTool.add(menuItemrun);

menuTool.add(menuItemstop);

menubar.add(menuFile);

menubar.add(menuTool);

menubar.setVisible(true);

frame.add(menubar,BorderLayout.NORTH);

frame.setSize(400,200);

frame.setVisible(true);

}

public static void main(String[] args){

new TestAbstractEvent();

}

}</pre><br>

<span style="font-size: 18px;">四、</span><span style="font-size: 18px;"> AbstractAction类 + 反射 的方法<br>

<span style="font-size: 18px;">这个方法,我也没搞清,照着别人的例子做出来的!<br>

</span><br>

效果:单击工具栏的三个按钮,通过按钮的名称,反射得到 与按钮名称相同的类实现响应。<br>

<br>

</span><pre class="java" name="code">import java.awt.BorderLayout;

import java.awt.event.ActionEvent;

import javax.swing.*;

class ViewAction extends AbstractAction{

private String ActionName="";

//private JFrame frame=null;

private Action action=null;

public ViewAction(){

}

public ViewAction(String ActionName){

this.ActionName=ActionName;

//this.frame=frame;

}

@Override

public void actionPerformed(ActionEvent e) {

Action action=getAction(this.ActionName);

action.execute();

}

private Action getAction(String ActionName){

try{

if (this.action==null){

Action action=(Action)Class.forName(ActionName).newInstance();

this.action=action;

}

return this.action;

}catch(Exception e){

return null;

}

}

}

public class TestAE extends JFrame {

public JToolBar bar=new JToolBar();

String buttonName[]={"b1","b2","b3"};

public TestAE(){

super("事件");

for (int i=0;i<buttonName.length;i++){

ViewAction action=new ViewAction(buttonName[i]);

JButton button=new JButton(buttonName[i]);

button.addActionListener(action);

bar.add(button);

}

this.getContentPane().add(bar,BorderLayout.NORTH);

this.setSize(300, 200);

this.setLocationRelativeTo(null);

this.setVisible(true);

}

public static void main(String [] args){

new TestAE();

}

}

interface Action{

void execute();

}

class b1 implements Action{

public void execute(){

JOptionPane.showMessageDialog(null, "单击了 b1");

}

}

class b2 implements Action{

public void execute(){

JOptionPane.showMessageDialog(null, "单击了 b2");

}

}

class b3 implements Action{

public void execute(){

JOptionPane.showMessageDialog(null, "单击了 b3");

}

}</pre><br>

<br>

<p></p>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: