1

所以下面是我正在制作的这款游戏的代码。它是一个数字猜谜游戏。无论如何,我的主要问题是如何制作 gamePanel 的内容,以便在调整屏幕大小时组件仍位于中心。因此,如果屏幕变得更小,那么组件会自动移动以被压扁并仍然在面板中看到。

多年来,我一直试图弄清楚这一点。

public class GameScreen {    

    public static void main(String[] args) {
        final JFrame game;
        final JButton playButton, guessButton;
        final JPanel topPanel, gamePanel, rangePanel, guessPanel;
        final JTextArea gameStatus;
        final GuessingGame gameNumbers;

        game = new JFrame();
        gameNumbers = new GuessingGame();
        game.setTitle("Guessing Game");
        game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JLabel playerLabel = new JLabel("Player: ");
        JLabel guessLabel = new JLabel("Guess: ");
        final JLabel rangeLabel = new JLabel("Range: ");

        final JTextField playerName = new JTextField("", 20);
        final JTextField guessInput = new JTextField("", 20);

        playButton = new JButton("Play");
        guessButton = new JButton("Guess");

        topPanel = new JPanel();
        gamePanel = new JPanel();
        rangePanel = new JPanel(new BorderLayout());
        guessPanel = new JPanel(new FlowLayout());
        CountDownProgressBar countDownPanel = new CountDownProgressBar();


        gameStatus = new JTextArea(10, 20);
        JScrollPane scroll = new JScrollPane(gameStatus);

        game.add(topPanel, BorderLayout.NORTH);
        game.add(scroll, BorderLayout.EAST);
        game.add(gamePanel, BorderLayout.WEST);
        game.add(countDownPanel, BorderLayout.SOUTH);

        gamePanel.add(rangePanel);
        gamePanel.add(guessPanel);

        topPanel.setBackground(Color.yellow);
        topPanel.add(playerLabel);
        topPanel.add(playerName);
        topPanel.add(playButton);

        rangePanel.add(rangeLabel);

        guessPanel.add(guessLabel);
        guessPanel.add(guessInput);
        guessPanel.add(guessButton);   

        gameStatus.setFocusable(false);
        guessButton.setEnabled(false); 
        guessInput.setEnabled(false);
        playButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (playerName.getText().equals("")) {
                    JOptionPane.showMessageDialog(gamePanel, "Invalid Player Name");
                } else {
                    gameNumbers.play();    
                    gameStatus.append("Game Started!\n");
                    rangeLabel.setText(gameNumbers.toString());           
                    guessButton.setEnabled(true);
                    guessInput.setEnabled(true);
                    playButton.setEnabled(false);
                    playerName.setEnabled(false);
                }
            }
        });
        guessButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int playerGuess = Integer.parseInt(guessInput.getText());
                if (gameNumbers.isGuessCorrect(playerGuess)) {
                    gameStatus.append("Game Over!\n");
                    guessButton.setEnabled(false);
                    guessInput.setEnabled(false);
                    playButton.setEnabled(true);
                    playerName.setEnabled(true);
                    JOptionPane.showMessageDialog(gamePanel, "" + playerName.getText() + " wins!");
                } if (gameNumbers.isGuessAlmostCorrect(playerGuess)) {
                    gameStatus.append("very close!\n");
                    guessInput.requestFocus();
                } if (gameNumbers.isGuessLarger(playerGuess)) {
                    gameStatus.append("try something smaller...\n");
                    guessInput.requestFocus();
                } if (gameNumbers.isGuessSmaller(playerGuess)) {
                    gameStatus.append("try something larger...\n");
                    guessInput.requestFocus();
                } 
            }
        });
        game.pack();
        game.setVisible(true);
    }


}
4

1 回答 1

2

有很多遗漏,但是,如果我没看错的话,请将布局管理器更改为gamePanel类似GridBagLayout( gamePanel = new JPanel(new GridBagLayout())) 的内容,并将其添加到game面板的中心位置 ( game.add(gamePanel, BorderLayout.CENTER))。

于 2012-12-26T04:58:28.950 回答