This article is your own study notes, welcome reprint, but please indicate the source: http://blog.csdn.net/jesson20121020
In the event model of swing, a component can initiate (trigger) an event. The type of each event is represented by a different class, and when the event is triggered it is received by one or more "listeners" and the listener is responsible for handling the event. So, the place where the event occurs can be separated from where the event was handled. now that you are using swing components in this way, you only need to write the code that will be called when the component receives the event. The so-called event listener is a class object that implements a listener interface of a particular type.
Below, we present the swing event model as an example of finding AddListener in the swing component, complete with the following code:
public class Showaddlisteners extends JFrame {private JTextField name = new JTextField;p rivate JButton search = new JB Utton ("Query");p rivate jtextarea results = new JTextArea (30,50);//define regular expression matching addlistenerprivate static Pattern AddListener = Pattern.compile ("(add\\w+? Listener\\ (. *?\\));p rivate static Pattern qualifier = Pattern.compile ("\\w+\\."); Public Showaddlisteners () {setvisible (true); SetSize (600, 400); JPanel top = new JPanel () Top.add (New JLabel ("Swing Class name (press Enter):")); Top.add (name); Top.add (search); Add ( Borderlayout.north,top); Add (new JScrollPane (results));//Set non-editable//results.seteditable (false); Search.addactionlistener (new ActionListener () {@Overridepublic void actionperformed (ActionEvent event) {//TODO Auto-generated method stub//Remove the front and back space of the keyword string nm = Name.gettext (). Trim ();//If the length of the keyword is omitted, the if is not successful if (nm.length () = = 0) {Results.settext ("no match!"); return;} Class<?> kind;try {kind = Class.forName ("javax.swing.") +NM);} catch (ClassNotFoundException e) {//TODO Auto-geNerated catch Blockresults.settext ("no match!"); return;} Method[] methods = Kind.getmethods () results.settext (""); for (Method m:methods) {Matcher Matcher = Addlistener.matcher ( M.tostring ());//If a matching method is found, output if (Matcher.find ()) {Results.append (Qualifier.matcher (Matcher.group (1)). ReplaceAll ("") + "\ n");}}});} /** * @param args */public static void main (string[] args) {//TODO auto-generated method Stubnew showaddlisteners ();}}Effect:
The main use of the regular expression to match the corresponding listener, here is just a listener to receive, it is also mentioned above, the component can also bind multiple listeners.
Here are a few important event listeners for JButton, namely Focuslistener, KeyListener, MouseListener, and Mousemotionlistener
, bind these listeners to the button and, when the corresponding event occurs, trigger the corresponding event to perform the corresponding operation, the complete code is as follows:
public class TrackEvent extends JFrame {private hashmap<string,jtextfield> map = new hashmap<string,jtextfield& gt; ();p rivate string[] event = {"Focusgained", "Focuslost", "keypressed", "keyreleased", "keytyped", "mouseclicked", " Mouseentered "," mouseexited "," mousepressed "," mousereleased "," mousedragged "," mousemoved "};p rivate MyButton b1 = new MyButton (Color.Blue, "test1");p rivate MyButton b2 = new MyButton (color.red, "test2"); class MyButton extends jbutton{ private void report (String field,string msg) {map.get (field). SetText (msg);} Private Focuslistener FL = new Focuslistener () {@Overridepublic void Focuslost (FocusEvent e) {//TODO auto-generated Metho D stubreport ("Focuslost", e.paramstring ());} @Overridepublic void focusgained (FocusEvent e) {//TODO auto-generated method Stubreport ("focusgained", e.paramstring () );}}; Private KeyListener KL = new KeyListener () {@Overridepublic void keytyped (KeyEvent e) {//TODO auto-generated method Stubr Eport ("keytyped", e.paramstring ());} @Overridepublic voidkeyreleased (KeyEvent e) {//TODO auto-generated method Stubreport ("Keyreleased", e.paramstring ());} @Overridepublic void keypressed (KeyEvent e) {//TODO auto-generated method Stubreport ("Keypressed", e.paramstring ());}}; Private MouseListener ml = new MouseListener () {@Overridepublic void mousereleased (MouseEvent e) {//TODO auto-generated m Ethod stubreport ("mousereleased", e.paramstring ());} @Overridepublic void mousepressed (MouseEvent e) {//TODO auto-generated method Stubreport ("mousepressed", e.paramstring ());} @Overridepublic void mouseexited (MouseEvent e) {//TODO auto-generated method Stubreport ("mouseexited", e.paramstring () );} @Overridepublic void mouseentered (MouseEvent e) {//TODO auto-generated method Stubreport ("Mouseentered", e.paramstring ());} @Overridepublic void mouseclicked (MouseEvent e) {//TODO auto-generated method Stubreport ("mouseclicked", e.paramstring ());}}; Private Mousemotionlistener MML = new Mousemotionlistener () {@Overridepublic void mousemoved (MouseEvent e) {//TODO AuTo-generated method Stubreport ("Mousemoved", e.paramstring ());} @Overridepublic void mousedragged (MouseEvent e) {//TODO auto-generated method Stubreport ("Mousedragged", e.paramstring ());}}; Public MyButton (color color,string label) {super (label); SetBackground (color); Addfocuslistener (fl); Addkeylistener ( KL); Addmouselistener (ml); Addmousemotionlistener (MML);}} Public TrackEvent () {//TODO auto-generated constructor Stubsetlayout (new GridLayout (Event.length +)), for (String evt : event) {JTextField tf = new JTextField (); tf.seteditable (false); Add (new JLabel (Evt,jlabel.right)); add (TF); SetSize ( 700,500); setvisible (true); Map.put (EVT, TF);} Add (B1); add (B2);} /** * @param args */public static void main (string[] args) {//TODO auto-generated method Stubswingutilities.invokelater (n EW Runnable () {@Overridepublic void Run () {//TODO auto-generated method Stubnew TrackEvent ();});}}Execution Result:
This way, when events such as mouse click Buttons, drag buttons, move buttons, and so on occur, the appropriate listener is triggered to display the action of the event as a string in JTextField, where the component tracks multiple events.
Java Interface Programming (4)------Swing Event Model