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

时间:2016-01-22 11:00:56

标签: java mouselistener

我有一个扩展jpanel并实现MouseListener的类。 在另一个类中,我需要在所述类(不使用静态)中发生MouseListener时调用一个方法,并获取" 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(),而不是每次都手动检查,而不是轮询值。

2 个答案:

答案 0 :(得分:0)

otherClass(而非DrawStuff)实施MouseListener

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

我会建议这些改变:

  • 不再在MouseListener中实施DrawStuff
  • DrawStuff中定义一个接受外部MouseListener
  • 的构造函数(或setter)
  • otherClass实施MouseListener
  • 在构建DrawStuff时添加this作为MouseListener - 参数
  • mouseClicked
  • 中实施otherClass
  • 您可以使用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...
}

答案 1 :(得分:0)

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

可以通过各种方式注册并采取行动。

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

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

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

为了清晰起见,请参阅以下内容:

  

公共类DrawStuff扩展了JPanel实现的MouseListener {

     

私有MouseListener ml;

     

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

     

//绘制东西public void mouseClicked(MouseEvent e)       {            if(ml!= null){ml.mouseClicked(e); }       }}

     

public class otherClass扩展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 }