鱼C论坛

 找回密码
 立即注册
查看: 440|回复: 1

java调用这个类莫名报错

[复制链接]
发表于 2024-6-18 01:19:09 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 IShehua 于 2024-6-18 01:26 编辑

//报错提示
Module 'day16-game' production: java.lang.ClassCastException: class org.jetbrains.jps.builders.java.dependencyView.TypeRepr$PrimitiveType cannot be cast to class org.jetbrains.jps.builders.java.dependencyView.TypeRepr$ClassType (org.jetbrains.jps.builders.java.dependencyView.TypeRepr$PrimitiveType and org.jetbrains.jps.builders.java.dependencyView.TypeRepr$ClassType are in unnamed module of loader java.net.URLClassLoader @31befd9f)

//内容
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();

        //设置内容
        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对象管理文字并添加到弹框当中
        JLabel warning = new JLabel(content);
        warning.setBounds(0, 0, 200, 150);
        jDialog.getContentPane().add(warning);

        //让弹框展示出来
        jDialog.setVisible(true);
    }

    public static String CodeUtil() {
        Random r = new Random();
        char[] code = new char[52];
        for (int i = 0; i < 26; i++) {
            code[i] = (char) (i + 65);
        }
        for (int i = 0; i < 26; i++) {
            code[i + 26] = (char) (i + 97);
        }

        for (int i = 0; i < code.length; i++) {
            int index = r.nextInt(code.length);
            char temp = code[i];
            code[i] = code[index];
            code[index] = temp;
        }

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 5; i++) {
            sb.append(code[i]);
        }
        int random = r.nextInt(48, 58);
        sb.setCharAt(r.nextInt(5), (char) random);

        return sb.toString();
    }

    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;
    }

    //点击按钮
    @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) {

    }
}
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-6-18 01:19:48 | 显示全部楼层
啊,看起来你遇到了一个`ClassCastException`错误,这个错误通常表示在代码某处尝试将一个对象强制转换为不兼容的类型。这种错误在使用类型转换时很常见。你这个问题看起来可能是某种依赖关系的问题,或者可能是类型转换的问题。

但是我注意到你的代码里有几个其他的小问题,例如:
1. `CodeUtil`方法中的代码逻辑有误。
2. `code`变量的类型及操作有问题。

先来解决`CodeUtil`方法中的问题:

- 你创建了两个for循环来生成字符,但没有正确地合成它们。
- 字符数组中需要包括字母和数字,而不是每次循环都重新赋值。

那么,我们可以将`CodeUtil`方法改写如下:

  1. import java.util.Random;

  2. public class CodeUtil {
  3.     public static String generateCode() {
  4.         Random r = new Random();
  5.         char[] codeChars = new char[26 * 2 + 10];
  6.         int index = 0;

  7.         // 添加A-Z
  8.         for (char c = 'A'; c <= 'Z'; c++) {
  9.             codeChars[index++] = c;
  10.         }

  11.         // 添加a-z
  12.         for (char c = 'a'; c <= 'z'; c++) {
  13.             codeChars[index++] = c;
  14.         }

  15.         // 添加0-9
  16.         for (char c = '0'; c <= '9'; c++) {
  17.             codeChars[index++] = c;
  18.         }

  19.         // 随机打乱数组
  20.         for (int i = 0; i < codeChars.length; i++) {
  21.             int randomIndex = r.nextInt(codeChars.length);
  22.             char temp = codeChars[i];
  23.             codeChars[i] = codeChars[randomIndex];
  24.             codeChars[randomIndex] = temp;
  25.         }

  26.         // 生成5位随机验证码
  27.         StringBuilder sb = new StringBuilder();
  28.         for (int i = 0; i < 5; i++) {
  29.             sb.append(codeChars[r.nextInt(codeChars.length)]);
  30.         }

  31.         return sb.toString();
  32.     }
  33. }
复制代码


同时记得在你的`LoginJFrame`类中更新调用的部分:

  1. // 更新rightCode的设置
  2. String codeStr = CodeUtil.generateCode();
复制代码


此外,还有一个问题是你在`checkUserInfo`方法中,做了一个ArrayList的类型转换,但是并没有进行泛型检查:

  1. public static boolean checkUserInfo(String tempName, String tempPass, ArrayList<User> list) {
  2.     int listLength = list.size();
  3.     // 遍历数组
  4.     for (int i = 0; i < listLength; i++) {
  5.         // 检查用户名是否存在
  6.         User temp = list.get(i);
  7.         String trueName = temp.getName();
  8.         if (tempName.equals(trueName)) {
  9.             // 如果存在 校验密码
  10.             String truePass = temp.getPassWorld();
  11.             boolean loginResult = tempPass.equals(truePass);
  12.             if (loginResult) {
  13.                 return true;
  14.             }
  15.         }
  16.     }
  17.     return false;
  18. }
复制代码


这样可以让代码更类型安全。

最后,再看看这个类是否有其他问题,以下是调整后的完整代码:

  1. package xyz.ui;

  2. import javax.swing.*;
  3. import java.awt.event.MouseEvent;
  4. import java.awt.event.MouseListener;
  5. import java.util.ArrayList;
  6. import java.util.Random;

  7. public class LoginJFrame extends JFrame implements MouseListener {
  8.     // 创建一个集合存储正确的用户名和密码
  9.     static ArrayList<User> list = new ArrayList<>();

  10.     JButton login = new JButton();
  11.     JButton register = new JButton();

  12.     JTextField username = new JTextField();
  13.     JTextField password = new JTextField();
  14.     JTextField code = new JTextField();

  15.     // 正确的验证码
  16.     JLabel rightCode = new JLabel();

  17.     static {
  18.         list.add(new User("zhangsan", "123"));
  19.         list.add(new User("lisi", "1234"));
  20.     }

  21.     public LoginJFrame() {
  22.         // 初始化界面
  23.         initJFrame();

  24.         // 在这个界面中添加内容
  25.         initView();

  26.         // 让当前界面显示出来
  27.         this.setVisible(true);
  28.     }

  29.     public void initView() {
  30.         // 1. 添加用户名文字
  31.         JLabel usernameText = new JLabel(new ImageIcon("day16-game\\image\\login\\用户名.png"));
  32.         usernameText.setBounds(116, 135, 47, 17);
  33.         this.getContentPane().add(usernameText);

  34.         // 2.添加用户名输入框
  35.         username.setBounds(195, 134, 200, 30);
  36.         this.getContentPane().add(username);

  37.         // 3.添加密码文字
  38.         JLabel passwordText = new JLabel(new ImageIcon("day16-game\\image\\login\\密码.png"));
  39.         passwordText.setBounds(130, 195, 32, 16);
  40.         this.getContentPane().add(passwordText);

  41.         // 4.密码输入框
  42.         password.setBounds(195, 195, 200, 30);
  43.         this.getContentPane().add(password);

  44.         // 验证码提示
  45.         JLabel codeText = new JLabel(new ImageIcon("day16-game\\image\\login\\验证码.png"));
  46.         codeText.setBounds(133, 256, 50, 30);
  47.         this.getContentPane().add(codeText);

  48.         // 验证码的输入框
  49.         code.setBounds(195, 256, 100, 30);
  50.         this.getContentPane().add(code);

  51.         // 设置验证码
  52.         String codeStr = CodeUtil.generateCode();
  53.         rightCode.setText(codeStr);
  54.         rightCode.addMouseListener(this);
  55.         rightCode.setBounds(300, 256, 50, 30);
  56.         this.getContentPane().add(rightCode);

  57.         // 5.添加登录按钮
  58.         login.setBounds(123, 310, 128, 47);
  59.         login.setIcon(new ImageIcon("day16-game\\image\\login\\登录按钮.png"));
  60.         login.setBorderPainted(false);
  61.         login.setContentAreaFilled(false);
  62.         login.addMouseListener(this);
  63.         this.getContentPane().add(login);

  64.         // 6.添加注册按钮
  65.         register.setBounds(256, 310, 128, 47);
  66.         register.setIcon(new ImageIcon("day16-game\\image\\login\\注册按钮.png"));
  67.         register.setBorderPainted(false);
  68.         register.setContentAreaFilled(false);
  69.         register.addMouseListener(this);
  70.         this.getContentPane().add(register);

  71.         // 7.添加背景图片
  72.         JLabel background = new JLabel(new ImageIcon("day16-game\\image\\login\\background.png"));
  73.         background.setBounds(0, 0, 470, 390);
  74.         this.getContentPane().add(background);
  75.     }

  76.     public void initJFrame() {
  77.         this.setSize(488, 430);
  78.         this.setTitle("拼图游戏 V1.0登录");
  79.         this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  80.         this.setLocationRelativeTo(null);
  81.         this.setAlwaysOnTop(true);
  82.         this.setLayout(null);
  83.     }

  84.     public void showJDialog(String content) {
  85.         JDialog jDialog = new JDialog();
  86.         jDialog.setSize(200, 150);
  87.         jDialog.setAlwaysOnTop(true);
  88.         jDialog.setLocationRelativeTo(null);
  89.         jDialog.setModal(true);

  90.         JLabel warning = new JLabel(content);
  91.         warning.setBounds(0, 0, 200, 150);
  92.         jDialog.getContentPane().add(warning);

  93.         jDialog.setVisible(true);
  94.     }

  95.     public static boolean checkUserInfo(String tempName, String tempPass, ArrayList<User> list) {
  96.         int listLength = list.size();
  97.         for (int i = 0; i < listLength; i++) {
  98.             User temp = list.get(i);
  99.             String trueName = temp.getName();
  100.             if (tempName.equals(trueName)) {
  101.                 String truePass = temp.getPassWorld();
  102.                 if (tempPass.equals(truePass)) {
  103.                     return true;
  104.                 }
  105.             }
  106.         }
  107.         return false;
  108.     }

  109.     @Override
  110.     public void mouseClicked(MouseEvent e) {
  111.         if (e.getSource() == login) {
  112.             String tempCode = code.getText();
  113.             String tempName = username.getText();
  114.             String tempPass = password.getText();

  115.             if (tempName.equals("") || tempPass.equals("")) {
  116.                 showJDialog("用户名或密码为空!");
  117.             } else if (tempCode.equals("")) {
  118.                 showJDialog("验证码不能为空!");
  119.             } else if (!tempCode.equalsIgnoreCase(rightCode.getText())) {
  120.                 showJDialog("验证码错误!");
  121.             } else if (!checkUserInfo(tempName, tempPass, list)) {
  122.                 showJDialog("用户名或密码错误!");
  123.             } else if (checkUserInfo(tempName, tempPass, list)){
  124.                 showJDialog("登入成功!可以开始愉快的play了");
  125.                 this.setVisible(false);
  126.                 new GameJFrame();
  127.             } else {
  128.                 System.out.println("??代码报错,请联系管理员");
  129.                 showJDialog("??代码报错,请联系管理员");
  130.             }
  131.         } else if (e.getSource() == register) {
  132.             System.out.println("注册");
  133.         } else if (e.getSource() == code) {
  134.             System.out.println("验证码");
  135.         }
  136.     }

  137.     // 长按不松
  138.     @Override
  139.     public void mousePressed(MouseEvent e) {}

  140.     // 松开按钮
  141.     @Override
  142.     public void mouseReleased(MouseEvent e) {}

  143.     // 鼠标划入
  144.     @Override
  145.     public void mouseEntered(MouseEvent e) {}

  146.     // 鼠标划出
  147.     @Override
  148.     public void mouseExited(MouseEvent e) {}
  149. }
复制代码


这些修改应该能解决你提到的问题。如果还有其他问题,可以再详细描述一下。祝你编程顺利!

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-3 15:43

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表