Re: [問題] 關於事件處理
※ 引述《akdsy (我喜歡小玉)》之銘言:
: 我照著書上這樣寫,
: 做按鈕的時候是利用 actionPerformed()
: public void actionPerfromed(ActionEvent e)
: {
: Button btn = (Button) e.getSource();
: ................
: }
: 這樣的確可以在按下按鈕之後做出反應,
: 然後在做Menu的時候,
: 他也是利用 actionPerfromed()
: public void actionPerformed( ActionEvent e)
: {
: MenuItem mi = (MenuItem) e.getSource();
: ...................
: }
: 問題來了!
: 可是要是同時做有 Botton 和 Menu 的時候,
: 這樣要怎麼讓 Button 和 Menu 做區別呢!
Object objClick= e.getSource();
if(objClick instanceof MenuItem){
MenuItem mi = (MenuItem) objClick;
/* do something ....*/
}else if( objClick instanceof Button){
Button btn = (Button) objClick;
/*do something*/
}
或者如果你已經確定它會是那個實例
if(objClick== smButton ){
/*do something....*/
}
instanceof 關鍵字可以分辨出類別 「屬於」 那個類別
是二元關鍵字 會回傳boolean值
--
I am a person, and I am always thinking .
Thinking in love , Thinking in life ,
Thinking in why , Thinking in worth.
I can't believe any of what ,
I am just thinking then thinking ,
but worst of all , most of mine is thinking not actioning...
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 60.250.227.184
→
02/27 21:35, , 1F
02/27 21:35, 1F
推
02/27 21:42, , 2F
02/27 21:42, 2F
推
02/27 21:55, , 3F
02/27 21:55, 3F
推
02/29 00:33, , 4F
02/29 00:33, 4F
討論串 (同標題文章)
java 近期熱門文章
PTT數位生活區 即時熱門文章