用java实现在文本框里输入一个数字,然后按回车键打印在控制台功能
时间: 2024-05-21 11:10:53 浏览: 13
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("Enter Number and Press Enter");
JTextField textField = new JTextField();
textField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String text = textField.getText();
int number = Integer.parseInt(text);
System.out.println("Entered Number: " + number);
textField.setText("");
}
});
frame.add(textField);
frame.setSize(300, 100);
frame.setVisible(true);
}
}
相关问题
java中在文本框输入文本按回车键把内容打印在控制台
可以通过添加监听器来实现在文本框输入文本后按回车键将内容打印在控制台上,以下是一个示例代码:
```java
import javax.swing.*;
import java.awt.event.*;
public class TextEnterListener implements ActionListener {
private JTextField textField;
public TextEnterListener(JTextField textField) {
this.textField = textField;
}
public void actionPerformed(ActionEvent e) {
System.out.println(textField.getText());
textField.setText("");
}
}
// 在主程序中添加监听器
JTextField textField = new JTextField();
textField.addActionListener(new TextEnterListener(textField));
```
在此示例代码中,`TextEnterListener` 类实现了 `ActionListener` 接口,该接口用于处理用户操作。在 `TextEnterListener` 类中,`actionPerformed` 方法将在回车键被按下时被调用,该方法将获取文本框中的文本并将其打印在控制台上,然后清空文本框。在主程序中,我们将 `TextEnterListener` 实例添加到文本框的监听器列表中,以便在用户按下回车键时调用该监听器的 `actionPerformed` 方法。
使用java创建一个窗体,添加一个文本框并在文本框里输入一个数字,当按下确定时,数字被打印在控制台
以下是使用Java创建窗体,添加一个文本框并在文本框里输入一个数字,并在按下确定按钮时将数字打印在控制台的示例代码:
```java
import javax.swing.*;
import java.awt.event.*;
public class MyFrame extends JFrame implements ActionListener {
JTextField textField;
JButton button;
MyFrame() {
// 创建窗体
super("My Frame");
setSize(300, 150);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 创建文本框和按钮
textField = new JTextField(10);
button = new JButton("确定");
// 添加组件到窗体
getContentPane().add(textField, "Center");
getContentPane().add(button, "South");
// 注册按钮点击事件
button.addActionListener(this);
setVisible(true);
}
// 按钮点击事件处理
public void actionPerformed(ActionEvent e) {
if(e.getSource() == button) {
// 获取文本框输入的数字并打印在控制台
int number = Integer.parseInt(textField.getText());
System.out.println("输入的数字是:" + number);
}
}
public static void main(String[] args) {
new MyFrame();
}
}
```
运行该程序,会弹出一个窗体,里面有一个文本框和一个按钮。在文本框中输入一个数字,然后按下按钮,程序会将该数字打印在控制台上。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)