用JAVA编写一个程序,当输入用户名和电话按下回车键后,在上面的文本框中显示输入的用户名和密码。
时间: 2024-02-01 21:15:01 浏览: 31
以下是一个简单的Java程序,可以实现你所需的功能:
```java
import javax.swing.*;
import java.awt.event.*;
public class UserInput extends JFrame implements ActionListener{
JTextField tf1, tf2;
JButton btn;
JLabel lbl1, lbl2;
UserInput(){
lbl1 = new JLabel("Username:");
lbl1.setBounds(50,50, 80,30);
add(lbl1);
tf1 = new JTextField();
tf1.setBounds(130,50, 150,30);
add(tf1);
lbl2 = new JLabel("Phone:");
lbl2.setBounds(50,100, 80,30);
add(lbl2);
tf2 = new JTextField();
tf2.setBounds(130,100, 150,30);
add(tf2);
btn = new JButton("Submit");
btn.setBounds(100,150,100,30);
btn.addActionListener(this);
add(btn);
setTitle("User Input");
setSize(350,250);
setLayout(null);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e){
String username = tf1.getText();
String phone = tf2.getText();
JOptionPane.showMessageDialog(this, "Username: " + username + "\nPhone: " + phone);
}
public static void main(String[] args) {
new UserInput();
}
}
```
该程序使用了JFrame和JTextField等Swing控件,通过addActionListener方法为Submit按钮添加了ActionListener,当按钮被点击时,调用actionPerformed方法,从文本框获取输入的用户名和电话,并弹出消息框显示这些信息。
运行该程序,输入用户名和电话,点击Submit按钮,即可看到弹出的消息框显示输入的信息。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)