我创建了一个扩展 Component 类的类。设置绘画方法等。现在我刚刚在类中添加了一个鼠标侦听器,但它不起作用。这是鼠标侦听器类:
public class RobotDetailsMouseListener implements MouseListener {
private Robot robot;
private boolean showDetails;
public RobotDetailsMouseListener(Robot robot) {
this.robot = robot;
showDetails = true;
}
@Override
public void mouseClicked(MouseEvent arg0) {
System.out.println("This is being ran!");
robot.setShowDetails(showDetails);
showDetails = !showDetails;
}
@Override
public void mouseEntered(MouseEvent arg0) {
System.out.println("Mouse is over a robot");
}
@Override
public void mouseExited(MouseEvent arg0) {
System.out.println("Mouse is over a robot");
}
@Override
public void mousePressed(MouseEvent arg0) {
System.out.println("Mouse is over a robot");
}
@Override
public void mouseReleased(MouseEvent arg0) {
System.out.println("Mouse is over a robot");
}
}
这是通过该this.addMouseListener()
方法添加到组件中的,我正在向它传递一个类型的新对象RobotDetailsMouseListener()
:
this.addMouseListener(new RobotDetailsMouseListener(this));
组件本身在屏幕上移动很多(它在 2d 平面上模拟自主机器人)。这和它有什么关系吗?
提前致谢!