| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 433 人关注过本帖
标题:大家看看,为什么不能用new Threelistener(),而必须用this
只看楼主 加入收藏
xw2014
Rank: 1
等 级:新手上路
帖 子:54
专家分:0
注 册:2006-1-17
收藏
 问题点数:0 回复次数:1 
大家看看,为什么不能用new Threelistener(),而必须用this

这个程序红色部分是有问题的,只要把它改成this就可以,为什么会这样?
import java.awt.*;
import java.awt.event.*;
public class Threelistener implements MouseMotionListener,MouseListener,WindowListener {
//实现了三个接口
private Frame f;
private TextField tf;
public static void main(String args[])
{
Threelistener two = new Threelistener();
two.go(); }
public void go() {
f = new Frame("Three listeners example");
f.add(new Label("Click and drag the mouse"),"North");
tf = new TextField(30);
f.add(tf,"South"); //使用缺省的布局管理器
f.addMouseMotionListener(new Threelistener()); //注册监听器MouseMotionListener
f.addMouseListener(new Threelistener()); //注册监听器MouseListener
f.addWindowListener(new Threelistener()); //注册监听器WindowListener
f.setSize(300,200);
f.setVisible(true);
}
public void mouseDragged (MouseEvent e) {
//实现mouseDragged方法
String s = "Mouse dragging : X="+e.getX()+"Y = "+e.getY();
tf.setText(s);
}
public void mouseMoved(MouseEvent e){}
//对其不感兴趣的方法可以方法体为空
public void mouseClicked(MouseEvent e){}
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 void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){ }
public void windowClosing(WindowEvent e) {
//为了使窗口能正常关闭,程序正常退出,需要实现windowClosing方法
System.exit(0);
}
public void windowOpened(WindowEvent e) {}
//对其不感兴趣的方法可以方法体为空
public void windowIconified(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
public void windowActivated(WindowEvent e) { }
public void windowDeactivated(WindowEvent e) {}
}

搜索更多相关主题的帖子: new 
2007-05-15 13:50
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 

因为你new的ThreeListener里面的东西都是空的


private Frame f;
private TextField tf;

你能保证这些被初始化吗?
但是用this,就代表当前对象


可惜不是你,陪我到最后
2007-05-15 16:27
快速回复:大家看看,为什么不能用new Threelistener(),而必须用this
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018034 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved