【问题标题】:Listener invokes another listener侦听器调用另一个侦听器
【发布时间】:2011-10-18 18:02:09
【问题描述】:

在使用 Swing 创建菜单的类中,我添加了 4 个项目,每个项目负责绘制不同颜色的圆圈。我已经为该菜单的每个项目分配了一个 ActionListener,只是为了指定所需圆圈的颜色。我需要在面板中的任意位置单击鼠标并在那里创建要创建的圆圈。为此,我创建了另一个类 ScrollPane,它实现了 mouseListener 并负责绘制圆。不过,我不知道如何将 mouseListener 触发到将完成这项工作的第二类。这应该仅在调用菜单的侦听器后完成。我显然需要对类 ScrollPane 的引用,然后在其上分配 mouseListener。

我不知道我应该对方法 addMouseListener 使用什么样的参数。或者这是在 ScrollPane 类中声明的?我认为如果所有任务都在一个实现两个侦听器的类中执行,这很简单,但是如果我想将它们分开怎么办? actionPerformed 还应该做什么? 以下是相关代码:

public class CreateMenu implements ActionListener {
    private ScrollPane scrollPane;

    public void actionPerformed(ActionEvent e) {
        JMenuItem source = (JMenuItem)(e.getSource());
        if(source.getText() == "Green Circle")
            this.scrollPane.setColor(Color.green);
         else if(source.getText() == "Blue Circle")
             this.scrollPane.setColor(Color.blue);
         else if(source.getText() == "Red Circle")
             this.scrollPane.setColor(Color.red);
         else if(source.getText() == "Yellow Circle")
             this.scrollPane.setColor(Color.yellow);
         else
             return;
     }
}

.

public class ScrollPane extends JPanel implements MouseListener {
    private Dimension area;
    private Vector<Circle> circles;
    private Color color;
    private JPanel drawingPane;

    public ScrollPane() {
        super(new BorderLayout());

        area = new Dimension(0,0);
        circles = new Vector<Circle>();

        //Set up the drawing area.
        drawingPane = new DrawingPane();
        drawingPane.setBackground(Color.white);
        drawingPane.addMouseListener(this);

        //Put the drawing area in a scroll pane.
        JScrollPane scroller = new JScrollPane(drawingPane);
        scroller.setPreferredSize(new Dimension(200,200));

        add(scroller, BorderLayout.CENTER);
    }

    public class DrawingPane extends JPanel {
        protected void paintComponent(Graphics g, Color color ) {
            super.paintComponent(g);

            Rectangle rect;
            for (int i = 0; i < circles.size(); i++) {
                rect = circles.elementAt(i).getRect();
                g.setColor(circles.elementAt(i).getTheColor());
                g.fillOval(rect.x, rect.y, rect.width, rect.height);
            }
        }
    }

    public void mouseReleased(MouseEvent e) {
        final int W = 100;
        final int H = 100;
        boolean changed = false;

        if(SwingUtilities.isLeftMouseButton(e)) {
            int x = e.getX() - W/2;
            int y = e.getY() - H/2;
            if (x < 0) x = 0;
            if (y < 0) y = 0;
            Rectangle rect = new Rectangle(x, y, W, H);
            Circle newCircle = new Circle(rect, this.color);
            circles.addElement(newCircle);
            drawingPane.scrollRectToVisible(rect);

            int this_width = (x + W + 2);
            if (this_width > area.width) {
                area.width = this_width; changed=true;
            }

            int this_height = (y + H + 2);
            if (this_height > area.height) {
                area.height = this_height; changed=true;
            }
        }
        if (changed) {
            //Update client's preferred size because
            //the area taken up by the graphics has
            //gotten larger or smaller (if cleared).
            drawingPane.setPreferredSize(area);

            //Let the scroll pane know to update itself
            //and its scrollbars.
            drawingPane.revalidate();
        }
        drawingPane.repaint();
    }
    public void mouseClicked(MouseEvent e){}
    public void mouseEntered(MouseEvent e){}
    public void mouseExited(MouseEvent e){}
    public void mousePressed(MouseEvent e){}


    public void setColor(Color color) {
        this.color = color;
    }

    public JPanel getDrawingPane() {
        return drawingPane;
    }
}

【问题讨论】:

  • 一方面,不要将字符串与 == 进行比较:if(source.getText() == "Green Circle")。而是使用 equals 方法。

标签: java swing listener


【解决方案1】:

不要尝试重新发送鼠标侦听器调用,如果这是您想要做的。你不是老鼠。相反,让鼠标侦听器方法简单地调用对象上执行实际工作的其他方法。你的鼠标监听例程应该是微不足道的“如果这个,调用这个,如果那个,调用那个。”

然后,如果对象之间存在关系,则让其中一个对象接收鼠标侦听器点击。然后调用应受该点击影响的所有对象的方法。

我过去所做的就是创建我自己的侦听器模式,例如,将网格放置在另一个摆动对象中。然后,当对象 X 发生需要传递给对象 Y 的事情时,我使用我的侦听器模式。这样,每当调用给定的方法时,就很清楚需要完成哪些工作。而且它更可重复使用。所以如果不是鼠标点击而是菜单触发,我就不用重复逻辑了。

抱歉,如果不是很清楚。

【讨论】:

    【解决方案2】:

    您始终可以将事件从一个组件转发到另一个组件,如here 所示; Action 是封装菜单和组件共享行为的首选方式。

    在这种情况下,您似乎想自己进行绘图和鼠标处理。您可能想检查一下这个简单的GraphPanel,它依赖于普通的侦听器。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-30
      • 2020-07-04
      • 2011-08-16
      • 1970-01-01
      • 2017-02-22
      • 2019-07-25
      相关资源
      最近更新 更多