Java仅一次addMouseListener [英] Java only addMouseListener once

查看:222
本文介绍了Java仅一次addMouseListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序中有一个组合框,其中包含三个选项:CIRCLE,RECTANGLE和FREEHAND.每个选项都连接到鼠标监听器.如果我将这三个选项夹在中间,鼠标监听器会给我带来一些问题.因此,我只想添加一次mouselistener(例如,在构造函数中,方法的开头或其他位置).甚至有可能,代码看起来如何? 如果不可能,我还有其他方法可以解决吗?

I have a combobox in my program, with three options CIRCLE, RECTANGLE, FREEHAND. Each option is connected to a mouselistener. If I swich between the three options the mouselisteners are causing me some problems. Therefore I would like to add a mouselistener only once (for example in the constructor or in the beginning of the method, or somewhere else). Is it even possible, and how would the code look like? If it is not possible, is there any other way I can solve it?

public void actionPerformed(ActionEvent e) {      

         if (e.getSource().equals(comboBox)) {            

            JComboBox cb = (JComboBox)e.getSource();

                if (cb.getSelectedItem().equals("Rectangle")) {                
                contentPane.addMouseListener(new MouseAdapter() {       //First mouseListener     
                    @Override
                    public void mousePressed(MouseEvent e) {  
                        startX = e.getX();     
                        startY = e.getY(); 
                    }
                    @Override
                    public void mouseReleased(MouseEvent e) {
                        endX = e.getX();
                        endY = e.getY();

                        int width = startX - endX;
                        int height = startY - endY;
                        w = Math.abs(width);
                        h = Math.abs(height);

                        Rectangle r =  new Rectangle(startX, startY, w, h, pickedColor, thickness);
                        shapeList.add(r);
                        repaint(); 
                    }                    
                });   

            }
            else if (cb.getSelectedItem().equals("Circle")) {

                contentPane.addMouseListener(new MouseAdapter() {            //Second
                    @Override
                    public void mousePressed(MouseEvent e) {  
                        startX = e.getX();     
                        startY = e.getY();
                    }
                    @Override
                    public void mouseReleased(MouseEvent e) {
                        endX = e.getX();
                        endY = e.getY();

                        int width = startX - endX;
                        int height = startY - endY;
                        w = Math.abs(width);
                        h = Math.abs(height);

                        Circle c =  new Circle(startX, startY, w, h, pickedColor, thickness);

                        shapeList.add(c);
                        repaint();
                    }
                });  
            }
            else if (cb.getSelectedItem().equals("Freehand")) {   

                contentPane.addMouseListener(new MouseAdapter() {            //Third
                    @Override
                    public void mousePressed(MouseEvent e) {            
                        startX = e.getX();
                        startY = e.getY();
                    }
                    @Override
                    public void mouseDragged(MouseEvent e) {                       

                        FreeHand fh =  new FreeHand(startX, startY, e.getX(), e.getY(), pickedColor, thickness);

                        shapeList.add(fh);
                        repaint();                       
                    }
                });    
            }                    

        }

推荐答案

最简单的解决方案是存储最后一个监听器,并在添加新监听器之前将其删除:

The simplest solution would be to store the last listener and remove that before adding a new one:

            private MouseListener lastListener;
            // ...

            JComboBox cb = (JComboBox)e.getSource();

            if (cb.getSelectedItem().equals("Rectangle")) {         
            if (lastListener != null)
                contentPane.removeMouseListener(lastListener);
            lastListener = new MouseAdapter() {       //First mouseListener     
                // ...
            };
            contentPane.addMouseListener(lastListener);

这篇关于Java仅一次addMouseListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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