gpt4 book ai didi

java - addMouseListener 出错

转载 作者:行者123 更新时间:2023-11-29 03:38:08 27 4
gpt4 key购买 nike

当我编译以下代码时出现错误:

The method addMouseListener(Player) is undefined for the type Player

代码:

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class Player extends Obj implements MouseListener {

public Player() {
super();
addMouseListener(this);
}

public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
}

这是 Obj 类的代码:

import java.awt.Rectangle;
import java.util.ArrayList;

public class Obj {

protected int x, y, width, height, dx, dy, depth;
protected double speed, direction;
protected Rectangle bound;
protected ArrayList<Obj> collideList;

public Obj() {
bound = new Rectangle(x, y, width, height);
collideList = new ArrayList<>();
}

public boolean checkCollision(Obj obj1, Obj obj2) {
boolean collide = false;

// Temporarily move bound to where it will be next step,
//to anticipate a collision
// (this is important to make sure objects don't "stick"
// to each other)
obj1.getBound().translate(obj1.getDx(), obj1.getDy());
// If their bounds intersect, they have collided
if (obj1!=obj2 && obj1.getBound().intersects(obj2.getBound())) {
collide = true;
} else {
// Move the bound back
bound.translate(-obj1.getDx(), -obj1.getDy());
}

return collide;
}

public void step() {
bound.setBounds(x, y, width, height);
}

public Rectangle getBound() {
return bound;
}

public int getX() {
return x;
}

public int getY() {
return y;
}

public int getDy() {
return dy;
}

public int getDx() {
return dx;
}

public void setX(int x) {
this.x = x;
}

public void setY(int y) {
this.y = y;
}

public int getWidth() {
return width;
}

public int getHeight() {
return height;
}
}

最佳答案

您不能将鼠标监听器添加到任何对象类型。 Obj 必须扩展 java.awt.Component 才能做到这一点。

关于java - addMouseListener 出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14468630/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com