我正在学习如何制作Pacman游戏,因此我创建了一个JPanel并在其上添加了一个keyListener。但我也想显示高分和当前分数,所以我添加了一个JFrame并将JPanel放在那里。现在游戏不会运行,尽管图像确实会加载。我尝试过自己搜索,但很难,因为我不知道如何完全描述问题。添加到JFrame后,游戏变得无响应
下面是代码:
编辑:好的,所以我不知道为什么,但是当我把东西在年底建立GUI,而不是游戏再次工作。也许这是你们谈论的焦点?无论如何,非常感谢您的帮助!
/**
* This class creates a PacMan GUI that extends the JFrame class. It has a Board (JPanel) and
* includes a constructor method that sets up the frame and adds a key listener to the board.
*/
import java.awt.*;
import javax.swing.*;
@SuppressWarnings("serial") //takes away yellow lines
public class PacManGUI extends JFrame {
// Board panel
private Board board = new Board();
JLabel currentScoreLabel = new JLabel("Score: " + Board.score);
JLabel highScoreLabel = new JLabel("High Score: " + Board.highScore + " by " + Board.initials);
/**
* PacMan GUI constructor
* @param selectedTheme
*/
public PacManGUI(String selectedTheme) {
//1. Setup the GUI
setSize(620, 675);
setTitle("PacMan");
getContentPane().setBackground(Color.BLACK);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //removes the java icons that pop up when running
add(currentScoreLabel, BorderLayout.PAGE_START);
currentScoreLabel.setForeground(Color.WHITE);
add(highScoreLabel, BorderLayout.PAGE_END);
highScoreLabel.setForeground(Color.WHITE);
//2. Listen for events on the board and add the board to the GUI
add(board);
addKeyListener(board);
board.setFocusable(true);
//3. Make GUI visible
setVisible(true);
}
}
你的程序的其余部分在哪里? – gpasch
我不知道是否应该发布我的整个程序,因为它里面有几个类。当我将板子连接到JFrame时,我只有这个问题,所以我只发布了这个类。 – CreamLover123
*“我不确定我是否应该发布我的整个程序,”*不,但不是不可编译的代码片断。为了尽快提供更好的帮助,请发布[MCVE]或[简短,独立,正确的示例](http://www.sscce.org/)。 *“所以我添加了一个JFrame并将JPanel放在那里,现在游戏不会运行”*您以前看到它运行的是哪个? –