Author: vamei Source: http://www.cnblogs.com/vamei welcome reprint, please also keep this statement. Thank you!
In the GUI, we can see how to use the Graphic Tree to organize a GUI. However, such a graphical interface is static. We cannot interact with this interface. Gui graphic elements need to be addedEvent Response(Event handling) to obtain a dynamic graphical interface.
Element, event, listener
We mentioned many graphic elements in the GUI article. Some events may occur on these graphic elements, such:
- Click
- Drag a scroll bar
- Select menu
Events in Java are represented by objects, such as actionevents. The graphic objects that each event has an effect on, such as buttons, scroll bars, and menus.
The so-called interactive GUI refers to the generation of corresponding actions when the above events occur, such:
- Change color
- Change window content
- Pop-up menu
Each action is targeted at an event. We put the action in an actionlistener and let the listener monitor the events of a certain graphic object. When an event occurs, the action in the listener occurs accordingly.
Therefore, a responsive GUI is the result of interaction between graphical objects, event objects, and listener objects. We already know how to create a graphic object. We need to add listeners to graphic objects and let listeners capture events.
Button response
The following is a responsive button. After you click the button, the panel color changes, for example:
(This example is adapted from core Java 2, Volume 1, Example 8-1)
Import Javax. Swing .* ; Import Java. AWT. event .* ; Import Java. AWT .*; Public Class Helloworldswing { Private Static Void Createandshowgui () {jframe Frame = New Jframe ("helloworld" ); Frame. setdefaclocloseoperation (jframe. exit_on_close ); // Pane's layout Container CP = Frame. getcontentpane (); CP. setlayout ( New Flowlayout ()); // Add interactive panel to content pane CP. Add ( New Buttonpanel ()); // Show the window Frame. Pack (); frame. setvisible ( True );} Public Static Void Main (string [] ARGs) {runnable tr = New Runnable (){ Public Void Run () {createandshowgui () ;}; javax. Swing. swingutilities. invokelater (TR );}} /** * Jpanel with event handling */ Class Buttonpanel Extends Jpanel { Public Buttonpanel () {jbutton yellowbutton = New Jbutton ("yellow" ); Jbutton redbutton = New Jbutton ("red" ); This . Add (yellowbutton ); This . Add (redbutton ); /** * Register actionlisteners */ Colequaltion yellowaction = New Colequaltion (color. Yellow); colequaltion redaction = New Colequaltion (color. Red); yellowbutton. addactionlistener (yellowaction); redbutton. addactionlistener (Redaction );} /** * Actionlistener as an inner class */ Private Class Colpartition tion Implements Actionlistener { Public Colequaltion (color C) {backgroundcolor = C ;} /** * Actions */ Public Void Actionreceivmed (actionevent event) {setbackground (backgroundcolor ); // Outer object, jpanel Method Repaint ();} Private Color backgroundcolor ;}}
Above, we use an internal class colpartition to implementActionlistenerInterface. This is done to make it easier for the listener to call Members of graphical objects, such as the setbackground () method.
ActionlistenerActionreceivmed ()The method must be overwritten. This method containsAction. The parameter of this method is the event object, that is, the listener.ActioneventTypeEvent. Actionevent is a high-level class. Java will find the typical event (click) that will occur in the graphic object (button)Event.
The object generated by colexception is the listener object.
We added the corresponding listener object for the two buttons jbuttons. When an event occurs, the corresponding action is triggered accordingly.
Summary
Actionlistener Interface
Actionevent class
Welcome to continue reading the "Java quick tutorial" SeriesArticle