以下は、私が作成しているこのゲームのコードです。その数を推測するゲーム。とにかく私の主な質問は、画面のサイズを変更したときにコンポーネントがまだ中央にあるように、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);
}
}