添加 getter 后 JButtons 表现怪异

JButtons acting weird after adding getter

我正在制作一个 java 游戏,它在 JPanel 中有一组 JButton。 一切正常,除了我向扩展的 JButton class 添加一个简单的 getter 时。 发生的情况是 JButton 仅在我将鼠标悬停在它们上方时可见。 如果我只是删除 getter,问题就不会再发生了。 为什么会这样,我该如何解决?

public class MyButton extends JButton {

int x;
int y;

public int getX() {
    return x;
}

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

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

}

Everything is working fine, except when I add a simple getter to my extended JButton class

不要覆盖 getX() 和 getY() 方法。 API 中已经存在这些方法。为您的变量和方法取一个不同的名称。

I'm making a java game that has an array of JButtons in a JPanel

您可能正在尝试为按钮指定一个网格位置,因此请尝试使用:

  1. 变量名,例如 rowcolumn
  2. 方法名称,例如 getRow()getColumn()