鱼C论坛

 找回密码
 立即注册
查看: 67|回复: 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) {

    }
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 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 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-6-26 22:48

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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