2012-07-24 94 views
0

#在我發佈的問題中,在此我無法理解第1行和第2行提到的代碼,因爲我知道他們是他們用於設置這個按鈕的動作監聽器,但是對我來說最令人困惑的是,在第1行和第2行中,{JB1.addActionListener(this)}的語法在這裏是「this」的角色。 ..請告訴我們背後的基本內容。以及整個語法是如何工作的...詳細信息。 #Swing:動作監聽器的作用

import java.awt.*; 
    import java.awt.event.ActionEvent; 
    import java.awt.event.ActionListener; 

    import javax.swing.*; 



    public class frametest1_1 implements ActionListener 
    { 
     JLabel JL; 
     public frametest1_1() 
     { 
      //create a JFrame container 
      JFrame JF=new JFrame("A BUTTON"); 

      //Frame Layout This is contained in Java .awt.*; "ON USING THIS OPTION OUR BUTTON AND OTHER COMPONENT ARE ADJUSTED IN THE FRAME AUTOMATICALLY" 
      JF.setLayout(new FlowLayout()); 

      //set the size of the container 
      JF.setSize(200, 200); 

      //set visible 
      JF.setVisible(true); 

      //make button 
      JButton JB1=new JButton("FIRST"); 
      JButton JB2=new JButton("SECOND"); 

      //add button to the JFrame container 
      JF.add(JB1); 
      JF.add(JB2); 

      //Create and add Label to the JFrame container 
      JL=new JLabel("PRESS A BUTTON"); 
      JF.add(JL); 

      //set action command :now this will help in determining that which button is presses, is it FIRST or SECOND 
      JB1.setActionCommand("one"); 
      JB2.setActionCommand("two"); 

      //The action responded is added to the actionlistener 
      JB1.addActionListener((ActionListener) this); // line 1 
      JB2.addActionListener((ActionListener) this); // line 2 
     } 

     public void actionPerformed(ActionEvent ae) 
     { 

      if(ae.getActionCommand().equals("one")) 
       JL.setText("First Button Pressed");  // to set text on the label 
      else 
       JL.setText("Second button Pressed"); // to set the text on the label 

     } 
     public static void main(String[] args) 
     { 
      SwingUtilities.invokeLater(new Runnable() 
      { 
       public void run() 
       { 
        new frametest1_1(); 
       } 
      }); 
     } 
    } 
+2

請學習Java命名約定並嚴格遵守。 – kleopatra 2012-07-24 11:40:19

回答

1

考慮到Listener is someone who reacts to some action

2.ActionListener接口具有回調方法,命名actionPerformed,在其內部是代碼當某些動作在控制器上完成將運行。

3.這條線JB1.addActionListener((ActionListener) this);意味着如下

JB1 - Button 
    ActionListener - Interface 
    addActionListener - Registering the Button with the Listener. 

4.addActionListener綁定/註冊ButtonListener(此處其的ActionListener)。

MVC架構Button is the controller,當某些動作上完成它,那麼誰都是被告知做某些事情是由它註冊到監聽器來完成。

6.而這種監聽器將具有callback方法,這將是由實現聽者類中重寫。

7.在你的榜樣此外,你也可以做這樣的...... JB1.addActionListener(this);

+0

謝謝.....但讓我知道一件事,我傳遞「this」的參數基本上是類frametest1_1.if的實例,而不是「this」,我創建它後像frametest1_1一樣傳遞此類的對象obj = new frametest1_1();,它會錯的 – 2012-07-24 11:43:08

+0

「this」這裏的意思是「當前對象」,你正在傳遞實現了Runnable接口的類的實例,你可以傳遞這個類的對象到這個位置,但是你必須確定你正在實例化''你從main()函數'附加了監聽器的相同對象。當你應用Singleton Pattern時,可能會產生一個對象。 – 2012-07-24 16:06:48

1

「這」指的是封閉類(frametest1_1)的當前實例,該實例中構建run()方法。

一些進一步的讀數上「這個」: Using the this Keyword

-1

提供的代碼是從視圖面向對象的設計點非常難看 - 因此你的困惑。這絕對是壞主意,有一類負責:

  • 含主要方法
  • 抱着JFrame組件(即JLabel)引用
  • 實施 ActionListener概念

侵犯Single responsibility principle,雖然這種醜陋的例子甚至可以在Swing的官方教程中找到。

稍好執行完全相同的功能將是以下幾點:

import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.*; 

public class frametest1_1 { 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       runFrameTest(); 
      } 
     }); 
    } 

    public static void runFrameTest() { 
     //create a JFrame container 
     JFrame JF=new JFrame("A BUTTON"); 

     //Frame Layout This is contained in Java .awt.*; "ON USING THIS OPTION OUR BUTTON AND OTHER COMPONENT ARE ADJUSTED IN THE FRAME AUTOMATICALLY" 
     JF.setLayout(new FlowLayout()); 

     //set the size of the container 
     JF.setSize(200, 200); 

     //set visible 
     JF.setVisible(true); 

     //make button 
     JButton JB1=new JButton("FIRST"); 
     JButton JB2=new JButton("SECOND"); 

     //add button to the JFrame container 
     JF.add(JB1); 
     JF.add(JB2); 

     //Create and add Label to the JFrame container 
     final JLabel JL=new JLabel("PRESS A BUTTON"); 
     JF.add(JL); 

     //set action command :now this will help in determining that which button is presses, is it FIRST or SECOND 
     JB1.setActionCommand("one"); 
     JB2.setActionCommand("two"); 

     ActionListener labelUpdater = new ActionListener() { 
      public void actionPerformed(ActionEvent ae) {    
       if(ae.getActionCommand().equals("one")) 
        JL.setText("First Button Pressed");  // to set text on the label 
       else 
        JL.setText("Second button Pressed"); // to set the text on the label    
      } 
     }; 
     //The action responded is added to the actionlistener 
     JB1.addActionListener(labelUpdater); // line 1 
     JB2.addActionListener(labelUpdater); // line 2 
    } 

} 

希望這有助於理解......

+1

您正在通過使用初始化GUI的靜態方法提供反OO模式來提供OO課程。您應該有一個實例,它自己在構造函數或實例方法中創建GUI。 'static'是OO編程最糟糕的敵人! – 2012-07-24 12:05:58

+0

我同意我的代碼不是100%從OO的角度來看是完美的,但我試圖解釋如何在Swing中正確使用ActionListener,而不是如何構造基於Swing的應用程序。關於這裏使用的'static'方法 - 我在這裏使用它有2個原因:1.這個例子太小,無法在這裏創建單獨的類/實例2.爲了不污染'''邏輯的main'方法。 – Yura 2012-07-24 12:16:17

+1

答案的第一句話:「提供的代碼在面向對象設計的角度上非常醜陋」,所以我會假設你會提供一些從面向對象設計的角度來看並不醜陋的東西。其次,如果你認爲你的例子太小而不能遵循面向對象的設計,這使得問題與你的答案一樣有效。最後,從MVC的角度來看,這是完全可以接受的。您有一個控制器類(frametest_1_1),它可以創建視圖並偵聽視圖事件。我認爲用戶主要關心'this'和'ActionListener'的用法。 – 2012-07-24 12:30:25