2

我一直在尝试实现鼠标运动事件调度,但我继续收到堆栈溢出错误。它适用于 mouseMoved(MouseEvent e) 方法,但不适用于 mouseDragged(MouseEvent e)。有人知道为什么吗?有什么解决办法吗?

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Test extends JFrame {

public Test() { 
    setLayout(null);
    setSize(500,500);
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    addMouseMotionListener(new MouseMotionListener() {
         public void mouseMoved(MouseEvent e) {
             System.err.println("moved outside");
         }
         public void mouseDragged(MouseEvent e) {
            System.err.println("dragged outside");
         }
    });

    JPanel inside = new JPanel();
    inside.setLocation(0, 0);
    inside.setSize(100, 100);
    inside.setBackground(Color.RED);

    inside.addMouseMotionListener(new MouseMotionListener() {
        public void mouseDragged(MouseEvent e) {
            System.out.println("dragged inside");

            //The error occurs here when I begin dragging 
            //here and continue dragging to any other location.

            Test.this.dispatchEvent(e);
        }
        public void mouseMoved(MouseEvent e) {
            System.out.println("moved inside");
            Test.this.dispatchEvent(e);
        }
    });
    add(inside);
}

public static void main(String[] args) {
    Test client = new Test();
}
}

我的实际项目使用了许多内部组件,我的目标是让每个组件实现自己的鼠标按下/单击/释放操作,并让框架处理影响所有组件的鼠标运动和拖动。

这是适用于两种鼠标运动方法的类似代码。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Test2 {

public static void main(String... args) {
    SwingUtilities.invokeLater(new Runnable() {

        public void run() {
            final JComponent outside = new JPanel();
            JComponent inside = new JPanel();
            inside.setBackground(Color.red);
            inside.setPreferredSize(new Dimension(200, 200));
            inside.addMouseMotionListener(new MouseAdapter() {
                public void mouseDragged(MouseEvent e) {
                    System.out.println("dragged inside");
                    outside.dispatchEvent(e);
                }
                public void mouseMoved(MouseEvent e) {
                    System.out.println("moved inside");
                    outside.dispatchEvent(e);
                }
            });

            outside.add(inside);
            outside.setPreferredSize(new Dimension(300, 300));
            outside.addMouseMotionListener(new MouseAdapter() {
                public void mouseMoved(MouseEvent e) {
                    System.err.println("moved outside");
                }
                public void mouseDragged(MouseEvent e) {
                    System.err.println("dragged outside");
                }
            });

            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(outside);
            frame.pack();
            frame.setVisible(true);
        }
    });
}
}

帮助表示赞赏。

4

1 回答 1

1

MouseMotionListener正在inside生成一个新事件。该事件将再次被相同的 捕获MouseMotionListener,从而创建一个无限循环。由于您正在创建一个事件,而前一个事件仍未完成,因此它们将堆积起来,直到StackOverflowError您的应用程序爆炸。

您的第二个代码没有这个问题,因为inside委托给outside它并在那里完成。

于 2013-06-28T02:40:13.793 回答