如何为自定义光标创建单独的 class

How to create a separate class for a custom cursor

我正在尝试将游戏作为个人项目制作,并且想要一个具有两种状态的自定义光标;正常并点击。如何为此创建一个单独的 class 并在创建 window 的 class 中使用它?

这只是为了获得学习经验和为游戏创建自定义光标的机会。我已经尝试将代码放入 Window class 并且没有单独的 class 用于鼠标。那行得通,但是很乱,我想学习如何通过分离 classes 来清理它。

如果您不知道我是否在某个代码中包含某些内容,请假设我没有。我是编程新手,了解的不多。

//This is the code that worked
public class Window extends JFrame {
    Cursor normal = Toolkit.getDefaultToolkit().createCustomCursor(Toolkit.getDefaultToolkit().getImage("Cursor.gif"), new Point(0, 0), "Gauntlet");
    Cursor clicked = Toolkit.getDefaultToolkit().createCustomCursor(Toolkit.getDefaultToolkit().getImage("Cursor_Clicked.gif"), new Point(0, 0), "Gauntlet_Clicked");
    class myMouseListener implements MouseListener {
        @Override
        public void mouseEntered(MouseEvent arg0) {
            setCursor(normal);
        }
        public void mouseExited(MouseEvent arg0) {
        }
        @Override
        public void mouseClicked(MouseEvent arg0) {
        }
        @Override
        public void mousePressed(MouseEvent arg0) {
            setCursor(clicked);
        }
        @Override
        public void mouseReleased(MouseEvent arg0) {
            setCursor(normal);
        }
    }
    myMouseListener mml = new myMouseListener();
    public Window() {
        super.setSize(1200, 1000);
        super.addMouseListener(mml);
        super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        super.setVisible(true);
    }
    public static void main(String[] args) {
        new Window();
    }
}
//This is the current code
public class Window extends JFrame {
    public Window() {
        super.setSize(1200, 1000);
        super.add(new Mouse());
        super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        super.setVisible(true);
    }
    public static void main(String[] args) {
        new Window();
    }
}
//This is the new class for the cursor that I'm having trouble with
public class Mouse extends JFrame {
    public Mouse() {
        super.setCursor(c);
    }
    Cursor normal = Toolkit.getDefaultToolkit().createCustomCursor(Toolkit.getDefaultToolkit().getImage("Cursor.gif"), new Point(0, 0), "Gauntlet");
    Cursor clicked = Toolkit.getDefaultToolkit().createCustomCursor(Toolkit.getDefaultToolkit().getImage("Cursor_Clicked.gif"), new Point(0, 0), "Gauntlet_Clicked");
    Cursor c;
    class myMouseListener implements MouseListener {
        @Override
        public void mouseEntered(MouseEvent arg0) {
            c = normal;
        }
        public void mouseExited(MouseEvent arg0) {
        }
        @Override
        public void mouseClicked(MouseEvent arg0) {
        }
        @Override
        public void mousePressed(MouseEvent arg0) {
            c = clicked;
        }
        @Override
        public void mouseReleased(MouseEvent arg0) {
            c = normal;
        }
    }
}

使用当前代码,我希望我的自定义光标在我按下鼠标左键时会发生变化,就像在功能代码中一样。但是,鼠标根本没有变化。就好像我什么代码都没写一样

  1. 您忘记将 myMouseListener 添加到鼠标对象。
  2. 我不相信你可以将一个 JFrame 添加到另一个 JFrame 中,你可以让 Mouse 扩展 JPanel 而不是 JFrame。
  3. 当你写 c = normalc = clicked 我相信这会改变 c 在鼠标 class 中的指针,但不会更新 superclass 的游标,因为该游标保存在一个单独的变量中,该变量仍然指向未初始化的 c。要解决此问题,您只需调用 setCursor(normal)setCursor(clicked),如下所示:
public class Mouse extends JPanel {
    public Mouse() {
        this.addMouseListener(new myMouseListener());
    }
    Cursor normal = Toolkit.getDefaultToolkit().createCustomCursor(Toolkit.getDefaultToolkit().getImage("Cursor.gif"), new Point(0, 0), "Gauntlet");
    Cursor clicked = Toolkit.getDefaultToolkit().createCustomCursor(Toolkit.getDefaultToolkit().getImage("Cursor_Clicked.gif"), new Point(0, 0), "Gauntlet_Clicked");
    class myMouseListener implements MouseListener {
        @Override
        public void mouseEntered(MouseEvent arg0) {
            setCursor(normal);
        }
        public void mouseExited(MouseEvent arg0) {
        }
        @Override
        public void mouseClicked(MouseEvent arg0) {
        }
        @Override
        public void mousePressed(MouseEvent arg0) {
            setCursor(clicked);
        }
        @Override
        public void mouseReleased(MouseEvent arg0) {
            setCursor(normal);
        }
    }
}