|
发表于 2024-6-18 01:19:48
|
显示全部楼层
啊,看起来你遇到了一个`ClassCastException`错误,这个错误通常表示在代码某处尝试将一个对象强制转换为不兼容的类型。这种错误在使用类型转换时很常见。你这个问题看起来可能是某种依赖关系的问题,或者可能是类型转换的问题。
但是我注意到你的代码里有几个其他的小问题,例如:
1. `CodeUtil`方法中的代码逻辑有误。
2. `code`变量的类型及操作有问题。
先来解决`CodeUtil`方法中的问题:
- 你创建了两个for循环来生成字符,但没有正确地合成它们。
- 字符数组中需要包括字母和数字,而不是每次循环都重新赋值。
那么,我们可以将`CodeUtil`方法改写如下:
- import java.util.Random;
- public class CodeUtil {
- public static String generateCode() {
- Random r = new Random();
- char[] codeChars = new char[26 * 2 + 10];
- int index = 0;
- // 添加A-Z
- for (char c = 'A'; c <= 'Z'; c++) {
- codeChars[index++] = c;
- }
- // 添加a-z
- for (char c = 'a'; c <= 'z'; c++) {
- codeChars[index++] = c;
- }
- // 添加0-9
- for (char c = '0'; c <= '9'; c++) {
- codeChars[index++] = c;
- }
- // 随机打乱数组
- for (int i = 0; i < codeChars.length; i++) {
- int randomIndex = r.nextInt(codeChars.length);
- char temp = codeChars[i];
- codeChars[i] = codeChars[randomIndex];
- codeChars[randomIndex] = temp;
- }
- // 生成5位随机验证码
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < 5; i++) {
- sb.append(codeChars[r.nextInt(codeChars.length)]);
- }
- return sb.toString();
- }
- }
复制代码
同时记得在你的`LoginJFrame`类中更新调用的部分:
- // 更新rightCode的设置
- String codeStr = CodeUtil.generateCode();
复制代码
此外,还有一个问题是你在`checkUserInfo`方法中,做了一个ArrayList的类型转换,但是并没有进行泛型检查:
- public static boolean checkUserInfo(String tempName, String tempPass, ArrayList<User> list) {
- int listLength = list.size();
- // 遍历数组
- for (int i = 0; i < listLength; i++) {
- // 检查用户名是否存在
- User temp = list.get(i);
- String trueName = temp.getName();
- if (tempName.equals(trueName)) {
- // 如果存在 校验密码
- String truePass = temp.getPassWorld();
- boolean loginResult = tempPass.equals(truePass);
- if (loginResult) {
- return true;
- }
- }
- }
- return false;
- }
复制代码
这样可以让代码更类型安全。
最后,再看看这个类是否有其他问题,以下是调整后的完整代码:
- package xyz.ui;
- import javax.swing.*;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseListener;
- import java.util.ArrayList;
- import java.util.Random;
- public class LoginJFrame extends JFrame implements MouseListener {
- // 创建一个集合存储正确的用户名和密码
- static ArrayList<User> list = new ArrayList<>();
- JButton login = new JButton();
- JButton register = new JButton();
- JTextField username = new JTextField();
- JTextField password = new JTextField();
- JTextField code = new JTextField();
- // 正确的验证码
- JLabel rightCode = new JLabel();
- static {
- list.add(new User("zhangsan", "123"));
- list.add(new User("lisi", "1234"));
- }
- public LoginJFrame() {
- // 初始化界面
- initJFrame();
- // 在这个界面中添加内容
- initView();
- // 让当前界面显示出来
- this.setVisible(true);
- }
- public void initView() {
- // 1. 添加用户名文字
- JLabel usernameText = new JLabel(new ImageIcon("day16-game\\image\\login\\用户名.png"));
- usernameText.setBounds(116, 135, 47, 17);
- this.getContentPane().add(usernameText);
- // 2.添加用户名输入框
- username.setBounds(195, 134, 200, 30);
- this.getContentPane().add(username);
- // 3.添加密码文字
- JLabel passwordText = new JLabel(new ImageIcon("day16-game\\image\\login\\密码.png"));
- passwordText.setBounds(130, 195, 32, 16);
- this.getContentPane().add(passwordText);
- // 4.密码输入框
- password.setBounds(195, 195, 200, 30);
- this.getContentPane().add(password);
- // 验证码提示
- JLabel codeText = new JLabel(new ImageIcon("day16-game\\image\\login\\验证码.png"));
- codeText.setBounds(133, 256, 50, 30);
- this.getContentPane().add(codeText);
- // 验证码的输入框
- code.setBounds(195, 256, 100, 30);
- this.getContentPane().add(code);
- // 设置验证码
- String codeStr = CodeUtil.generateCode();
- rightCode.setText(codeStr);
- rightCode.addMouseListener(this);
- rightCode.setBounds(300, 256, 50, 30);
- this.getContentPane().add(rightCode);
- // 5.添加登录按钮
- login.setBounds(123, 310, 128, 47);
- login.setIcon(new ImageIcon("day16-game\\image\\login\\登录按钮.png"));
- login.setBorderPainted(false);
- login.setContentAreaFilled(false);
- login.addMouseListener(this);
- this.getContentPane().add(login);
- // 6.添加注册按钮
- register.setBounds(256, 310, 128, 47);
- register.setIcon(new ImageIcon("day16-game\\image\\login\\注册按钮.png"));
- register.setBorderPainted(false);
- register.setContentAreaFilled(false);
- register.addMouseListener(this);
- this.getContentPane().add(register);
- // 7.添加背景图片
- JLabel background = new JLabel(new ImageIcon("day16-game\\image\\login\\background.png"));
- background.setBounds(0, 0, 470, 390);
- this.getContentPane().add(background);
- }
- public void initJFrame() {
- this.setSize(488, 430);
- this.setTitle("拼图游戏 V1.0登录");
- this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
- this.setLocationRelativeTo(null);
- this.setAlwaysOnTop(true);
- this.setLayout(null);
- }
- public void showJDialog(String content) {
- JDialog jDialog = new JDialog();
- jDialog.setSize(200, 150);
- jDialog.setAlwaysOnTop(true);
- jDialog.setLocationRelativeTo(null);
- jDialog.setModal(true);
- JLabel warning = new JLabel(content);
- warning.setBounds(0, 0, 200, 150);
- jDialog.getContentPane().add(warning);
- jDialog.setVisible(true);
- }
- public static boolean checkUserInfo(String tempName, String tempPass, ArrayList<User> list) {
- int listLength = list.size();
- for (int i = 0; i < listLength; i++) {
- User temp = list.get(i);
- String trueName = temp.getName();
- if (tempName.equals(trueName)) {
- String truePass = temp.getPassWorld();
- if (tempPass.equals(truePass)) {
- return true;
- }
- }
- }
- return false;
- }
- @Override
- public void mouseClicked(MouseEvent e) {
- if (e.getSource() == login) {
- String tempCode = code.getText();
- String tempName = username.getText();
- String tempPass = password.getText();
- if (tempName.equals("") || tempPass.equals("")) {
- showJDialog("用户名或密码为空!");
- } else if (tempCode.equals("")) {
- showJDialog("验证码不能为空!");
- } else if (!tempCode.equalsIgnoreCase(rightCode.getText())) {
- showJDialog("验证码错误!");
- } else if (!checkUserInfo(tempName, tempPass, list)) {
- showJDialog("用户名或密码错误!");
- } else if (checkUserInfo(tempName, tempPass, list)){
- showJDialog("登入成功!可以开始愉快的play了");
- this.setVisible(false);
- new GameJFrame();
- } else {
- System.out.println("??代码报错,请联系管理员");
- showJDialog("??代码报错,请联系管理员");
- }
- } else if (e.getSource() == register) {
- System.out.println("注册");
- } else if (e.getSource() == code) {
- System.out.println("验证码");
- }
- }
- // 长按不松
- @Override
- public void mousePressed(MouseEvent e) {}
- // 松开按钮
- @Override
- public void mouseReleased(MouseEvent e) {}
- // 鼠标划入
- @Override
- public void mouseEntered(MouseEvent e) {}
- // 鼠标划出
- @Override
- public void mouseExited(MouseEvent e) {}
- }
复制代码
这些修改应该能解决你提到的问题。如果还有其他问题,可以再详细描述一下。祝你编程顺利!
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 |
|