[java]代码库
import java.awt.*;
import java.awt.event.*;
public class InnerClass {
private Frame f;
private TextField tf;
public InnerClass() {
f = new Frame("Inner classes example");
tf = new TextField(30);
}
public void launchFrame() {
Label label = new Label("Click and drag the mouse");
f.add(label, BorderLayout.NORTH);
f.add(tf, BorderLayout.SOUTH);
f.addMouseMotionListener(new MyMouseMotionListener());/* 参数为内部类对象 */
f.setSize(300, 200);
f.setVisible(true);
}
class MyMouseMotionListener extends MouseMotionAdapter { /* 内部类开始 */
public void mouseDragged(MouseEvent e) {
String s = "Mouse dragging: x=" + e.getX() + "Y=" + e.getY();
tf.setText(s);
}
};
public static void main(String args[]) {
InnerClass obj = new InnerClass();
obj.launchFrame();
}
}