只用awt不用Swing写以下代码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-17 19:05:15 浏览: 74
以下是只用 AWT 不用 Swing 写 GuessNumber 类的代码,增加了一些布局和字体设置,以保证文字有足够的显示空间:
```
package chap03;
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之间的数字:");
promptLabel.setFont(new Font("Dialog", Font.PLAIN, 14));
inputField = new TextField(10);
inputField.setFont(new Font("Dialog", Font.PLAIN, 14));
guessButton = new Button("猜!");
guessButton.addActionListener(this);
guessButton.setFont(new Font("Dialog", Font.PLAIN, 14));
exitButton = new Button("退出");
exitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
exitButton.setFont(new Font("Dialog", Font.PLAIN, 14));
resultLabel = new Label("");
resultLabel.setFont(new Font("Dialog", Font.PLAIN, 14));
Panel panel = new Panel(new FlowLayout(FlowLayout.CENTER, 10, 10));
panel.add(promptLabel);
panel.add(inputField);
panel.add(guessButton);
panel.add(exitButton);
setLayout(new BorderLayout(10, 10));
add(panel, BorderLayout.CENTER);
add(resultLabel, BorderLayout.SOUTH);
setTitle("猜数字游戏界面");
setSize(300, 150);
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();
}
}
```
阅读全文