保持一个的MouseListener始终运行 [英] Keeping a MouseListener always running

查看:281
本文介绍了保持一个的MouseListener始终运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的构造:

 公板(最终布尔[] []板){
    this.board =板;
    高度= board.length;
    宽度=板[0]。长度;
    的setBackground(Color.black);
    BUTTON1 =的新的JButton(运行);
    添加(按钮1);
    button1.addActionListener(新的ActionListener(){
        公共无效的actionPerformed(ActionEvent的五){
            isActive = isActive!;
            button1.setText(isActive暂停:运行?);
        }
    });
    按钮2 =的新的JButton(随机);
    添加(按钮2);
    button2.addActionListener(新的ActionListener(){
        公共无效的actionPerformed(ActionEvent的五){
            setBoard(randomBoard());
        }
    });
    按钮3 =的新的JButton(清除);
    添加(按钮3);
    button3.addActionListener(新的ActionListener(){
        公共无效的actionPerformed(ActionEvent的五){
            setBoard(clearBoard());
        }
    });
    addMouseListener将(新的MouseListener(){
        @覆盖
        公共无效的mouseClicked(的MouseEvent E){
        }        @覆盖
        公共无效的mouseEntered(的MouseEvent E){
        }        @覆盖
        公共无效的mouseExited(的MouseEvent E){
        }        @覆盖
        公共无效鼠标pressed(的MouseEvent E){
            板[e.getY()/乘数] [e.getX()/乘数=板[e.getY()/乘数] [e.getX()/乘数]!;
        }        @覆盖
        公共无效的mouseReleased(的MouseEvent E){
        }
    });
}

的ActionListener s的总是'听';然而的MouseListener 停止'听'我点击运行后(按钮1 )。这是为什么?如何让的MouseListener 留在听吗?

如果它的任何使用,我也有这个的paintComponent 类:

  @覆盖
公共无效的paintComponent(图形G){
    super.paintComponent方法(G);
    的for(int i = 0; I<高度;我++){
        对于(INT J = 0; J<宽度; J ++){
            g.setColor(板[I] [J] Color.green:Color.gray);
            g.fillRect(J *乘数,我*倍频,倍频 - 1,乘数 - 1);
        }
    }
    如果(isActive){
        timer.start();
    }
    其他{
        timer.stop();
        重绘();
    }
}


解决方案

A 的MouseListener 将继续作为长期工作,因为你加入的对象它仍然活着,并假设你不叫 removeMouseListener()就可以了。当你的程序运行和变化的数据和这样的,你的听众里面的code的行为可能会改变(例如,一个标志设置使其忽略另一个方法的调用),但听者将始终运行其方法的将会的调用。

(正如我在我的评论中提到,你的问题很可能与你在你的的paintComponent()方法做奇怪的事情要做。)

I have this constructor:

public Board(final boolean[][] board) {
    this.board = board;
    height = board.length;
    width = board[0].length;
    setBackground(Color.black);
    button1 = new JButton("Run");
    add(button1);
    button1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            isActive = !isActive;
            button1.setText(isActive ? "Pause" : "Run");
        }
    });
    button2 = new JButton("Random");
    add(button2);
    button2.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            setBoard(randomBoard());
        }
    });
    button3 = new JButton("Clear");
    add(button3);
    button3.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            setBoard(clearBoard());
        }
    });
    addMouseListener(new MouseListener() {
        @Override
        public void mouseClicked(MouseEvent e) {
        }

        @Override
        public void mouseEntered(MouseEvent e) {
        }

        @Override
        public void mouseExited(MouseEvent e) {         
        }

        @Override
        public void mousePressed(MouseEvent e) {
            board[e.getY() / multiplier][e.getX() / multiplier] = !board[e.getY() / multiplier][e.getX() / multiplier];
        }

        @Override
        public void mouseReleased(MouseEvent e) {       
        }
    });
}

The ActionListeners are always 'listening'; however the MouseListenerstops 'listening' after I click Run (button1). Why is this and how do I make MouseListener remain listening?

If it's any use, I also have this paintComponent class:

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    for (int i = 0; i < height; i++) {
        for (int j = 0; j < width; j++) {
            g.setColor(board[i][j] ? Color.green : Color.gray);
            g.fillRect(j * multiplier, i * multiplier, multiplier - 1, multiplier - 1);
        }
    }
    if (isActive) {
        timer.start();
    }
    else {
        timer.stop();
        repaint();
    }
}

解决方案

A MouseListener will continue to work as long as the object you added it to is still alive and assuming you haven't called removeMouseListener() on it. As your program runs and changes data and such, the behavior of the code inside your listener may change (e.g., a flag is set causing it to ignore a call to another method), but the listener will be "always running" and its methods will be called.

(As I mentioned in my comment, your problem likely has to do with the strange things you are doing in your paintComponent() method.)

这篇关于保持一个的MouseListener始终运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆