简体   繁体   English

鼠标监听器不监听

[英]mouse listener does not listen

I have a school assignment that I'm stuck with. 我有一个固定的学校作业。

I'm aiming for a MVC implementation but right now I'm just doing it all in the view (just to make thing simple for now). 我的目标是实现MVC,但现在我只是在视图中进行所有操作(只是为了使事情变得简单)。

So - I have a frame and it has a panel. 所以-我有一个框架,它有一个面板。

The panel has a list of shapes. 面板上有一个形状列表。 Whenever the user is pressing the addLine/addRect buttons - an event is raised and a line/rect is added to this list. 每当用户按下addLine / addRect按钮时,就会引发一个事件并将行/矩形添加到此列表中。

The paintComponent function draws all the shapes in the list (all shapes knows how to draw themselves). paintComponent函数绘制列表中的所有形状(所有形状都知道如何绘制自己)。

So far so good - it works! 到目前为止,一切都很好-它有效!

The only other demand in this assignment is that whenever the user clicks on a point in the drawing area - all shapes that contain this point are deleted. 此分配的唯一其他要求是,只要用户单击绘图区域中的一个点,就会删除所有包含该点的形状。 each shape has its own Contains(p) function. 每个形状都有其自己的Contains(p)函数。 so I've decided to add a MouseListener to the panel that will get the X,Y coordinates of the click and will remove the relevant shapes from the list of shapes. 因此,我决定在面板上添加一个MouseListener,该鼠标监听器将获取点击的X,Y坐标,并将从形状列表中删除相关的形状。

I don't know if this is a good idea or not but this is not my problem at the moment. 我不知道这是否是个好主意,但目前这不是我的问题。

My problem is that the MouseListener does not respond to the clicking - I know that because I have a breakpoint on the inside of the implementation of mouseClicked and the debugger never got to that breakpoint. 我的问题是MouseListener无法响应单击-我知道这是因为在mouseClicked实现的内部有一个断点,而调试器从未到达该断点。

My question is why? 我的问题是为什么?

Here is my code: it has some more issues I need to address but for now they don't concern me 这是我的代码:还有更多我需要解决的问题,但现在他们不关心我

//MyFrame.java //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 //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;
    }
}

I added the following to your Button Panel: 我在您的“按钮面板”中添加了以下内容:

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

The small red rectangle is the area where your mouse listeners work. 红色小矩形是鼠标侦听器工作的区域。 So maybe you mean parent.addMouseListener or drawingArea..addMouseListener (However your drawingArea is also kinda small)? 因此,也许您的意思是parent.addMouseListenerdrawingArea..addMouseListener (但是您的drawingArea也有点小)? Just use the trick with the Background and check your area and adjust the size or the Panel you really want to have the Listener. 只需将技巧与背景一起使用,然后检查您的区域并调整大小或您真正想拥有监听器的面板即可。 Maybe also add MyShapeAbstract,MyLine, MyRectangle so its possible to test the complete code. 也许还添加MyShapeAbstract,MyLine,MyRectangle,以便可以测试完整的代码。

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

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