编写窗体应用程序,安排一个文本框、一个按钮和一个标签,从文本框录入一个数字(0~9),点击按钮将其对应的英文单词(如:zero,one等)显示在标签中。
时间: 2024-05-02 17:19:54 浏览: 94
这是一个编程问题,可以使用 C#、Java、Python等编程语言实现。以下是使用 C#实现的代码示例:
```
private void button1_Click(object sender, EventArgs e)
{
string[] words = { "zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine" };
int num = Int32.Parse(textBox1.Text);
label1.Text = words[num];
}
```
在该代码中,首先定义了一个字符串数组,保存数字 0~9 对应的英文单词。当用户点击按钮时,从文本框中获取数字,使用 Int32.Parse()方法将其转换为整数类型。然后,根据该数字在数组中寻找对应的英文单词,并将其显示在标签中。
相关问题
java编写窗体应用程序,安排一个文本框、一个按钮和一个标签,从文本框录入一个数字(0~9),点击按钮将其对应的英文单词(如:zero,one等)显示在标签中。
好的,这是一个非常基础的GUI编程问题,以下是java实现的一种方式:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class NumberToWordConverter extends JFrame implements ActionListener {
private JLabel label;
private JTextField textField;
private JButton button;
public NumberToWordConverter() {
setTitle("数字转英文单词");
setLayout(new FlowLayout());
label = new JLabel("输入数字:");
add(label);
textField = new JTextField(5);
add(textField);
button = new JButton("转换");
button.addActionListener(this);
add(button);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String input = textField.getText();
int num = Integer.parseInt(input);
String[] words = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
if (num >= 0 && num <= 9) {
label.setText(words[num]);
} else {
label.setText("输入有误!");
}
}
public static void main(String[] args) {
new NumberToWordConverter();
}
}
```
上述代码创建了一个NumberToWordConverter类,继承了JFrame类,并实现了ActionListener接口。其中,包括一个标签、一个文本框和一个按钮,并在按钮被点击时触发actionPerformed方法进行事件处理。在方法中,根据输入的数字选择对应英文单词,并在标签中显示结果。此外,还增加了输入检查,如果输入不在0~9范围内,标签会显示“输入有误!”提示信息。
用java编写窗体应用程序,安排一个文本框、一个按钮和一个标签,从文本框录入一个数字(0~9),点击按钮将其对应的英文单词(如:zero,one等)显示在标签中 。进一步,扩展数据的范围(如:0~100),如何修改程序实现翻译。
以下是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();
}
}
```
阅读全文