最好有个例子,谢谢哈~~~
还有我这个怎么监视器没起作用啊。。import java.awt.*;
import java.awt.event.*;public class Test {
public static void main(String args[]) {
new Win();
}
}class Win extends Frame {
Button button;
TextField textf;
TextArea texta;
MouseL ml;
Win() {
setLayout(new FlowLayout());
button = new Button("Button");
textf = new TextField(8);
texta = new TextArea();
button.addMouseListener(ml);
textf.addMouseListener(ml);
texta.addMouseListener(ml);
add(button);
add(textf);
add(texta);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
pack();
setVisible(true);
validate();
}
}class MouseL extends MouseAdapter {
Win win;
public MouseL(Win win) {
this.win = win;
}

public void mousePressed(MouseEvent e) {
/*
if(e.getSource() == win.button) {
win.texta.append("mouse clicked on Button. Position: " + "(" + e.getX() + "," + e.getY() + ").");
}
if(e.getSource() == win.textf) {
win.texta.append("mouse clicked on TextField. Position: " + "(" + e.getX() + "," + e.getY() + ").");
}
if(e.getSource() == win.texta) {
win.texta.append("mouse clicked on TextArea. Position: " + "(" + e.getX() + "," + e.getY() + ").");
}
*/
System.out.println(e.getSource());
}
}

解决方案 »

  1.   

     你这一段不就是用匿名内部类做监听器么?addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });至于ml不起作用 ,是因为你只声明,并没有在Win的构造方法中初始化,你在addMouseListener之前加上这句:
    ml = new MouseL(this);
      

  2.   

    忘了一句:没有初始化的ml就是null,你可以打印看看,所以你的ml不起作用。
      

  3.   

    参看http://blog.csdn.net/gaowen_han/article/details/7170918以及http://blog.csdn.net/gaowen_han/article/details/7163755里面讲得很详细