在实例中强制从 mouseListener 调用方法

Forcing method call from mouseListener in instance

我有一个 class,它扩展了 jpanel 并实现了 MouseListener。 在另一个 class 中,我需要在上述 class 中出现 MouseListener 时调用一个方法(不使用 static)并获取 "e" 的值。代码更好地解释了这一点。

public class DrawStuff extends JPanel implements MouseListener
{
//draws stuff
public void mouseClicked(MouseEvent e) 
    {
     //need this e
    }
}

public class otherClass extends JFrame
{
    DrawStuff draw = new DrawStuff();
    add(draw);
    int[][] arr_which_cannot_be_passed;
    public void methodToBeCalled()
    {
        //e can be used here
    }
    //other JFrame components added
}

Draw.getE() 不好,因为单击鼠标时需要调用 methodToBeCalled(),而不是每次都手动检查,我不想轮询该值。

otherClass(而不是DrawStuff)实施MouseListener

这将在每次鼠标单击后将事件发送到所需的实例。

我建议进行以下更改:

  • 不再在 DrawStuff 中实施 MouseListener
  • DrawStuff 中定义一个接受外部 MouseListener
  • 的构造函数(或 setter)
  • otherClass实施MouseListener
  • 在构造 DrawStuff 时添加 this 作为 MouseListener 参数
  • otherClass
  • 中实施mouseClicked
  • 您可以使用e.getComponent()获取相关"DrawStuff"实例的引用

您的代码可能如下所示:

public class DrawStuff extends JPanel// do not implement MouseListener anymore
{
//...draws stuff...

    public DrawStuff(MouseListener listener) // add MouseListener as constructor parameter (will be an instance of "otherClass")
    {
        //...create your components...

        // use the instance of "otherClass" as MouseListener
        component.addMouseListener(listener);
    }
}

public class otherClass extends JFrame
    implements MouseListener// let this Frame implement MouseListener
{

    // give "DrawStuff" a reference to "this" (as implementation of MouseListener)
    DrawStuff draw = new DrawStuff(this);

    //...other stuff...

    public void mouseClicked(MouseEvent e) 
    {
         methodToBeCalled(e);
    }

    public void methodToBeCalled(MouseEvent e)
    {
        //e can be used here
        //you can use e.getComponent() to get the reference of the relevant "DrawStuff" instance
    }

    //...other JFrame components added...
}

因此,只要 DrawStuff 必须处理鼠标单击事件,otherClass 就会感兴趣。

可以通过多种方式注册和执行此兴趣。

一种方法是扩展 DrawStuff 的 API,以便您可以注册鼠标侦听器。 DrawStuff 然后通过将回调调用到已注册的侦听器(如果有的话)来传播其 MouseClicked 事件。

otherClass 使用这个新的 API 方法将它自己的 MouseListener 实现注册到 DrawStuff - 该实现调用 'methodToBeCalled'.

使用此机制,您仍然可以将 DrawStuff 与 otherClass 分离,并确保鼠标单击事件由两个实例处理。

为清楚起见已编辑,请参阅下文:

public class DrawStuff extends JPanel implements MouseListener {

private MouseListener ml;

public void registerMouseListener(MouseListener ml) { { this.ml = ml; }

//draws stuff public void mouseClicked(MouseEvent e) { if (ml != null) { ml.mouseClicked(e); } } }

public class otherClass extends JFrame { DrawStuff draw = new DrawStuff();

public void intialise() {

    draw.registerMouseListener(new MoueListener() {
           ....
           public void mouseClicked(MouseEvent e) {
               methodToBeCalled();                
           }
           ....
    }); 
}

public void do()
{     add(draw);    
}



int[][] arr_which_cannot_be_passed;
public void methodToBeCalled()
{
    //e can be used here
}
//other JFrame components added }