当我单击JButton时,我希望该游戏开始 [英] when i click JButton i want that game should start
问题描述
我正在从事蛇游戏项目。我有三个Java文件,分别为:
I am working on a snake game project. I have three java files named:
-
Engine.java
-
GameBoard.java
-
Snake.java
Engine.java
GameBoard.java
Snake.java
我在此项目中添加了两个 JFrames
。在第一帧中有三个按钮:
I have added two JFrames
in this project. In the first frame are three buttons:
-
播放
-
规则
-
出口
play
rules
exit
当我们单击规则
按钮时,它会打开规则jframe
(正在运行)。当我们单击播放
按钮时,它将运行蛇游戏。请建议我点击玩
按钮真正开始游戏后该怎么办。
When we click to rules
button it opens rules jframe
(it's working). When we click play
button it should run the snake game. Please suggest me what should i do when i click play
button to actually starts the game.
这是代码我在播放
按钮 actionPerformed
方法中复制了:
This is the code i copied in play
button actionPerformed
method:
JFrame frame = new JFrame("SnakeGame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
Canvas canvas = new Canvas();
canvas.setBackground(Color.black);
canvas.setPreferredSize(new Dimension(GameBoard.MAP_SIZE * GameBoard.TILE_SIZE, GameBoard.MAP_SIZE * GameBoard.TILE_SIZE));
frame.add(canvas);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
new Engine(canvas).startGame();
推荐答案
添加 ActionListener
播放按钮,调用适当的启动方法。
Add an ActionListener
to your "play" button that calls the appropriate start method.
这篇关于当我单击JButton时,我希望该游戏开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!