簡體   English   中英

ActionListener介面

[英]ActionListener interface

public class myWindow extends JFrame implements ActionListener{

如果我有此代碼,則我的課程將是一個JFrame,在其中可以添加組件並在構造函數中向其添加actionlistener,如下所示

public MyWindow()
     {
      JButton b = new Jbutton("button");
       b.addActionListener(this);
        }

這個關鍵字將作為匿名的動作偵聽器對象(這是我的類)對嗎?

稍后我將使用以下標題覆蓋actionPerformed方法:

 public void ActionPerformed(ActionEvent ae)
         {      : 
                :
                        }

我在這里確實感到很困惑。.我的書說:“偵聽器對象以事件作為參數調用事件處理程序方法”

偵聽器對象:此

事件處理程序方法:ActionPerformed(ActionEvent ae)

參數:我的事件是JButton b ..如果不是EventAction類型,怎么會這樣呢? 如果是這樣,為什么我們要使用:

 ae.getActionCommand(); 

我認為這是一種告訴事件觸發哪個組件的方法,那么為什么當該組件作為參數傳遞時我們需要它呢?

這個關鍵字將作為匿名的動作偵聽器對象(這是我的類)對嗎?

不,這將是正在實現actionsListener的類的Object。 您的情況是“ MyWindow”。

我的事件是JButton b ..當它不是EventAction類型時,怎么會這樣呢? 如果是這樣,為什么我們要使用:

JButton b是一個組件,不是事件。 事件描述了源狀態的改變。 當用戶與GUI交互時,會生成事件,例如單擊按鈕,移動鼠標。

參考從這里

事件處理是一種控制事件並決定事件發生時應如何處理的機制。

事件處理涉及的步驟:-

  1. 用戶單擊按鈕,將生成事件。

  2. 現在,將自動創建相關事件類的對象,並在同一對象中填充有關源和事件的信息。

  3. 事件對象被轉發到已注冊的偵聽器類的方法。

  4. 該方法現在被執行並返回。

我認為這是一種告訴事件觸發哪個組件的方法,那么為什么當該組件作為參數傳遞時我們需要它呢?

現在您將了解,可能有許多按鈕注冊到了同一ActionsListerner。 現在可以對不同的事件執行不同的操作,使用e.getActionCommand()方便了,它將告訴您哪個按鈕是觸發事件的源。 希望這可以幫助。

我試圖給您提供一個簡單的JButton程序的示例。

    import javax.swing.*;       
    import java.awt.*;
    import java.awt.event.*;

    public class ButtonSwing {
        private int numClicks = 0;

        public Component createComponents() {
            //Method for creating the GUI componenets
            final JLabel label = new JLabel("Clicks: " + "0"); //final so that i can access inside inner class
            JButton button = new JButton("Simple Button");
            button.addActionListener(
            //inner class for ActionListener. This is how generally it is done.     
            new ActionListener() {
                           public void actionPerformed(ActionEvent e) {
                               numClicks++;
                               label.setText("Clicks: " + numClicks);
                   System.out.println(e.getActionCommand());
                   System.out.println(e.getModifiers());
                   System.out.println(e.paramString());
             }
            }
           );
            JPanel pane = new JPanel();   //using JPanel as conatiner first.
            pane.setLayout(new FlowLayout());
            pane.add(button);  // adding button to the JPanel.
            pane.add(label);   // adding label to the JPanel.
            return pane;
        }

        public static void main(String[] args) {
            JFrame frame = new JFrame("SwingApplication");
            ButtonSwing obj = new ButtonSwing();
            Component contents = obj.createComponents();
            frame.getContentPane().add(contents);
            frame.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });
            frame.pack();  //It will cause the window to be sized to fit the preferred size 
                            //and layouts of its subcomponents. 
            frame.setVisible(true);
        }
    }   

您的JButton是一個組件,不是事件。 在這種情況下,事件是由組件上的某些操作生成的,當您單擊按鈕時,將觸發一個ActionEvent並將其傳遞給訂閱該事件的所有偵聽器,在這種情況下,您的MyWindow對象將充當ActionListener

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM