用java编写窗体应用程序,安排一个文本框、一个按钮和一个标签,从文本框录入一个数字(0~9),点击按钮将其对应的英文单词(如:zero,one等)显示在标签中 。进一步,扩展数据的范围(如:0~100),如何修改程序实现翻译。
时间: 2024-05-10 12:19:24 浏览: 127
以下是Java代码实现:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class NumberToWordConverter extends JFrame implements ActionListener {
private JTextField textField;
private JLabel resultLabel;
private String[] wordArray = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
public NumberToWordConverter() {
super("Number to Word Converter");
// 设置窗体布局
setLayout(new FlowLayout());
// 创建文本框和标签
textField = new JTextField(10);
add(textField);
JButton button = new JButton("Convert");
add(button);
resultLabel = new JLabel("");
add(resultLabel);
// 为按钮添加动作监听器
button.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(250, 100);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String inputText = textField.getText();
try {
int number = Integer.parseInt(inputText);
if (number >= 0 && number <= 9) {
resultLabel.setText(wordArray[number]);
} else {
resultLabel.setText("Number out of range!");
}
} catch (NumberFormatException ex) {
resultLabel.setText("Invalid input!");
}
}
public static void main(String[] args) {
new NumberToWordConverter();
}
}
```
如果要扩展数据的范围到 0~100,可以修改 `wordArray` 数组为包含 101 个元素的字符串数组,其中前 10 个元素为 0~9 对应的英文单词,后面的元素为 10~100 对应的英文单词,可以通过一个 switch 语句实现转换。修改后的代码如下:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class NumberToWordConverter extends JFrame implements ActionListener {
private JTextField textField;
private JLabel resultLabel;
private String[] wordArray = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine",
"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen",
"seventeen", "eighteen", "nineteen", "twenty", "twenty-one", "twenty-two",
"twenty-three", "twenty-four", "twenty-five", "twenty-six", "twenty-seven",
"twenty-eight", "twenty-nine", "thirty", "thirty-one", "thirty-two",
"thirty-three", "thirty-four", "thirty-five", "thirty-six", "thirty-seven",
"thirty-eight", "thirty-nine", "forty", "forty-one", "forty-two", "forty-three",
"forty-four", "forty-five", "forty-six", "forty-seven", "forty-eight",
"forty-nine", "fifty", "fifty-one", "fifty-two", "fifty-three", "fifty-four",
"fifty-five", "fifty-six", "fifty-seven", "fifty-eight", "fifty-nine", "sixty",
"sixty-one", "sixty-two", "sixty-three", "sixty-four", "sixty-five", "sixty-six",
"sixty-seven", "sixty-eight", "sixty-nine", "seventy", "seventy-one",
"seventy-two", "seventy-three", "seventy-four", "seventy-five", "seventy-six",
"seventy-seven", "seventy-eight", "seventy-nine", "eighty", "eighty-one",
"eighty-two", "eighty-three", "eighty-four", "eighty-five", "eighty-six",
"eighty-seven", "eighty-eight", "eighty-nine", "ninety", "ninety-one",
"ninety-two", "ninety-three", "ninety-four", "ninety-five", "ninety-six",
"ninety-seven", "ninety-eight", "ninety-nine", "one hundred"};
public NumberToWordConverter() {
super("Number to Word Converter");
// 设置窗体布局
setLayout(new FlowLayout());
// 创建文本框和标签
textField = new JTextField(10);
add(textField);
JButton button = new JButton("Convert");
add(button);
resultLabel = new JLabel("");
add(resultLabel);
// 为按钮添加动作监听器
button.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 100);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String inputText = textField.getText();
try {
int number = Integer.parseInt(inputText);
if (number >= 0 && number <= 100) {
resultLabel.setText(wordArray[number]);
} else {
resultLabel.setText("Number out of range!");
}
} catch (NumberFormatException ex) {
resultLabel.setText("Invalid input!");
}
}
public static void main(String[] args) {
new NumberToWordConverter();
}
}
```
阅读全文