只用AWT改写package chap03; import javax.swing.; import java.awt.; import java.awt.event.*; public class GuessNumber extends JFrame implements ActionListener { private JLabel promptLabel, resultLabel; private JTextField inputField; private JButton guessButton; private int targetNumber, guessCount, maxTryTime; public GuessNumber () { targetNumber = (int)(Math.random()*10)+1; guessCount = 0; maxTryTime = 5; promptLabel = new JLabel("请输入一个0~10之间的数字:"); inputField = new JTextField(10); guessButton = new JButton("猜!"); guessButton.addActionListener(this); resultLabel = new JLabel(""); JPanel panel = new JPanel(); panel.add(promptLabel); panel.add(inputField); panel.add(guessButton); panel.add(resultLabel); this.add(panel); this.setTitle("猜数字游戏"); this.setSize(300, 150); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } public void actionPerformed(ActionEvent e) { String inputText = inputField.getText(); try { int guessNumber = Integer.parseInt(inputText); guessCount++; if (guessCount < maxTryTime) { int c = maxTryTime - guessCount; if (guessNumber > targetNumber) { resultLabel.setText("输入的过大,您还有" + c + "次机会"); } else if (guessNumber < targetNumber) { resultLabel.setText("输入的过小,您还有" + c + "次机会"); } else { resultLabel.setText("恭喜你,猜对了!你一共猜了" + guessCount + "次"); guessButton.setEnabled(false); } } else { resultLabel.setText("抱歉游戏失败"); guessButton.setEnabled(false); } } catch (NumberFormatException ex) { resultLabel.setText("输入异常,请重新输入!!!"); } } public static void main(String[] args) { new GuessNumber(); } }并添加退出按钮 输出代码
时间: 2024-01-17 22:05:15 浏览: 128
jsb.zip_awt_event_import java.awt._java 记事本
以下是使用AWT改写的GuessNumber代码,包括退出按钮:
```
import java.awt.*;
import java.awt.event.*;
public class GuessNumber extends Frame implements ActionListener {
private Label promptLabel, resultLabel;
private TextField inputField;
private Button guessButton, exitButton;
private int targetNumber, guessCount, maxTryTime;
public GuessNumber () {
targetNumber = (int)(Math.random()*10)+1;
guessCount = 0;
maxTryTime = 5;
promptLabel = new Label("请输入一个0~10之间的数字:");
inputField = new TextField(10);
guessButton = new Button("猜!");
guessButton.addActionListener(this);
exitButton = new Button("退出");
exitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
resultLabel = new Label("");
Panel panel = new Panel();
panel.add(promptLabel);
panel.add(inputField);
panel.add(guessButton);
panel.add(exitButton);
panel.add(resultLabel);
this.add(panel);
this.setTitle("猜数字游戏");
this.setSize(300, 150);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String inputText = inputField.getText();
try {
int guessNumber = Integer.parseInt(inputText);
guessCount++;
if (guessCount < maxTryTime) {
int c = maxTryTime - guessCount;
if (guessNumber > targetNumber) {
resultLabel.setText("输入的过大,您还有" + c + "次机会");
} else if (guessNumber < targetNumber) {
resultLabel.setText("输入的过小,您还有" + c + "次机会");
} else {
resultLabel.setText("恭喜你,猜对了!你一共猜了" + guessCount + "次");
guessButton.setEnabled(false);
}
} else {
resultLabel.setText("抱歉游戏失败");
guessButton.setEnabled(false);
}
} catch (NumberFormatException ex) {
resultLabel.setText("输入异常,请重新输入!!!");
}
}
public static void main(String[] args) {
new GuessNumber();
}
}
```
阅读全文