关于内部类的问题

hxshandle 2004-05-12 06:02:26
我在书上看到一个内部类的例子于是我在eclipse中调试了以下,可是例子连编译都没通过,(没有打错),贴出代码让大家指正:
public class TwoListenerInner extends MouseMotionAdapter {
private Frame f;
private TextField tf;
// MouseMotionHandler为一个内部类
public class MouseMotionListener extends MouseMotionAdapter{
public void mouseDragged(MouseEvent e){
String s="Mouse dragging : X="+e.getX()+"Y="+e.getY();
tf.setText(s);
}
}
//MouseEventHandle为一个内部类
public class MouseEventHandler extends MouseAdapter{
public void MouseEntered(MouseEvent e){
String s="The mouse entered";
tf.setText(s);
}
}
public void mouseExited(MouseEvent e){
String s="The mouse has left the building";
tf.setText(s);
}
public static void main(String[] args) {
TwoListenerInner that=new TwoListenerInner();
that.go();
}
public void go(){
f=new Frame("Two listeners example");
f.add("North",new Label("Click and drag the mouse"));
tf=new TextField(10);
f.addMouseMotionListener(new MoseMotionHandler());
f.addMouseListener(new MousrEventHandler());
f.setSize(300,200);
f.setVisible(true);
}

}
...全文
47 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
zgqallan 2004-05-12
  • 打赏
  • 举报
回复
内部类对象的创建要通过外部类对象

go()中的f.addMouseMotionListener(new MoseMotionHandler());
f.addMouseListener(new MousrEventHandler());

应改为 f.addMouseMotionListener(this.new MouseMotionHandler());
f.addMouseListener(this.new MouseEventHandler());

你的程序中还有好几处错误 我改了一下

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class TwoListenerInner extends MouseMotionAdapter {
private Frame f;
private TextField tf;
// MouseMotionHandler为一个内部类
public class MouseMotionHandler extends MouseMotionAdapter{
public void mouseDragged(MouseEvent e){
String s="Mouse dragging : X="+e.getX()+"Y="+e.getY();
tf.setText(s);
}
}
//MouseEventHandle为一个内部类
public class MouseEventHandler extends MouseAdapter{
public void MouseEntered(MouseEvent e){
String s="The mouse entered";
tf.setText(s);
}
}
public void mouseExited(MouseEvent e){
String s="The mouse has left the building";
tf.setText(s);
}
public static void main(String[] args) {
TwoListenerInner that=new TwoListenerInner();
that.go();
}
public void go(){
f=new Frame("Two listeners example");
f.add("North",new Label("Click and drag the mouse"));
tf=new TextField(10);
f.addMouseMotionListener(this.new MouseMotionHandler());
f.addMouseListener(this.new MouseEventHandler());
f.setSize(300,200);
f.setVisible(true);
}

}

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧