日期:2014-05-20 浏览次数:20682 次
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()); } }
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里面讲得很详细