首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

请问下各位,如何用内部类做监视器?

2012-02-23 
请教下各位,怎么用内部类做监视器???最好有个例子,谢谢哈~~~还有我这个怎么监视器没起作用啊。。。。。Java cod

请教下各位,怎么用内部类做监视器???
最好有个例子,谢谢哈~~~
还有我这个怎么监视器没起作用啊。。。。。

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里面讲得很详细

热点排行