繁体   English   中英

removeMouseListener()将不起作用(Java)

[英]removeMouseListener() won't work (Java)

我在应该做的简单任务中遇到问题。 下列类表示带有图片的JPanel。 每次我通过拖放来绘制形状后,我都希望鼠标对框架/面板/组件无响应。 我正在尝试通过以各种可能的方式删除mouselistener来做到这一点,正如您在方法mouseReleased(...)中所看到的那样,但是发生的事情是,当我完成绘制形状时,鼠标会继续响应并且每次按下框架上的按钮会继续绘制形状(逻辑有些缺陷)。

如何删除mouselistener,以便在addShape()方法完成后可以单击并使用鼠标做我想做的一切? 谢谢! 包问题2;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

@SuppressWarnings("serial")
public class PicturePanel extends JPanel { // TODO
    static final int NONE = -1,
            LINES_ONLY = 0, BOUNDED_SHAPES = 1, ALL_SHAPES = 2,
            RECTANGLE = 0, OVAL = 1, LINE = 2,
            DELETION = 11;

    private int shapesMode;
    private int actionMode;
    private Picture<Shape> picture;
    private JPanel controls;
    private JButton addShapeBtn, removeShapeBtn;

    private Shape tempShape; // for drawing the picture by dragging
    private question2.Point startDrag, endDrag;


    public PicturePanel(int shapesMode) {
        this.shapesMode = shapesMode;
        picture = new Picture<Shape>();

        addShapeBtn = new JButton("Add shape");
        removeShapeBtn = new JButton("Remove shape");

        ControlsListener l = new ControlsListener();
        addShapeBtn.addActionListener(l);
        removeShapeBtn.addActionListener(l);

        controls = new JPanel();
        controls.add(addShapeBtn);
        controls.add(removeShapeBtn);

        this.setLayout(new BorderLayout());
        this.add(controls, BorderLayout.SOUTH);
        tempShape = new Line(0, 0, 1000, 100, Color.black);
        picture.add(tempShape);

    }

    private class MouseListener extends MouseAdapter implements MouseMotionListener {
        public void mouseClicked(MouseEvent e) {
            switch (actionMode) {
                case DELETION:
                    question2.Point clickPosition = new question2.Point(e.getX(), e.getY());
                    picture.remove(clickPosition);
                    PicturePanel.this.removeMouseListener(this);

                    repaint();
                    actionMode = NONE;
                    break;

            }
            question2.Point clickPosition = new question2.Point(e.getX(), e.getY());
            picture.remove(clickPosition);
            PicturePanel.this.removeMouseListener(this);

            repaint();
        }

    }
    private class ControlsListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == addShapeBtn) {
                if (shapesMode == LINES_ONLY) {
                    addShape(LINE);
                } else if (shapesMode == BOUNDED_SHAPES) {
                    addShape(chooseBoundedShape(PicturePanel.this));
                } else {
                    addShape(chooseAnyShape(PicturePanel.this));
                }

            } else if (e.getSource() == removeShapeBtn) {
                removeShape();
            }

        }
    }

    private void addShape(int shapeType) {

        this.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                startDrag = new question2.Point(e.getX(), e.getY());
                endDrag = startDrag;
                repaint();

            }

            public void mouseReleased(MouseEvent e) {
                picture.add(tempShape);

                removeMouseMotionListener(this);
                for (java.awt.event.MouseListener m: PicturePanel.this.getMouseListeners()){
                    PicturePanel.this.removeMouseListener(m);
                }
                repaint();
            }

        });

        this.addMouseMotionListener(new MouseMotionAdapter() {
            public void mouseDragged(MouseEvent e) {
                PicturePanel.this.repaint();

                endDrag = new question2.Point(e.getX(), e.getY());
                switch (shapeType) {
                    case LINE:
                        tempShape =
                                new Line(startDrag.getX(), startDrag.getY(), endDrag.getX(),
                                        endDrag.getY(), new Color(((int) (Math.random() * 255)), ((int) (Math.random() * 255)),
                                                ((int) (Math.random() * 255))));

                        break;
                    case OVAL:
                        tempShape =
                                new Oval(Math.min(startDrag.getX(), endDrag.getX()),
                                        Math.min(startDrag.getY(), endDrag.getY()),
                                        Math.abs(endDrag.getX() - startDrag.getX()),
                                        Math.abs(endDrag.getY() - startDrag.getY()),
                                        new Color(((int) (Math.random() * 255)), ((int) (Math.random() * 255)), ((int) (Math.random() * 255))), false);
                        break;
                    case RECTANGLE:
                        tempShape =
                                new Rectangle(Math.min(startDrag.getX(), endDrag.getX()),
                                        Math.min(startDrag.getY(), endDrag.getY()),
                                        Math.abs(endDrag.getX() - startDrag.getX()),
                                        Math.abs(endDrag.getY() - startDrag.getY()),
                                        new Color(((int) (Math.random() * 255)), ((int) (Math.random() * 255)), ((int) (Math.random() * 255))), false);
                        break;
                }
                repaint();
            }
        });

    }

    private void removeShape() {
        System.out.println("click on shape to remove");
        MouseListener ml = new MouseListener();
        PicturePanel.this.addMouseListener(ml);

    }

    public Picture<Shape> getPicture() {
        return picture;
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        picture.show(g);
        tempShape.draw(g);
    }

    public static int chooseShapesType(Component parent) {
        int choice = JOptionPane.showOptionDialog(parent, "Choose shape type", null, 0, JOptionPane.QUESTION_MESSAGE
                , null, new String[] {"Lines only", "Bounded shapes", "All shapes"}, null);
        if (choice == 0)
            return LINES_ONLY;
        else if (choice == 1)
            return BOUNDED_SHAPES;
        else if (choice == 2)
            return ALL_SHAPES;

        return NONE;

    }

    private static int chooseBoundedShape(Component parent) {
        int choice = JOptionPane.showOptionDialog(parent, "Choose shape type", null, 0, JOptionPane.QUESTION_MESSAGE
                , null, new String[] {"Rectangle", "Oval"}, null);
        if (choice == 0)
            return RECTANGLE;
        else if (choice == 1)
            return OVAL;

        return NONE;
    }

    private static int chooseAnyShape(Component parent) {
        int choice = JOptionPane.showOptionDialog(parent, "Choose shape type", null, 0, JOptionPane.QUESTION_MESSAGE
                , null, new String[] {"Rectangle", "Oval", "Line"}, null);
        if (choice == 0)
            return RECTANGLE;
        else if (choice == 1)
            return OVAL;
        else if (choice == 2)
            return LINE;

        return NONE;
    }


}

让我们不要谈谈您的解决方案是否是一种好的做法,而只是解决您的问题。

我注意到,您实现了2个侦听器类。 1) MouseListener 2) ControlsListener 仅在MouseListener实现中完成侦听器的删除,而在ControlsListener中则不会删除。

PicturePanel尽管您使用ControlsListener但从未使用过MouseListener ,但它从未将其自身移除,如上所述。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM