JavaSE 内部类(InnerClass)实现

{相关信息}
内部类(Inner Class)是被定义在一个类内部的类,内部类的特点如下:
1)内部类的对象具有外部类变量和方法的访问权限,包括私有成员
2)实现事件监听器的功能时,采用内部类、匿名类实现相当容易
所以,内部类在 AWT 的事件处理机制中应用比较广泛

{内部类使用实例}

JavaSE 内部类(InnerClass)实现

package sup.orange.learn;



import java.awt.*;

import java.awt.event.MouseEvent;

import java.awt.event.MouseMotionAdapter;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import java.awt.print.Printable;



/**

 * Created by re-x on 11/2/14.

 */

public class ListenerInnerClass {



    private Frame f;

    private TextField tf;



    public ListenerInnerClass() {

        f = new Frame("click, move mouse");

        tf = new TextField(30);

    }



    public static void main(String[] args) {

        ListenerInnerClass li = new ListenerInnerClass();

        li.show();

    }



    public void show() {

        f.add("North", tf);

        f.addMouseMotionListener(new MyMouseMotionListener());

        f.setSize(300, 240);

        f.addWindowListener(new WindowAdapter() {

            @Override

            public void windowClosing(WindowEvent e) {

                super.windowClosing(e);

                f.setVisible(false);

                f.dispose();

                System.out.println("quit");

                System.exit(0);

            }

        });



        f.setVisible(true);

    }



    class MyMouseMotionListener extends MouseMotionAdapter {

        public void mouseMoved(MouseEvent e) {

            tf.setText("x= "+e.getX()+", y= "+e.getY());

        }

    }

}

 

你可能感兴趣的:(JavaSE)