只用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, 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(); } }运行时能给文字足够的显示空间
时间: 2024-01-16 17:04:10 浏览: 74
好的,以下是使用AWT重写的代码,确保给予足够的显示空间:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
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.setLayout(new GridLayout(3, 2, 5, 5));
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.setMinimumSize(new Dimension(300, 150));
this.setMaximumSize(new Dimension(600, 300));
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();
}
}
```
阅读全文