国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

something about Listeners

2019-11-18 11:26:25
字體:
供稿:網(wǎng)友

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


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 双城市| 彭州市| 多伦县| 上饶县| 安新县| 万全县| 徐水县| 富宁县| 新邵县| 克东县| 邻水| 万荣县| 鹤岗市| 安陆市| 乐山市| 新源县| 遂川县| 平邑县| 太白县| 甘肃省| 延安市| 德保县| 绍兴县| 和龙市| 通州区| 莲花县| 鹤峰县| 武邑县| 泸州市| 察雅县| 南靖县| 仪陇县| 迭部县| 靖宇县| 招远市| 麻城市| 神木县| 左贡县| 明水县| 大化| 潜山县|