簡體   English   中英

如何在匿名類內部創建Action Listener?

[英]How to create Action Listener inside anonymous class?

我目前有一個Frame類,用於創建框架,添加按鈕和標簽。 我目前在Frame類中具有動作偵聽器的代碼,並且需要移動它,以便從匿名類中調用動作偵聽器。 這是我目前擁有的。

 public static void main(String[] args)
 {
   Frame grid = new Frame();
    grid.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    grid.setVisible(true);
    grid.pack();
    grid.setTitle("Frame");
}

public class Frame extends JFrame
{
ImageIcon green = new ImageIcon("green.png");
JLabel label;
public JButton button1,button2;

public Frame()
{

    setLayout(new GridLayout(4,4));

  /**create buttons*/
    button1 = new JButton("");
    add(button1);
    button2 = new JButton("");
    add(button2);
    label = new JLabel(green);
    add(label);



/**Add action listener to buttons, I need these 2 liseners in a anonymous class */
button1.addActionListener(new ActionListener() 
{
        public void actionPerformed(ActionEvent arg0) 
        {
           button1.setText("X");                       
}

});
button2.addActionListener(new ActionListener() 
{
public void actionPerformed(ActionEvent arg0) 
{
           button2.setText("X");    

}

});

誰能告訴我如何移動該動作監聽器,以便從匿名類中調用它? 我假設我在創建框架時主要是這樣做的? 像這樣的東西?

Frame grid = new Frame(){
       //Code might go here?
}

我不確定,我是匿名類的新手。 誰能告訴我如何在匿名類中實現動作偵聽器?

您可能會說類似的話:

class Frame 
{
    private JButton button1;

    // Here it is:
    private ActionListener firstActionListener = new ActionListener()
    {
        public void actionPerformed(ActionEvent arg0) 
        {
            button1.setText("X");
        }
    };

    public Frame()
    {
        ....
        button1.addActionListener(firstActionListener);
    }
}

雖然我想知道您需要什么。 您可能需要看一下http://docs.oracle.com/javase/tutorial/uiswing/misc/action.html

您不能簡單地將偵聽器添加到既不支持您嘗試連接的偵聽器的組件,也不能添加到您沒有引用的組件。

也就是說, JFrame不支持ActionListener並且您沒有要向其添加操作的按鈕的引用。

我知道你所做的按鈕public ,但對我來說,這是一個糟糕的主意,因為你是暴露的組件外修改。 沒有什么能阻止代碼更改按鈕的狀態,將控制權從Frame類移開了。

相反,您應該為感興趣的各方提供注冊興趣的功能,例如知道何時激活按鈕。

public static class Frame extends JFrame {

    ImageIcon green = new ImageIcon("green.png");
    JLabel label;
    private JButton button1, button2;

    public Frame() {
        //...
    }

    public void addButton1ActionListener(ActionListener listener) {
        button1.addActionListener(listener);
    }

    public void addButton2ActionListener(ActionListener listener) {
        button2.addActionListener(listener);
    }

    public void removeButton1ActionListener(ActionListener listener) {
        button1.removeActionListener(listener);
    }

    public void removeButton2ActionListener(ActionListener listener) {
        button2.removeActionListener(listener);
    }

然后,您只需將ActionListener添加到想要的任何按鈕,例如...

public static void main(String[] args) {
    Frame grid = new Frame();
    grid.addButton1ActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            // Do stuff
        }
    });
    grid.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    grid.setVisible(true);
    grid.pack();
    grid.setTitle("Frame");
}

現在,這引起了問題,因為現在您已經提供了對底層按鈕的訪問權限,而您可能不想這么做。

現在,除了問題以外,我可能建議使用某種模型。 這將應用於Frame ,使其可以根據需要修改其狀態,然后將事件通知提供給感興趣的各方,說明某些狀態或其他狀態已更改,並且他們應采取適當的措施,例如更新視圖。

最好用MVC模式來描述

暫無
暫無

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

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