Java 在实例中强制从mouseListener调用方法
我有一个扩展jpanel并实现MouseListener的类。 在另一个类中,当MouseListener出现在所述类中时(不使用static),我需要调用一个方法,并获取“e”的值。代码更好地解释了这一点Java 在实例中强制从mouseListener调用方法,java,mouselistener,Java,Mouselistener,我有一个扩展jpanel并实现MouseListener的类。 在另一个类中,当MouseListener出现在所述类中时(不使用static),我需要调用一个方法,并获取“e”的值。代码更好地解释了这一点 public class DrawStuff extends JPanel implements MouseListener { //draws stuff public void mouseClicked(MouseEvent e) { //need this 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()不太好,因为单击鼠标时需要调用MethodToBeCall(),而不是每次手动检查,我也不想轮询值。让
其他类
(而不是DrawStuff
)实现鼠标侦听器
这将在每次鼠标单击后将事件发送到所需的实例
我建议作出以下改变:
- 不再在
DrawStuff
中执行MouseListener
- 在
DrawStuff
中定义接受外部MouseListener
- 让
otherClass
实现MouseListener
- 在构造
DrawStuff
时,将此
添加为鼠标侦听器
-参数
- 在
otherClass
- 您可以使用获取相关“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仍然与其他类解耦,并确保两个实例都处理鼠标单击事件
为清晰起见进行了编辑,请参见以下内容:
公共类DrawStuff扩展JPanel实现MouseListener{
私人滑鼠听器
public void registerMouseListener(mouseStener ml){{{this.ml=ml;
}
//在鼠标选中的情况下绘制公共空白(MouseEvent e)
{
如果(ml!=null){ml.mouseClicked(e);}
}}
公共类otherClass扩展了JFrame{
DrawStuff draw=新DrawStuff()
我不能,因为otherclass中还有其他元素。这意味着我无法准确测量绘制的内容的大小,而且因为drawstuff包含一个网格,所以我必须知道相对于drawstuff的位置。我在同一JFrame上也有多个drawstuff实例,这进一步使IssueQualited it和它的工作变得复杂ed,但有人告诉我,传递方法并保持函数与查看内容分开是更好的做法。但我真的很感激你的回答你应该实现应用程序状态的一些模型。在单击侦听器中更新模型,并在绘制时使用模型中的值。这完全取决于你如何实现ent the model.hege#u hegedus我使用的是MVC结构,这在视图中出现。API是指类吗?还有,在这种情况下,你会如何处理传播?我过去常常在C#中抛出自定义事件,并根据它们的消息捕获它们,但经过一些研究,似乎没有一个java等价物,我必须使用接口ect?我还有很多东西要学XD.Interface(应用程序编程接口)。传播很容易-你只需调用回调。MouseListener实例是用DrawStuff注册的-在它自己的MouseClicked方法中,它然后在注册的侦听器上调用回调(即registeredMouseListener.MouseClicked(e))
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 }