1
所以下面是我製作這款遊戲的代碼。它是一個猜數字遊戲。無論如何,我的主要問題是如何製作gamePanel的內容,以便在您調整屏幕大小時,組件仍處於中心位置。所以如果屏幕聚集得越來越小,那麼這些組件會自動移動而被壓扁並在面板中靜止。JPanel的中心組件可調整大小
我一直在試圖弄清楚這個年齡。
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);
}
}
*「我一直在試圖弄清楚這個問題。」*第一個數字a)如何使用代碼格式。選擇代碼示例,然後單擊郵件發佈/編輯表單上方的「{}」按鈕。 b)如何在每句話的開頭使用你的shift鍵。我(一)不能讀'嘟''。 - 關於要求,顯示GUI的ASCII藝術,因爲它應該以最小尺寸出現,另一個顯示在向GUI添加額外寬度/高度後如何分配額外空間。爲了更快地提供幫助,請發佈[SSCCE](http://sscce.org/)。 –