鼠标听者不听

mouse listener does not listen

我有一个学校作业,我一直坚持。

我的目标是 MVC 实现,但现在我只是在视图中完成所有操作(暂时只是为了让事情变得简单)。

所以 - 我有一个框架,它有一个面板。

面板有一个形状列表。每当用户按下 addLine/addRect 按钮时 - 将引发一个事件并将 line/rect 添加到此列表中。

paintComponent 函数绘制列表中的所有形状(所有形状都知道如何绘制自己)。

到目前为止一切顺利 - 有效!

此作业中唯一的其他要求是,每当用户单击绘图区域中的一个点时,所有包含该点的形状都会被删除。 每个形状都有自己的 Contains(p) 函数。 所以我决定在面板上添加一个 MouseListener,它将获取点击的 X、Y 坐标,并从形状列表中删除相关形状。

我不知道这是否是个好主意,但这不是我目前的问题。

我的问题是 MouseListener 不响应点击 - 我知道这是因为我在 mouseClicked 的实现内部有一个断点,而调试器从未到达该断点。

我的问题是为什么?

这是我的代码:它还有一些我需要解决的问题,但现在它们与我无关

//MyFrame.java

public class MyFrame extends JFrame {
    private MyJPanel panel ;

    public MyFrame() throws MyShape.IllegalArgumentException {
        initUI();
    }

    private void initUI() throws IllegalArgumentException {
        setLayout(new FlowLayout());
        panel = new MyJPanel();
        add(panel);
        setTitle("Shapes Editor");
        setSize(600, 600);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.pack();
        setVisible(true);
    }
}

//MyPanel.java

public class MyJPanel extends JPanel {

    ArrayList<MyShapeAbstract> pic;
    DrawingArea drawingArea;
    ButtonsPanel buttonsPanel;

    public ButtonsPanel getButtonsPanel() {
        return buttonsPanel;
    }
    public void setButtonsPanel(ButtonsPanel buttonsPanel) {
        this.buttonsPanel = buttonsPanel;
    }

    public ArrayList<MyShapeAbstract> getPic() {
        return pic;
    }
    public void setPic(ArrayList<MyShapeAbstract> pic) {
        this.pic = pic;
    }

    public MyJPanel() throws MyShape.IllegalArgumentException {
        initUI();
    }

    private void initUI() throws MyShape.IllegalArgumentException {
        setLayout(new FlowLayout());
        pic = new ArrayList<MyShapeAbstract>();
        add(buttonsPanel = new ButtonsPanel(this));
        add(drawingArea = new DrawingArea(this));
        drawingArea.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                Point p = null;
                try {
                    p = new Point(e.getX(), e.getY());
                } catch (IllegalArgumentException e1) {
                    e1.printStackTrace();
                }
                Iterator<MyShapeAbstract> iter = pic.iterator();
                while (iter.hasNext()){
                    MyShapeAbstract shape = iter.next();
                    if(shape.contains(p))
                    {
                        iter.remove();
                    }
                }
                drawingArea.repaint();
                pic.clear();
                repaint();
            }
        });

        JButton addLineButton = buttonsPanel.getAddLineButton();
        addLineButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    addLine();
                    drawingArea.repaint();
                    repaint();
                } catch (IllegalArgumentException e1) {
                    e1.printStackTrace();
                }
            }
        });
        add(addLineButton);

        JButton addRectButton = buttonsPanel.getAddRectButton();
        addRectButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    addRect();
                } catch (IllegalArgumentException e1) {
                    e1.printStackTrace();
                }
                drawingArea.repaint();
                repaint();
            }
        });
        add(addRectButton);
    }

    @Override
    public void repaint() {
        Graphics g = getGraphics();
        if(g!=null && pic != null)
            for(MyShapeAbstract shape : pic){
                shape.draw(g);
            }
    }

    private void addRect() throws IllegalArgumentException {
        Random r = new Random();
        pic.add(new MyRectangle(new Point(r.nextInt(200), r.nextInt(200)), new Point(r.nextInt(200),r.nextInt(200))));
    }
    private void addLine() throws IllegalArgumentException {
        Random r = new Random();
        pic.add(new MyLine(new Point(r.nextInt(200), r.nextInt(200)), new Point(r.nextInt(200),r.nextInt(200))));
    }
}

class DrawingArea extends JPanel{
    public DrawingArea(MyJPanel parent) {
        this.parent = parent;
        setLayout(new FlowLayout());
    }

    @Override
    public void repaint() {
        Graphics g = getGraphics();
        if(g!=null && parent.getPic() != null)
            for(MyShapeAbstract shape : parent.getPic()){
                shape.draw(g);
            }
    }

    MyJPanel parent;
}

class ButtonsPanel extends JPanel{
    //ArrayList<JButton> buttons = new ArrayList<JButton>();

    MyJPanel parent;
    private JButton addLineButton  = new JButton("addLineButton");
    private JButton addRectButton = new JButton("addRectButton");


    ButtonsPanel(final MyJPanel parent){
        this.parent = parent;
        setLayout(new FlowLayout());
        add(addLineButton);
        add(addRectButton);
        addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                Point p = null;
                try {
                    p = new Point(e.getX(), e.getY());
                } catch (IllegalArgumentException e1) {
                    e1.printStackTrace();
                }
                Iterator<MyShapeAbstract> iter = parent.getPic().iterator();
                while (iter.hasNext()){
                    MyShapeAbstract shape = iter.next();
                    if(shape.contains(p))
                    {
                        iter.remove();
                    }
                }
                repaint();
            }
        });
    }

    @Override
    public void repaint() {
        Graphics g = getGraphics();
        if(g!=null && parent.getPic() != null)
            for(MyShapeAbstract shape : parent.getPic()){
                shape.draw(g);
            }
    }

    public JButton getAddLineButton() {
        return addLineButton;
    }

    public void setAddLineButton(JButton addLineButton) {
        this.addLineButton = addLineButton;
    }

    public JButton getAddRectButton() {
        return addRectButton;
    }

    public void setAddRectButton(JButton addRectButton) {
        this.addRectButton = addRectButton;
    }
}

我在您的按钮面板中添加了以下内容:

 ButtonsPanel(final MyJPanel parent){
    this.parent = parent;
    setLayout(new FlowLayout());
    add(addLineButton);
    add(addRectButton);
    //show me the Panel size :)
    this.setBackground(Color.RED);

红色的小矩形是鼠标监听器工作的区域。所以也许你的意思是 parent.addMouseListenerdrawingArea..addMouseListener (但是你的 drawingArea 也有点小)?只需将技巧与背景一起使用并检查您的区域并调整大小或您真正想要拥有监听器的面板。也许还可以添加 MyShapeAbstract、MyLine、MyRectangle,以便测试完整代码。