设个一个带用户名与密码登陆界面,用户可以点击登陆,如果用户与密码为空则给出“请输入提示”;如果没有用户名则可点击注册,注册上有用户名与密的输入,点击注册按钮将判断用户名与密码输入项是否为空,为空给出提示,然后验证密码要两次验证,不一致则给出提示。请用Java完成以上代码
时间: 2024-09-11 07:17:29 浏览: 33
js判断登陆用户名及密码是否为空的简单实例
在Java中,你可以使用Swing库创建图形用户界面(GUI),并结合事件监听来处理用户的输入。以下是一个简单的例子,展示了如何创建这样的登录注册界面:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class LoginRegisterFrame extends JFrame {
private JTextField usernameField;
private JPasswordField passwordField;
private JPasswordField confirmPasswordField;
public LoginRegisterFrame() {
// 创建窗口
setTitle("登录注册");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 创建组件
JPanel mainPanel = new JPanel();
JPanel loginPanel = new JPanel();
JPanel registerPanel = new JPanel();
JLabel usernameLabel = new JLabel("用户名:");
usernameField = new JTextField();
JLabel passwordLabel = new JLabel("密码:");
passwordField = new JPasswordField();
JLabel confirmPasswordLabel = new JLabel("确认密码:");
confirmPasswordField = new JPasswordField();
JButton loginButton = new JButton("登录");
JButton registerButton = new JButton("注册");
// 设置布局
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
loginPanel.setLayout(new GridBagLayout());
registerPanel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 2;
gbc.fill = GridBagConstraints.HORIZONTAL;
loginPanel.add(Box.createHorizontalStrut(5), gbc);
loginPanel.add(usernameLabel, gbc);
loginPanel.add(Box.createHorizontalStrut(5), gbc);
loginPanel.add(usernameField, gbc);
loginPanel.add(passwordLabel, gbc);
loginPanel.add(passwordField, gbc);
gbc.gridy++;
loginPanel.add(confirmPasswordLabel, gbc);
loginPanel.add(confirmPasswordField, gbc);
gbc.gridy++;
loginPanel.add(loginButton, gbc);
registerPanel.add(Box.createHorizontalStrut(5), gbc);
registerPanel.add(usernameLabel, gbc);
registerPanel.add(Box.createHorizontalStrut(5), gbc);
registerPanel.add(usernameField, gbc);
registerPanel.add(passwordLabel, gbc);
registerPanel.add(passwordField, gbc);
registerPanel.add(confirmPasswordLabel, gbc);
registerPanel.add(confirmPasswordField, gbc);
gbc.gridy++;
registerPanel.add(registerButton, gbc);
// 事件监听
loginButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String username = usernameField.getText();
String password = new String(passwordField.getPassword());
if (username.isEmpty() || password.isEmpty()) {
JOptionPane.showMessageDialog(null, "请输入用户名和密码!");
} else if (!password.equals(new String(confirmPasswordField.getPassword()))) {
JOptionPane.showMessageDialog(null, "密码不一致,请再次输入!");
} else {
// 这里仅作示例,实际应用中需要连接到数据库或服务器验证
System.out.println("登录成功,用户名: " + username);
}
}
});
registerButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (usernameField.getText().isEmpty() || passwordField.getPassword().length == 0 || confirmPasswordField.getPassword().length == 0) {
JOptionPane.showMessageDialog(null, "所有字段都不能为空!");
} else if (!new String(passwordField.getPassword()).equals(new String(confirmPasswordField.getPassword()))) {
JOptionPane.showMessageDialog(null, "密码不一致,请再次输入!");
} else {
// 注册过程略...
System.out.println("注册成功,用户名: " + usernameField.getText());
}
}
});
// 添加面板到主窗口
mainPanel.add(loginPanel);
mainPanel.add(Box.createVerticalStrut(10));
mainPanel.add(registerPanel);
setContentPane(mainPanel);
setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
try {
LoginRegisterFrame frame = new LoginRegisterFrame();
} catch (Exception e) {
e.printStackTrace();
}
});
}
}
```
阅读全文