1

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

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

如果您不知道我在其中一个代码中是否包含某些内容,请假设我没有。我对编程很陌生,不太了解。

//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;
        }
    }
}

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

4

1 回答 1

0
  1. 您忘记将 myMouseListener 添加到 Mouse 对象。
  2. 我不相信您可以将 JFrame 添加到另一个 JFrame 中,您可以让 Mouse 扩展 JPanel 而不是 JFrame。
  3. 当您编写 c = normalc = clicked我相信这会更改cMouse 类中的指针,但不会更新超类的游标,因为该游标是使用单独的变量保存的,该变量仍指向未初始化的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);
        }
    }
}
于 2019-07-24T03:28:00.730 回答