简体   繁体   English

我需要注册mouseListener吗?

[英]Do I need to register my mouseListener?

I am supposed to have a GUI that keeps track of a list of pre-specified "special" points. 我应该有一个GUI,它可以跟踪预先指定的“特殊”点的列表。 If the user's mouse moves over one of these points, the point lights up. 如果用户的鼠标移到这些点之一上,该点将亮起。 My task was to write the code that would go inside of the handler/listener below and use the references and also assume the existence of the method listed below (which I was supposed to invoke- assume it has already been written). 我的任务是编写将在下面的处理程序/侦听器内部使用的代码,并使用引用,并假定存在下面列出的方法(我应该调用该方法-假定它已经被编写)。

  • specialPointList // an ArrayList of the pre-specified "special" points; specialPointList //预先指定的“特殊”点的ArrayList; the type is ArrayList<Point> 类型是ArrayList<Point>
  • assume this method exists: public void lightUpAPoint(Point p) 假定存在此方法: public void lightUpAPoint(Point p)

My code will go inside the handler method: 我的代码将放入处理程序方法内:

public void mouseMoved(MouseEvent event) {

}

Do I need to register my mouseListener? 我需要注册mouseListener吗?

This is what I believe the question is asking me to do: 我认为这是我要问的问题:

public void mouseMoved(MouseEvent event) {
    specialPointList = ArrayList<Point>();

    Point mousePoint = event.getPoint(); 
    for(Point point : specialPointList) {
        if(mousePoint.equals(point)) {
            lightUpAPoint(point);
        }
    }
}

Whatever listener you create you need to register it to ui component. 无论您创建什么监听器,都需要将其注册到ui组件。 Look at methods of component. 看一下组件的方法。 If there is method to add your listener you can do it. 如果有添加侦听器的方法,则可以执行此操作。

All swing components support MouseListener. 所有的swing组件都支持MouseListener。

When action is made by user new event is created and is sent to your listener. 当用户执行操作时,将创建新事件并将其发送到您的侦听器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM