java event listener mechanism consisting of

Event Source (Component)

Event (Event)

Listener (Listener)

Event Processing (after treatment initiation event)

 

Event listener mechanism flowchart

 

 

 

Be sure to remember the simplicity:

Determining an event source (container or component)

AddXXXListener by the event source object () method to register the listener with the event source.

The receiving method XXXListener subclass object, or XXXListener subclass XXXAdapter the subclass object.

General use anonymous inner classes to represent.

When the coating method, a method generally XXXEvent parameter type variable reception.

After the incident triggered event will be packaged into object passed to the variable. (Including the event source object by getSource () or, getComponent () Get.)

 

Event Source: graphical interface components are those swing awt package or packages.
   

Event: Each event has its own unique source of common events and corresponding event.

Listener: The action could trigger a certain event (more than one action) have been packaged into a listener.

More than three, in java you have been defined. Direct access to its objects with it.

We need to do that is to be processed to produce action.

 

Eg:

package june610;

 

import java.awt.Button;

import java.awt.FlowLayout;

import java.awt.Frame;

import java.awt.TextField;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.KeyAdapter;

import java.awt.event.KeyEvent;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

 

class MyWin extends WindowAdapter{

    public void windowClosing(WindowEvent e){

        System.out.println("hahahha");

        System.exit(0);

    }

}

 

public class FrameDemo {// if written in the form of an internal class that must be preceded by public static, since the main method is static, dynamic class or method can not be called

    public static void main(String[] args) {

        // set the form

        Frame f = new Frame("窗体");

        f.setSize(400, 300);

        f.setLocation (500, 300); // left distance from the top

        / ** This method can be used provided a disposable

         The setBounds * ( int X, int Y, int width, int height) and the moving assembly to adjust the size.

         */

        Button b = new Button("按钮");

        Button b2 = new Button("按钮2");

        TextField tf = new TextField(20);

        f.add (b); // add a button to the form

        f.add (b2); // add a button to the form

        f.add (tf); // order on the form in the order added

       

        f.setLayout ( new new the FlowLayout ()); // Sets the layout manager container

        //f.addWindowListener(new MyWin ());

       

        b.addActionListener ( new new the ActionListener () {// anonymous inner classes, added convenience action listener

 

            public void actionPerformed(ActionEvent e) {

                . System OUT .println ( "button to turn off the screen");

                //System.exit(0);

            }

        });

       

        //mouse

        b.addMouseListener ( new new MouseAdapter An () {// mouse motion listener

            int count = 1;

            public void mouseEntered(MouseEvent e){

                . System OUT .println ( "mouse enter" + (count ++) + "times!");

            }

        });

       

        b.addMouseListener ( new new MouseAdapter An () {// and as above, can be written together

            int clickCount = 1;

            public void mouseClicked(MouseEvent e){

                if(e.getClickCount() == 2){

                . The System OUT .println ( "Double-action" + clickCount ++);  

                }

            }

        });

       

        /*

        f.addWindowListener (new WindowAdapter () // anonymous inner classes wording

        {

            public void windowClosing(WindowEvent e)

            {

                System.out.println ( "I'm off");

                System.exit(0);

            }

            public void windowActivated(WindowEvent e)

            {

                System.out.println ( "I live.");

 

            }

           

            public void windowOpened(WindowEvent e)

            {

                System.out.println ( "I was opened, hahahhahah ");

            }

 

        });*/

       

        //keyboard:

        b2.addKeyListener(new KeyAdapter() {

           

            public void keyPressed(KeyEvent e)

            {

                . The System OUT .println ( "action keyboard"); // no response by using the mouse,

                System.out.println(e.getKeyChar()+"---"+e.getKeyCode());//f---70等、

                IF (e.getKeyCode () == 27) {// Hold down the esc key to exit

                    . System OUT .println ( "the ESC key to shut me up!");

                    System.exit(0);

                }

                // Key combination CTRL + ENTER to close

                if(e.isControlDown() && e.getKeyCode() == KeyEvent.VK_ENTER){

                    . System OUT .println ( "CTRL + the ENTER key combination to shut me up!");

                    System.exit(0);

                }

            }

        });

        // text box

        tf.addKeyListener(new KeyAdapter() {

            public void keyPressed(KeyEvent e){

                if(!(e.getKeyCode()>=KeyEvent.VK_0 && e.getKeyCode()<=KeyEvent.VK_9)){

                    . System OUT .println (e.getKeyChar () + "is not in line with the digital!");

                }

            }

        });

       

        f.setVisible ( to true ); // set the visibility

 

    }

 

}

 

Eg: // lists all files within the folder

package june610;

 

import java.awt.Button;

import java.awt.FlowLayout;

import java.awt.Frame;

import java.awt.TextArea;

import java.awt.TextField;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.KeyAdapter;

import java.awt.event.KeyEvent;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import java.io.File;

 

class MyFrame {

    private Frame f;

    private Button b;

    private TextField tf;

    private TextArea ta;

 

    MyFrame() {

        init();

    }

 

    void init() {

        f = new Frame ( "My Computer");

        f.setBounds(300, 100, 600, 500);

        f.setLayout(new FlowLayout());

        b = new Button ( "to");

        tf = new TextField(60);

        ta = new TextArea(25, 70);

 

        f.add(tf);

        f.add(b);

        f.add (a);

        f.setVisible(true);

        action();

    }

 

    // operations on form

    void action() {

        f.addWindowListener(new WindowAdapter() {

            public void windowClosing(WindowEvent e) {

                System.exit(0);

            }

        });

       

        buttonAction();

        keyAction();

    }

    void keyAction(){

        // set keyboard sniffers, when the enter key input to achieve the same function and click the mouse!

        b.addKeyListener(new KeyAdapter() {

            public void keyPressed(KeyEvent e){

                //if(e.getKeyCode() == 10){

                    //buttonAction();

                //}

               

                String dirPath = tf.getText (); // Get the text (we want to verify that the path), then get the file

                File file = new File (dirPath); // get the file

                if (file.exists () && file.isDirectory ()) {// is determined, and whether the presence of no folders

                    ta.setText ( ""); // if eligible, then empty the previous data;

                    String[] names = file.list();

                    for (String name : names) {

                        ta.append(name + "\r\n");

                    }

                    System.out.println("=======");

                } else {

                    ta.setText("");

                    ta.append ( "I'm sorry, make sure you enter the path!");

                }

               

                System.out.println(e.getKeyCode());

            }

        });

       

        tf.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                String dirPath = tf.getText (); // Get the text (we want to verify that the path), then get the file

                File file = new File (dirPath); // get the file

                if (file.exists () && file.isDirectory ()) {// is determined, and whether the presence of no folders

                    ta.setText ( ""); // if eligible, then empty the previous data;

                    String[] names = file.list();

                    for (String name : names) {

                        ta.append(name + "\r\n");

                    }

                    System.out.println("=======");

                } else {

                    ta.setText("");

                    ta.append ( "I'm sorry, make sure you enter the path!");

                }

            }

        });

    }

   

    void buttonAction() {

        b.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                String dirPath = tf.getText (); // Get the text (we want to verify that the path), then get the file

                File file = new File (dirPath); // get the file

                if (file.exists () && file.isDirectory ()) {// is determined, and whether the presence of no folders

                    ta.setText ( ""); // if eligible, then empty the previous data;

                    String[] names = file.list();

                    for (String name : names) {

                        ta.append(name + "\r\n");

                    }

                    System.out.println("=======");

                } else {

                    ta.setText("");

                    ta.append ( "I'm sorry, make sure you enter the path!");

                }

            }

        });

    }

}

 

public class FrameDemo3 {

    public static void main(String[] args) {

        new MyFrame();

    }

}

Guess you like

Origin www.cnblogs.com/fanweisheng/p/11137616.html