近期写了一个小项目,需要用到用户的登陆界面与注册
我将登陆界面和注册界面分开来写
先看登陆界面,代码如下:
// 注册buttonOK(登陆的按钮)的ActionEvent事件监听
buttonOK.addActionListener(e -> {
// 从账号框中取出账号
String accountidtxt = txtAccountId.getText();
// 创建AccountDaoImp对象
AccountDao accountDao = new AccountDaoImp();
// 通过AccountDaoImp对象调用findByid,该方法通过用户账号查询数据库信息
Account account = accountDao.findByid(accountidtxt);
// 从密码框中取出密码
String passwordText =new String(txtPassword.getPassword());
// 判断账号密码是否正确,如果对象account返回的不为空,并且账号的密码和数据库中的密码一样,则说明账号登陆成功
if (account != null && passwordText.equals(account.getPassword())){
System.out.println("登陆成功!");
//这里要修改
}
// 如果输入的账号密码都为空,则弹出创建账号为空标签
else if (accountidtxt.isEmpty() || passwordText.isEmpty()){
// 账号为空标签
JLabel label = new JLabel("注册账号或密码不能为空,请您重新登陆!!");
label.setFont(new Font("微软雅黑",Font.PLAIN,15));
// 创建提示框
JOptionPane.showMessageDialog(null,label,"登陆失败",JOptionPane.PLAIN_MESSAGE);
}
// 反之的登陆失败
else {
// 创建账号密码错误标签
JLabel label = new JLabel("您输入的账号或密码有误,请重新输入!");
label.setFont(new Font("微软雅黑",Font.PLAIN,15));
// 创建提示框
JOptionPane.showMessageDialog(null,label,"登陆失败",JOptionPane.PLAIN_MESSAGE);
}
});
这里只重点写了如何验证用户的登陆,放在了一个监听事件里面,其他的地方大同小异。
再看登陆界面,代码如下:
// 注册buttonOK1(注册的按钮)的ActionEvent事件监听
buttonOK1.addActionListener(e -> {
// 从账号文本输入框中获取数据
String AccountidText = txtAccountId.getText();
// 从密码框中取出密码
String PasswordText = new String(txtPassword.getPassword());
// 查询账号,查看账号是否唯一
// 创建AccountDaoImp对象
AccountDao accountDao = new AccountDaoImp();
// 通过AccountDaoImp对象调用findByid,该方法通过用户账号查询数据库信息
Account account = accountDao.findByid(AccountidText);
// 如果输入的账号密码都为空,则弹出创建账号为空标签
if (AccountidText.isEmpty() || PasswordText.isEmpty()){
// 创建账号为空标签
JLabel label = new JLabel("注册账号或密码不能为空,请您重新注册!!");
label.setFont(new Font("微软雅黑",Font.PLAIN,15));
// 创建提示框
JOptionPane.showMessageDialog(null,label,"注册失败",JOptionPane.PLAIN_MESSAGE);
}
// 将用户输入的账号调用findByid查询,如果对象account返回的不为空,则说明账号已经被注册过了
else if (account != null){
// 账号已经被注册标签
JLabel label = new JLabel("该账号已被注册,请您重新注册!!");
label.setFont(new Font("微软雅黑",Font.PLAIN,15));
// 创建提示框
JOptionPane.showMessageDialog(null,label,"注册失败",JOptionPane.PLAIN_MESSAGE);
}
// 注册成功
else {
System.out.println("注册成功!");
// 进入到完善信息的窗口
InformationFrame informationFrame = new InformationFrame();
informationFrame.setVisible(true);
setVisible(false);
}
});
这里只重点写了如何验证用户的注册,放在了一个监听事件里面,其他的地方大同小异。
运行结果:
以上内容仅供参考学习,如有侵权请联系我删除!
如果这篇文章对您有帮助,左下角的大拇指就是对博主最大的鼓励。
您的鼓励就是博主最大的动力!