给这个代码添加退出按钮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 07:05:15 浏览: 64
好的,我可以为您添加一个退出按钮。下面是代码:
```
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, exitButton;
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);
exitButton = new JButton("退出");
exitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
resultLabel = new JLabel("");
JPanel panel = new JPanel();
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.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();
}
}
```
我在原有代码的基础上,添加了一个名为 `exitButton` 的按钮,并为其添加了一个 `ActionListener`,当点击该按钮时,程序将通过 `System.exit(0)` 退出。同时,我也将该按钮添加到了 `JPanel` 中,以便在界面中显示。
阅读全文