java登录界面
时间: 2023-06-29 14:14:48 浏览: 37
好的,我可以为你提供一个简单的Java登录界面的代码,你可以参考一下:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class LoginFrame extends JFrame implements ActionListener {
// 创建组件
JLabel userLabel = new JLabel("用户:");
JLabel passwordLabel = new JLabel("密码:");
JTextField userText = new JTextField();
JPasswordField passwordText = new JPasswordField();
JButton loginButton = new JButton("登录");
JButton resetButton = new JButton("重置");
public LoginFrame() {
// 设置窗体相关属性
this.setTitle("Java登录界面");
this.setSize(300, 200);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 设置布局管理器
this.setLayout(null);
// 设置组件位置
userLabel.setBounds(50, 30, 50, 20);
passwordLabel.setBounds(50, 70, 50, 20);
userText.setBounds(110, 30, 120, 20);
passwordText.setBounds(110, 70, 120, 20);
loginButton.setBounds(70, 120, 60, 25);
resetButton.setBounds(170, 120, 60, 25);
// 添加组件
this.add(userLabel);
this.add(passwordLabel);
this.add(userText);
this.add(passwordText);
this.add(loginButton);
this.add(resetButton);
// 给按钮添加监听器
loginButton.addActionListener(this);
resetButton.addActionListener(this);
// 设置窗体可见
this.setVisible(true);
}
// 监听按钮事件
public void actionPerformed(ActionEvent e) {
if (e.getSource() == loginButton) {
String userName = userText.getText();
String password = new String(passwordText.getPassword());
if (userName.equals("admin") && password.equals("admin123")) {
JOptionPane.showMessageDialog(null, "登录成功!");
} else {
JOptionPane.showMessageDialog(null, "用户名或密码错误!");
}
} else if (e.getSource() == resetButton) {
userText.setText("");
passwordText.setText("");
}
}
public static void main(String[] args) {
new LoginFrame();
}
}
```
这是一个简单的登录界面,包含了用户名、密码、登录和重置按钮,你可以根据需要进行修改和美化。