Java Learning (9)

Source: Internet
Author: User

Event handling:

ActionEvent && windowevent:

ActionEvent--Contains a action_performed. The triggered action has a click button, double click Options, select the specified menu item, enter input, and so on.

addActionListener (<al>)--Al->actionlistener or a class implements ActionListener.

Button.addactionlistener (new ActionListener () {public    void actionperformed (ActionEvent event) { Buttonpanel.setbackground (backgroundcolor);    }});

The above code is equivalent to declaring a class implementing the ActionListener interface and then implementing the Actionperformed method in the class. Where backgroundcolor should be defined as final.

Why do this, because the Java compiler will automatically generate a constructor when compiling, in other words, it will generate more than one class, as shown, the class file "Buttonframe$1.class", and the value passed when the value passed when the assignment is not allowed in the change, There will be no inconsistency in the situation.

EventHandler-Create () can be used for dynamically creating listeners. The action object must be public, otherwise the build fails.

Eventhandler.create (Class actioninterfaceclass, Object Target, String actionlist, string prpertieslist);

Actionlist,propertieslist--"Action.action1.action2 ..." && "Para1. Para2. "Use dots to separate multiple elements. (Not quite understand--to be added)

The listener is used to monitor the event, and the included Actionperformed method is to handle the event.

JComponent && JPanel:

JComponent is a container for the Staples Swing component (in addition to the topmost component).

JPanel is a jcomponent, a lightweight container with many layouts, in the Java API inheritance order: Java.awt.Container---javax.swing.JComponent Javax.swing.JPanel

At the heart of event handling is an object that adds a listener to handle an action event:

Object.addactionlistener (new ActionListener () {}); (Anonymous inner class--defines an object that implements the class of the ActionListener interface)

--Object.addactionlistener (new ActionListener () {public void actionperformed () {}});

--Object.addactionlistener (new ActionListener () {public void actionperformed (ActionEvent event) {}});

So the root cause is to define an action and define the method that good one handles the action (new Actionsample implements ActionListener {public void actionperformed ( ActionEvent event) {}}), and then use Smpleobject.addactionlistener (actionsample) when defining an instance of an object, to add an incident handler listener to the instance.

A Java noun: adapter class. Scope of application, there are many methods defined in the interface, but the class used to implement the interface requires only a subset of the methods defined in the interface, so a class is required to implement this interface as an adapter class, where all interface method implementations in the adapter class are NULL, In this way, the class that originally needs to implement this interface can be overridden by inheriting the adapter class to only partial classes, avoiding content redundancy. Associate to an anonymous inner class, first the anonymous inner class is used to define an object that implements an excuse or inherits a class from a class, so if you set an adapter class that implements an excuse, and then apply that adapter class to an anonymous inner class, you can avoid implementing many unnecessary methods in the definition section of an anonymous inner class. The result is beautiful Java code and improved readability.

Semantic events: Events that describe a user's actions, such as clicking a button;

Low-level events: events that compose semantic events, such as moving the mouse while clicking a button, lifting the mouse, and pressing the mouse button;

Java Learning (9)

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.