java好看的登录界面
时间: 2023-11-10 15:02:58 浏览: 54
这里提供一个简单的 Java 登录界面的代码片段,你可以根据自己的需要进行修改和美化:
```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 userLabel, passwordLabel;
private JTextField userText;
private JPasswordField passwordText; private JButton loginButton, resetButton;
public LoginFrame() {
setTitle("登录");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(320, 200);
setLocationRelativeTo(null); // 窗口居中
userLabel = new JLabel();
userLabel.setText("用户名:");
userText = new JTextField();
passwordLabel = new JLabel();
passwordLabel.setText("密码:");
passwordText = new JPasswordField();
loginButton = new JButton("登录");
resetButton = new JButton("重置");
setLayout(null);
userLabel.setBounds(40, 30, 80, 30);
userText.setBounds(120, 30, 120, 30);
passwordLabel.setBounds(40, 70, 80, 30);
passwordText.setBounds(120, 70, 120, 30);
loginButton.setBounds(40, 120, 80, 30);
resetButton.setBounds(160, 120, 80, 30);
add(userLabel);
add(userText);
add(passwordLabel);
add(passwordText);
add(loginButton);
add(resetButton);
loginButton.addActionListener(this);
resetButton.addActionListener(this);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == loginButton) {
String userName = userText.getText();
String password = new String(passwordText.getPassword());
// 在这里添加登录逻辑
} else if (e.getSource() == resetButton) {
userText.setText("");
passwordText.setText("");
}
}
public static void main(String[] args) {
new LoginFrame();
}
}
```