2014-03-03 23 views
0

我目前有一個類框架,創建一個框架,添加按鈕和標籤。我目前在我的Frame類中有動作偵聽器的代碼,我需要將其移動,以便從匿名類調用動作偵聽器。這是我現在擁有的。如何在匿名類中創建Action Listener?

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? 
} 

我不確定,我是新的匿名類。任何人都可以告訴我如何在一個匿名類中實現動作偵聽器嗎?

+0

您需要提供一些手段,通過該'ActionListener'可以或許通過一個getter或setter – MadProgrammer

回答

0

你大概的意思是類似的東西:

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

+0

謝謝你,這是完美的註冊到按鈕,描述。你能告訴我爲什麼你在Frame類中做了這個,而不是在main類中?這是唯一的方法嗎? – Reeggiie

+0

您可以在'main'中以相同的方式聲明'firstActionListener'。但是,那麼爲什麼你要這麼做更加不明確。 (如果它是在類中聲明的,而不是在main中聲明的,例如,你可以稍後從按鈕中移除這個ActionListener,但是當它只在main中聲明時,這與'button1 。addActionListener(new ActionListener(){...}'你已經使用過... – Marco13

0

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

也就是說,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模式