WEB开发网      濠电娀娼ч崐濠氬疾椤愶附鍋熸い鏍ㄧ〒闂勫嫰鏌﹀Ο渚Ц闁诲氦顕ч湁婵犲﹤楠告禍鍓х磼鏉堛劌绗氶柟宄版嚇閹晠宕归銈嗘濠电偞鍨堕幐鎾磻閹捐秮褰掓偐閻戞﹩妫勯梺鎼炲妼鐎涒晝绮嬪澶樻晝闁挎繂鏌婇敃鍌涚厵閻庢稒锚閻忥絾绻濇繝鍐ㄧ伌闁诡垰鍟村畷鐔碱敂閸♀晙绱樺┑鐐差嚟婵儳螞閸曨剚鍙忛柍鍝勬噹缁€澶嬬箾閹存繄锛嶆鐐灲閹綊宕惰濡插鏌涢妸銉ヮ劉缂佸倸绉归弫鎾绘晸閿燂拷 ---闂備焦瀵уú鈺呭箯閿燂拷
开发学院WEB开发Jsp something about Listeners 阅读

something about Listeners

 2008-01-05 18:41:35 来源:WEB开发网 闂備線娼уΛ鎾箯閿燂拷闂備礁鎲¢崹鐢垫崲閹扮増鍎嶆い鎺戝€甸崑鎾斥槈濞嗗秳娌紓鍌氱▌閹凤拷濠电姭鎷冮崨顓濈捕闂侀潧娲ゅú銊╁焵椤掍胶鈯曢柕鍥╁仧缁辩偤鏁撻敓锟�闂備線娼уΛ鎾箯閿燂拷  闂備胶枪缁绘鈻嶉弴銏犳瀬闁绘劖顐煎☉妯锋瀻闁归偊鍓涘▔姘舵⒑閸涘⿴娈旀繛灞傚妼閳绘捇骞嬪┑鎰濡炪倖姊婚崢褏鎲撮敓锟�
核心提示:ListenersListeners are how anything gets done in Swing. Clicking on stuff causes Events. Events are sort of like little messages that get sent around inside you

Listeners


Listeners are how anything gets done in Swing. Clicking on stuff causes Events. Events are sort of like little messages that get sent around inside your PRogram. If you want, you can watch all these messages and try to filter out the ones you want. That would be a colossal waste of time, on top of being really, really inefficient. There are better ways to do things. Namely, Event Listeners.

Event Listeners are functions you define that will be called when Events happen. The JFC/Swing core tells each Component - like JButtons and JMenus - when an Event they would be interested in occurs. This is how JButtons know to make themselves look "clicked". You can also ask a JButton to tell you when it gets clicked by registering an Event Listener with it. If you were to write java code with Emacs, there would be 3 steps involved in registering an Event Listener with a JButton:

1 - Define the listener:
There are different kinds of Events, so there are different kinds of Listeners. JButton generates an ActionEvent when it is clicked, so we create a class that implements the ActionListener interface...

class AnActionListener implements ActionListener{
   public void actionPerformed(ActionEvent actionEvent){
      System.out.println("I was selected.");
   }
}


2 - Create an instance of the listener
Ok, this is pretty simple...

ActionListener actionListener = new AnActionListener();


3 - Register the listener with a component
Start out by pretending you already have a JButton and you want that listener function we wrote in step 1 to get called when J. Random User clicks on it. You do this by registering it with the JButton. Essentially you ask the JButton to add it to the list of functions it calls when it decides to generates an ActionEvent, like so...

JButton button = new JButton();
... // other code
button.addActionListener(actionListener);

Visual Age to the Rescue!
Most programs have lots of buttons. And Menus. And List boxes. And Other Stuff (tm). Writing Event Listeners for all of these would be tedious and error-prone. Debugging Event Listeners isn't mUCh fun either. That's why we have Visual IDE's (Integrated Development Environments). In VAJ, you can draw some buttons and other components, then do some pointy-clicky stuff and VAJ will create and manage all those Event Listeners for you. Most of the time you don't even have to write any code! Ok, I'm getting a little too excited, that's it for Listeners.



Figure 1. A simple GUI

Figure 1's GUI comprises a panel containing three buttons and a label. When you press a button, the GUI writes a message to the label indicating which button you pressed.

To display the panel, I embedded it within a frame. However, for the purposes of today's Java Q&A, I'll simply focus on the panel code. (Please see Resources for the full source code.)

Implementation

Tags:something about Listeners

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接