我正在尝试将游戏制作为个人项目,并且想要一个具有两种状态的自定义光标;正常并点击。如何为此创建一个单独的类并在创建窗口的类中使用它?
这只是为了学习经验和为游戏创建自定义光标的机会。我已经尝试将代码放入 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;
}
}
}
使用当前代码,我希望我的自定义光标在我按下鼠标左键时会发生变化,就像在功能代码中一样。但是,鼠标根本没有变化。就好像我什至没有编写任何代码一样。