最好有个例子,谢谢哈~~~
还有我这个怎么监视器没起作用啊。。。。。
- Java code
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()); }}
------解决方案--------------------
你这一段不就是用匿名内部类做监听器么?
- Java code
addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } });
------解决方案--------------------
参看http://blog.csdn.net/gaowen_han/article/details/7170918以及http://blog.csdn.net/gaowen_han/article/details/7163755里面讲得很详细