2016-11-22 30 views
0

我已經提交的JButton有兩個的ActionListeners如何在內存中設置ActionListeners?

Category cat = new Category(); 

//this is launched second 
submit.addActionListener(new ConfirmListener(new CategoryService(), cat)); 

//this is launched first. Set values 
submit.addActionListener(new ActionListener() { 

    public void actionPerformed(ActionEvent e) { 
     String title = field.getText(); 
     cat.setTitle(title); 
    } 

}); 

難道他們把堆棧和他們彈出他們推出?第二個偵聽器設置對象的值,第一個偵聽器使用該對象。

回答

1

ActionListener s存儲在EventListenerList中,所以它們將按照添加順序執行。

3

'ActionListener's存儲在組件的成員列表數據結構中。它們通常按照添加順序進行調用,但不能保證,並且可以通過例如子類別進行不同處理。

因此,依賴於偵聽器的特定調用順序被認爲是不好的做法。如果需要訂單,請創建一個組合監聽器,然後以您需要的順序調用實際的監聽器方法。