老鼠听众不听

时间:2015-05-19 20:16:43

标签: java swing listener mouselistener

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

我的目标是实现MVC,但现在我只是在视图中完成所有操作(现在只是为了简化)。

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

面板有一个形状列表。只要用户按下addLine / addRect按钮,就会引发一个事件,并在此列表中添加一行/ 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;
    }
}

1 个答案:

答案 0 :(得分:1)

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

 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(但你的绘图区域也有点小)?只需使用背景技巧并检查您的区域并调整您想要拥有监听器的大小或面板。也许还可以添加MyShapeAbstract,MyLine,MyRectangle,以便测试完整的代码。