java猜数字游戏GUI
时间: 2023-09-28 15:05:20 浏览: 44
好的,以下是一个简单的猜数字游戏GUI的Java代码示例:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
public class GuessNumberGame extends JFrame implements ActionListener {
private int randNum;
private int numGuesses;
private final JTextField guessField;
private final JTextArea resultArea;
public GuessNumberGame() {
setTitle("Guess Number Game");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
JPanel topPanel = new JPanel();
topPanel.setLayout(new FlowLayout());
JLabel guessLabel = new JLabel("Guess a number between 1 and 100: ");
topPanel.add(guessLabel);
guessField = new JTextField(10);
topPanel.add(guessField);
JButton guessButton = new JButton("Guess");
guessButton.addActionListener(this);
topPanel.add(guessButton);
add(topPanel, BorderLayout.NORTH);
JPanel bottomPanel = new JPanel();
bottomPanel.setLayout(new BorderLayout());
JLabel resultLabel = new JLabel("Results:");
bottomPanel.add(resultLabel, BorderLayout.NORTH);
resultArea = new JTextArea();
resultArea.setEditable(false);
bottomPanel.add(resultArea, BorderLayout.CENTER);
add(bottomPanel, BorderLayout.CENTER);
setSize(400, 300);
setVisible(true);
startGame();
}
private void startGame() {
Random rand = new Random();
randNum = rand.nextInt(100) + 1;
numGuesses = 0;
resultArea.setText("");
}
private void handleGuess(int guess) {
numGuesses++;
if (guess == randNum) {
resultArea.append("Congratulations! You guessed the number in " + numGuesses + " guesses.\n");
startGame();
} else if (guess < randNum) {
resultArea.append("Too low! Try again.\n");
} else {
resultArea.append("Too high! Try again.\n");
}
guessField.setText("");
}
public void actionPerformed(ActionEvent e) {
try {
int guess = Integer.parseInt(guessField.getText());
if (guess >= 1 && guess <= 100) {
handleGuess(guess);
} else {
JOptionPane.showMessageDialog(this, "Please enter a number between 1 and 100!");
}
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(this, "Please enter a valid number!");
}
}
public static void main(String[] args) {
new GuessNumberGame();
}
}
```
这个程序使用了Java的Swing库来创建GUI界面,通过添加监听器来处理用户输入并进行游戏逻辑。游戏逻辑包括生成一个1到100之间的随机数字,并与用户输入的数字进行比较,直到用户猜对为止。用户每次猜测后,程序会显示一条消息告诉他们猜的数字是太高还是太低,并清空输入框以便下一轮猜测。如果用户猜对了,程序会显示一条消息告诉用户他们猜对了,并重新开始游戏。
相关推荐
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)