JAVA學習筆記(四十七)- 事件監聽

來源:互聯網
上載者:User

標籤:事件   監聽   

定義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學習筆記(四十七)- 事件監聽

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.