java getSource()和 getActionCommand()
生活随笔
收集整理的這篇文章主要介紹了
java getSource()和 getActionCommand()
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
比如說 按紐的事件,同一個JFrame里可能有多個按鈕的事件,為了避免沖突,給每個按鈕設置不同的ActionCommand,在監聽時間的時候,用這個做條件區分事件,以做不同的響應
追問 他與getSource有什么區別
回答 getSource()
Returns:
The object on which the Event initially occurred.
依賴于事件對象getActionCommand()Returns the command name of the action event fired by this button. If the command name is null (default) then this method returns the label of the button.
依賴于按鈕上的字符串getSource得到的組件的名稱,而getActionCommand得到的是標簽。
如:Button bt=new Button("buttons");用getSource得到的是bt 而用getActionCommand得到的是:buttonse.getSource() 返回的當前動作所指向的對象,包含對象的所有信息
e.getActionCommand() 返回的是當前動作指向對象的名稱
總結
以上是生活随笔為你收集整理的java getSource()和 getActionCommand()的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java构造函数经典详解
- 下一篇: Java Vector与ArrayLis