Java只有addMouseListener一次

时间:2014-10-31 23:16:13

标签: java swing mouselistener

我的程序中有一个组合框,有三个选项CIRCLE,RECTANGLE,FREEHAND。每个选项都连接到mouselistener。如果我在三个选项之间切换,那么mouselisteners会给我带来一些问题。因此,我想只添加一次mouselistener(例如在构造函数中或方法的开头,或其他地方)。它是否可能,代码如何? 如果不可能,还有其他方法可以解决吗?

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();                       
                    }
                });    
            }                    

        }

2 个答案:

答案 0 :(得分:2)

每次在组合框中进行选择时,不应向面板添加不同的侦听器,而应在构造函数中向面板添加单个鼠标侦听器。当调用其中一个方法时,此侦听器应首先检查选择了哪个选项,并相应地采取行动(即绘制一个矩形,一个圆圈,或者根据选择自由地绘制)。

答案 1 :(得分:1)

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

            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);