MouseListener在运行时中开始提供错误

时间:2016-04-01 18:14:59

标签: java swing jpanel mouselistener

我目前正在制作一个涉及通过JPanel制作的按钮的游戏。但是,在程序运行期间的某个时刻,只要鼠标事件以下列形式注册,用于制作按钮的JPanel就会开始出现常量错误:

at java.awt.AWTEventMultiCaster.mouse[eventName](UnknownSource)

它走得太快以至于我看不到初始错误,并且错误仅在运行时中期的某个时刻开始。有人可以帮帮我吗?以下是我的JPanel的代码:

JPanel trackPanel = new JPanel() {
        int state = 0;
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponents(g);

            addMouseListener(new MouseAdapter() {
                public void mouseEntered(MouseEvent e) {
                    if (dragMe == null) {
                        state = 1;
                    }
                }
                public void mouseExited(MouseEvent e) {
                    if (dragMe == null) state = 0;
                    else state = 1;
                }
                public void mousePressed(MouseEvent e) {
                    if (dragMe == null && !e.isMetaDown())
                    {
                        dragMe = new DragFrameThread(vTrack);
                        dragMe.start();
                    }
                    state = 2;
                }
                public void mouseReleased(MouseEvent e) {
                    if (state == 2 && e.isMetaDown()) System.exit(0);
                    if (dragMe != null) {
                        dragMe.interrupt();
                        dragMe = null;
                        state = 1;
                    }
                }
            });

            g.setColor(new Color(0, 127, 127, 255));
            g.drawRect(0, 0, getWidth()-1, getHeight()-1);
            g.setColor(new Color(0, 127, 127, 223));
            g.drawRect(1, 1, getWidth()-3, getHeight()-3);
            g.setColor(new Color(0, 127, 127, 191));
            g.drawRect(2, 2, getWidth()-5, getHeight()-5);
            g.setColor(new Color(0, 127, 127, 159));
            g.fillRect(3, 3, getWidth()-6, getHeight()-6);

            g.drawImage(BootAssets.VTScaledLogo, 0, 0, getWidth(), getHeight(), null);

            if (state == 1) g.setColor(new Color(255, 255, 255, 127));
            if (state == 2) g.setColor(new Color(0, 0, 0, 127));
            if (state != 0) g.fillRect(0, 0, getWidth(), getHeight());

            repaint();
        }
    };

非常感谢! 〜帕拉

1 个答案:

答案 0 :(得分:0)

我认为问题是在绘制窗口小部件时会调用paintComponent方法。在每次绘制调用时添加鼠标侦听器都是错误的。

将addMouseListener移出paintComponent方法,并将其单独添加到trackPanel。您将需要重构代码,因为它当前具有状态集并以JPanel的定义方式进行更新。

       private final AtomicInteger state = new AtomicInteger(0);
       JPanel trackPanel = new JPanel() {
        @Override
        protected void paintComponent(Graphics g)
        {
            super.paintComponent(g);

            g.setColor(new Color(0, 127, 127, 255));
            g.drawRect(0, 0, getWidth()-1, getHeight()-1);
            g.setColor(new Color(0, 127, 127, 223));
            g.drawRect(1, 1, getWidth()-3, getHeight()-3);
            g.setColor(new Color(0, 127, 127, 191));
            g.drawRect(2, 2, getWidth()-5, getHeight()-5);
            g.setColor(new Color(0, 127, 127, 159));
            g.fillRect(3, 3, getWidth()-6, getHeight()-6);

            g.drawImage(BootAssets.VTScaledLogo, 0, 0, getWidth(), getHeight(), null);

            if (state.get() == 1) g.setColor(new Color(255, 255, 255, 127));
            if (state.get() == 2) g.setColor(new Color(0, 0, 0, 127));
            if (state.get() != 0) g.fillRect(0, 0, getWidth(), getHeight());                
        }
    };

    trackPanel.addMouseListener(new MouseAdapter() {
        public void mouseEntered(MouseEvent e) {
            if (dragMe == null) {
                state.set(1);
            }
        }
        public void mouseExited(MouseEvent e) {
            if (dragMe == null) state.set(0);
            else state.set(1);
        }
        public void mousePressed(MouseEvent e) {
            if (dragMe == null && !e.isMetaDown())
            {
                dragMe = new DragFrameThread(vTrack);
                dragMe.start();
            }
            state.set(2);
        }
        public void mouseReleased(MouseEvent e) {
            if (state.get() == 2 && e.isMetaDown()) System.exit(0);
            if (dragMe != null) {
                dragMe.interrupt();
                dragMe = null;
                state.set(1);
            }
        }
    });