比較Java Swing中三種注冊(cè)事件的方法
Swing 是目前Java中不可缺少的窗口工具組,是建立圖形化用戶界面(GUI)程序的強(qiáng)大工具。Java Swing組件自動(dòng)產(chǎn)生各種事件來響應(yīng)用戶行為。Java將事件封裝成事件類,并且為每個(gè)事件類定義了一個(gè)事件監(jiān)聽器。一個(gè)組件注冊(cè)事件監(jiān)聽器方法,表明該組件要響應(yīng)指定事件。也就是說我們可以通過注冊(cè)監(jiān)聽器,監(jiān)聽事件源產(chǎn)生的事件,從而在事件處理程序中處理我們所需要處理的用戶行為。
Java Swing中處理各組件事件的一般步驟是:
1. 新建一個(gè)組件。
2. 將該組件添加到相應(yīng)的面板。
3. 注冊(cè)監(jiān)聽器以監(jiān)聽事件源產(chǎn)生的事件
4. 定義處理事件的方法。
注冊(cè)事件我們一般采用兩種方式:一是:利用一個(gè)監(jiān)聽器以及多個(gè)if語句來決定是哪個(gè)組件產(chǎn)生的事件;二是使用多個(gè)內(nèi)部類來響應(yīng)不同組件產(chǎn)生的各種事件,它又分兩種方式,一種是采用匿名內(nèi)部類,一種是采用一般內(nèi)部類。
下面我們采用以上三種方式來注冊(cè)事件。來說明以上三種方式是如何實(shí)現(xiàn)事件的處理方法。
一、采用一個(gè)監(jiān)聽器多個(gè)if語句來實(shí)現(xiàn)
在這種方式下:我們要繼承ActionListener接口,并且要實(shí)現(xiàn)actionPerformed方法。通過getActionCommand()方法來獲取事件的事件源。
- public class Test_01 extends JFrame implements ActionListener {
- Test_01() {
- 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(this);
- button2.addActionListener(this);
- }
- public void actionPerformed(ActionEvent e) {
- String source = e.getActionCommand();
- if (source.equals("按鈕一")) {
- System.out.println("你按了按鈕一");
- }
- if (source.equals("按鈕二")) {
- System.out.println("你按了按鈕二");
- }
- }
- public static void main(String args[]) {
- new Test_01();
- }
- }
利用一個(gè)監(jiān)聽器來處理事件的缺點(diǎn)是:其實(shí)當(dāng)處理的事件比較少的時(shí)候,這種方式還是一種比較好的方式,它簡單。當(dāng)程序比較復(fù)雜時(shí),需要一大串的if語句來實(shí)現(xiàn)。程序的代碼比較難閱讀和維護(hù)。
二、利用匿名內(nèi)部類來是實(shí)現(xiàn)
- public class Test_02 extends JFrame{
- Test_02(){
- 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 Test_02();
- }
- }
使用匿名內(nèi)部類來實(shí)現(xiàn)可以解決使用if來獲取事件源帶來的麻煩。但是使用匿名內(nèi)部類同樣存在著一些問題。由于它是和事件組一起的。根據(jù)事件組在代碼中的位置不同,類的定義以及處理事件,同樣不便于閱讀。如果事件處理程序比較復(fù)雜,內(nèi)部類中的代碼就會(huì)變的很長。
三、利用一般內(nèi)部類來實(shí)現(xiàn)
- public class Test_03 extends JFrame{
- Test_03(){
- 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 Button1ActionListener());
- button2.addActionListener(new Button2ActionListener());
- }
- private class Button1ActionListener implements ActionListener{
- public void actionPerformed(ActionEvent e) {
- System.out.println("你按了按鈕一");
- }
- }
- private class Button2ActionListener implements ActionListener{
- public void actionPerformed(ActionEvent e) {
- System.out.println("你按了按鈕二");
- }
- }
- public static void main(String[] args) {
- new Test_03();
- }
- }
利用一般內(nèi)部類我們可以解決很多的問題,該方法避免了第二種方法中由于使用匿名內(nèi)部類而導(dǎo)致的代碼混亂。它把所有的事件處理方法都集中在一塊,并且都具有有意義的名稱,程序非常容易閱讀與維護(hù)。單個(gè)的事件處理程序也可以被工具欄、菜單欄等重復(fù)使用。
基于上面的總結(jié),我們一般采用第三種方法來注冊(cè)事件。
原文鏈接:http://blog.csdn.net/chenssy/article/details/7392490
【編輯推薦】