java 获取登陆框里的账号密码
时间: 2023-08-13 15:03:04 浏览: 52
Java获取登录框里的账号密码需要使用Java GUI编程,具体实现步骤如下:
1. 创建一个JFrame窗口,用于显示登录框。
2. 在JFrame窗口中添加两个JLabel标签和两个JTextField文本框,分别用于显示“账号”、“密码”和输入账号密码。
3. 创建一个JButton按钮,用于表示“登录”操作。
4. 给“登录”按钮添加一个事件监听器,在监听器中获取账号密码文本框中的值,并进行相应的处理。
具体的代码实现可以参考下面的示例代码:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class LoginFrame extends JFrame implements ActionListener {
private JLabel accountLabel;
private JLabel passwordLabel;
private JTextField accountField;
private JTextField passwordField;
private JButton loginButton;
public LoginFrame() {
super("登录");
// 初始化组件
accountLabel = new JLabel("账号:");
passwordLabel = new JLabel("密码:");
accountField = new JTextField(20);
passwordField = new JTextField(20);
loginButton = new JButton("登录");
// 设置布局
setLayout(new GridLayout(3, 2));
// 添加组件
add(accountLabel);
add(accountField);
add(passwordLabel);
add(passwordField);
add(loginButton);
// 添加事件监听器
loginButton.addActionListener(this);
// 设置窗口大小和位置
setSize(300, 150);
setLocationRelativeTo(null);
// 显示窗口
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// 获取账号密码文本框中的值
String account = accountField.getText();
String password = passwordField.getText();
// 处理登录逻辑
// ...
// 输出账号密码
System.out.println("账号:" + account + ",密码:" + password);
}
public static void main(String[] args) {
new LoginFrame();
}
}
```