2011-06-10 95 views
0
yellowButton.addActionListener(this); 

什麼是(this)在addActionListener方法中引用的。有人可以告訴我如何actionListener方法的作品?事件處理問題

+1

有趣;幾小時前我們剛剛有一位同學在這裏。 http://stackoverflow.com/questions/6310649/can-somebody-explain-how-this-works – 2011-06-10 21:32:27

回答

3

this是當前對象。它必須實現ActionListener接口。

這通常用於將所有相關的代碼放在一個地方。但你也可以使用yellowButton.addActionListener(new ButtonActionListener()),其中ButtonActionListener是你的另一類。

執行ActionListener的類應指定發生操作時發生的情況。

+0

哪些對象JButton對象。它是指自己嗎? – dave 2011-06-10 21:17:25

+0

執行代碼的對象。 – Bozho 2011-06-10 21:18:14