用java实现填写账号密码和登录按钮界面
时间: 2024-03-07 12:50:26 浏览: 15
好的,您可以使用Swing库来实现一个简单的登录界面。下面是一个示例代码,您可以根据需要进行修改:
```
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class LoginFrame extends JFrame implements ActionListener {
private JTextField textField;
private JPasswordField passwordField;
private JButton loginButton;
public LoginFrame() {
setTitle("登录");
setSize(300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
// 创建面板
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(3, 2, 10, 10));
// 添加账号标签和文本框
JLabel accountLabel = new JLabel("账号:");
textField = new JTextField();
panel.add(accountLabel);
panel.add(textField);
// 添加密码标签和密码框
JLabel passwordLabel = new JLabel("密码:");
passwordField = new JPasswordField();
panel.add(passwordLabel);
panel.add(passwordField);
// 添加登录按钮
loginButton = new JButton("登录");
loginButton.addActionListener(this);
panel.add(loginButton);
// 添加面板到窗口
add(panel);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == loginButton) {
String account = textField.getText();
String password = new String(passwordField.getPassword());
// TODO: 在这里编写登录逻辑
System.out.println("账号:" + account);
System.out.println("密码:" + password);
}
}
public static void main(String[] args) {
new LoginFrame();
}
}
```
运行该程序,将会显示一个带有账号、密码、登录按钮的窗口。当用户点击登录按钮时,程序将会获取文本框和密码框中的内容,并在控制台输出。您可以在程序中添加自己的登录逻辑。