事件监听的三种方法

1.实现事件监听,总的来讲就是有三个步骤

 1)事件源注册事件监听器 --  事件源.addXXXListener( obj )  ,obj称为监听者
 2 ) 实现监听器接口   --obj所属的类实现XXXListener接口
 3)实现监听器接口中的抽象方法  --在obj所属的类中写监听器接口中的抽象方法 

2.具体来讲有三个方法:

1)监听注册,实现接口,实现接口的方法

public class MyFrame1 extends Frame implements ActionListener{
	private static final long serialVersionUID = 1L;
	private Label lb1;
	private Label lb2;
	private TextField tf1;
	private TextField tf2;
	private Button btn1;
	private Button btn2;
	public MyFrame1(){
		super("事件监听的三种方式:1");
		setBounds(200,50,170,200);
		setLayout( new FlowLayout() );
		lb1 = new Label("name");
		lb2 = new Label("psw");
		tf1 = new TextField(15);
		tf2 = new TextField(15);
		btn1 = new Button("OK");
		btn2 = new Button("exit");
		add(lb1);
		add(tf1);
		add(lb2);
		add(tf2);
		add(btn1);
		add(btn2);
		
		//监听
		btn1.addActionListener(this);
		btn2.addActionListener(this);
		setVisible(true);
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		//方法1
		/*
		if(e.getSource() == btn1){
			JOptionPane.showMessageDialog(this, "登录!");
		}else if(e.getSource() == btn2){
			System.exit(0);
		}
		*/
		
		//方法2
		if(e.getActionCommand().equals("OK")){
			JOptionPane.showMessageDialog(this, "登录!");
		}else if (e.getActionCommand().equals("exit")) {
			System.exit(0);
		}
	}
	public static void main(String[] args) {
		new MyFrame1();
	}
}

2)使用匿名内部类,方法类似

public class MyFrame2 extends Frame{
	private static final long serialVersionUID = 1L;
	private Label lb1;
	private Label lb2;
	private TextField tf1;
	private TextField tf2;
	private Button btn1;
	private Button btn2;
	public MyFrame2(){
		super("事件监听的三种方式:2");
		setBounds(200,50,170,200);
		setLayout( new FlowLayout() );
		lb1 = new Label("tf1");
		lb2 = new Label("tf2");
		tf1 = new TextField(15);
		tf2 = new TextField(15);
		btn1 = new Button("move");
		btn2 = new Button("exit");
		add(lb1);
		add(tf1);
		add(lb2);
		add(tf2);
		add(btn1);
		add(btn2);
		
		//监听
		btn1.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				String string = tf1.getText();
				tf2.setText(string);
				tf1.setText("");
			}
		});
		btn2.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		setVisible(true);
	}
	
	public static void main(String[] args) {
		new MyFrame2();
	}
}

3)使用内部类

public class MyFrame3 extends Frame{
	private static final long serialVersionUID = 1L;
	private Label lb1;
	private Label lb2;
	private TextField tf1;
	private TextField tf2;
	private Button btn1;
	private Button btn2;
	public MyFrame3(){
		super("事件监听的三种方式:3");
		setBounds(200,50,170,200);
		setLayout( new FlowLayout() );
		lb1 = new Label("tf1");
		lb2 = new Label("tf2");
		tf1 = new TextField(15);
		tf2 = new TextField(15);
		btn1 = new Button("move");
		btn2 = new Button("exit");
		add(lb1);
		add(tf1);
		add(lb2);
		add(tf2);
		add(btn1);
		add(btn2);
		
		//监听
		btn1.addActionListener(new MyAction());
		btn2.addActionListener(new MyAction());
		
		setVisible(true);
	}
	class MyAction implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			if(e.getSource() == btn1){
				String string = tf1.getText();
				tf2.setText(string);
				tf1.setText("");			
			}else if(e.getSource() == btn2){
				System.exit(0);
			}
		}
		
	}
	public static void main(String[] args) {
		new MyFrame3();
	}
}


  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值