Java Learning Note (ii) Event listener

Source: Internet
Author: User
Tags event listener

Java implementations for component events (such as click, Input, and so on) are similar to JavaScript, is to add listener, and then write the trigger function, the difference is that the Java implementation of the listener must use implements to add each interface to the class.

Related libraries for java.awt.event.*

For example, to add an interface ActionListener for a behavior event, you need to do the following within the implementation window's class:

public class Test extends JFrame implements actionlistener{public Test () {//...}    public void actionperformed (ActionEvent event) {//...} }

Note that as soon as you add the ActionListener interface, and test is not an abstract class, you must add the corresponding trigger function, which is added as actionperfomed.

To handle the keystroke click as an example, the following steps:

① Add ActionListener interface

② Write Trigger function actionperformed (ActionEvent event) within the corresponding class

③ Add Listener to button (set to button1): Button1.addactionlistener (This)

④ Adding a button to the container

⑤ in the trigger function, there are two ways to judge the triggering object

The first, using the GetSource () method of the event, gets an object that can be directly judged using the = = expression, such as

Object Source = Event.getsource (); if (Source = = button1) {//...}

The second is to set the behavior command prior to adding the listener, such as adding the action command "TRIG1" to button1 first.

Button1.setactioncommand ("Trig1");

Within the trigger function, use the Getactioncommand () method to get the behavior command:

if (event.getactioncommand () = = "Trig1") {//...}

The second usage is more used when several components trigger the same action, such as exiting the program function, which may exist both in menus and toolbars, and they can use the same command to simplify the code.


For other listening, similar to this, do not repeat, it is important to note that you must first add the various properties of the component, and then put it into the container, otherwise the settings will be ignored.

For window monitoring, there are a number of methods, if used as above, it will produce a lot of empty methods, in order to avoid this, you can use the adapter class to listen. You can then create a class under the original class to inherit the adapter for the response.

For example, the following keyboard listener, add a class to inherit keyadapter to implement monitoring, for the required trigger function, overwrite can be

Class Keymonitor extends keyadapter{public void keytyped (KeyEvent event) {//...}}


Java Learning Note (ii) Event listener

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.