码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA学习笔记(四十七)- 事件监听

时间:2015-04-04 09:10:25      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:事件   监听   

定义ActionListener接口的实现类实现事件监听

import java.awt.Button;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

/*
 * 事件监听
 */
public class Test04 {
    public static void main(String[] args) {
        Frame frame=new Frame("我的窗体");
        Button btn=new Button("点我");

        //为按钮绑定事件监听,即注册监听器
        //btn.addActionListener(new ButtonHandler());
        btn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //获取按钮上的文字
                String cmd=e.getActionCommand();
                System.out.println(cmd);
                //点击按钮弹出一个新的窗体
                Frame frm=new Frame("俺是被点击才弹出滴");
                frm.setSize(100,100);
                frm.setLocationRelativeTo(null);
                frm.setVisible(true);
            }
        });

        //为窗体绑定事件监听
        frame.addWindowListener(new WindowListener() {

            @Override
            public void windowOpened(WindowEvent e) {
                System.out.println("***windowOpened");
            }

            @Override
            public void windowIconified(WindowEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void windowDeiconified(WindowEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void windowDeactivated(WindowEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void windowClosing(WindowEvent e) {
                System.out.println("***windowClosing");
            }

            @Override
            public void windowClosed(WindowEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void windowActivated(WindowEvent e) {
                System.out.println("***windowActivated");

            }
        });

        frame.add(btn);
        frame.setSize(200,200);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

//定义ActionListener接口的实现类
class ButtonHandler implements ActionListener{
    //当发生ActionEvent事件时会自动产生事件对象,作为参数传入,同时会自动调用actinPerformed方法
    @Override
    public void actionPerformed(ActionEvent e) {
        //获取按钮上的文字
        String cmd=e.getActionCommand();
        System.out.println(cmd);
        //点击按钮弹出一个新的窗体
        Frame frm=new Frame("俺是被点击才弹出滴");
        frm.setSize(100,100);
        frm.setLocationRelativeTo(null);
        frm.setVisible(true);
    }
}

为一个组件绑定多个监听器

import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.TextEvent;
import java.awt.event.TextListener;

/*
 * 为一个组件绑定多个监听器
 */
public class Test05 {
    Frame frame = new Frame("我的窗体");
    Button btn = new Button("点我");
    TextField txt=new TextField(10);
    int count = 0;

    public Test05(){
        init();
    }

    public void init() {
        //为按钮绑定ActionEvent监听
        btn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("***actionPerformed");
                count++;
                btn.setLabel(count+"");
            }
        });

        //为按钮绑定MouseEvent监听
        btn.addMouseListener(new MouseListener() {

            @Override
            public void mouseReleased(MouseEvent e) {
                //System.out.println("****mouseReleased");

            }

            @Override
            public void mousePressed(MouseEvent e) {
                //System.out.println("****mousePressed");
            }

            @Override
            public void mouseExited(MouseEvent e) {
                //System.out.println("****mouseExited");
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                //System.out.println("****mouseEntered");
            }

            @Override
            public void mouseClicked(MouseEvent e) {
                System.out.println("****mouseClicked");
            }
        });

        //为文本框绑定监听器
        txt.addFocusListener(new FocusListener() {

            @Override
            public void focusLost(FocusEvent e) {
                System.out.println("***focusLost");
                TextField txtSource=(TextField) e.getSource();
                txtSource.setBackground(Color.white);

            }

            @Override
            public void focusGained(FocusEvent e) {
                System.out.println("***focusGained");
                TextField txtSource=(TextField) e.getSource(); //获取事件源
                txtSource.setBackground(Color.gray);

            }
        });

        txt.addTextListener(new TextListener() {

            @Override
            public void textValueChanged(TextEvent e) {
                System.out.println("文本框被更改"+e.paramString());
            }
        });

        txt.addKeyListener(new KeyListener() {

            @Override
            public void keyTyped(KeyEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void keyReleased(KeyEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void keyPressed(KeyEvent e) {
                System.out.println(e.getKeyCode()+" "+e.getKeyChar());
                if(e.getKeyCode()==KeyEvent.VK_ENTER){
                    System.out.println("哈哈,你按了回车,被发现了!");
                }
            }
        });


        frame.add("North",txt);
        frame.add(btn);
        frame.setSize(200,200);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

    }

    public static void main(String[] args) {
        new Test05();
    }
}

监听器实现方式

import java.awt.Button;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/*
 * 监听器实现方式
 */
public class Test06 extends Frame implements ActionListener{
    Button btn=new Button("点我");

    public Test06(String title){
        super(title);
        init();
    }

    public void init(){
        //方式一:匿名内部类
        /*btn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("使用匿名内部类方式");
            }
        });*/


        //方式二
        //btn.addActionListener(new MyHandler());

        //方式三
        //btn.addActionListener(this);

        //方式四
        btn.addActionListener(new MyHandler2());

        add(btn);
        setSize(200,200);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    //方式三:容器类
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("使用容器类方式");
    }


    //方式四:成员内部类
    class MyHandler2 implements ActionListener{
        public void actionPerformed(ActionEvent e) {
            System.out.println("使用外部类方式");
        }
    }

    public static void main(String[] args) {
        new Test06("窗体");
    }
}

//方式二:外部类
class MyHandler implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("使用外部类方式");
    }
}

JAVA学习笔记(四十七)- 事件监听

标签:事件   监听   

原文地址:http://blog.csdn.net/wangzi11322/article/details/44870735

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!