Leonhap 2022-11-05 19:34 采纳率: 100%
浏览 59
已结题

java 在调用其他类方法的时候JFrame界面卡住应该如何处理

因为代码比较长,提取了一部分核心的发出来
下面有程序运行图,涵盖了一些具体功能

问题是在事件监听器中,用swithch-case判断监听事件,有两个JFrame窗口
但是每当程序加载这四行代码的时候,两个JFrame窗口就会卡住(点击任何按钮或者编辑信息都没有反应)
我测试了很多次,只要不调用这四行代码就不会卡住,请问怎么样解决这种问题?

//调用的是下面这四行代码
Student student = new Student(userName, passWord);
String[] answers = student.exam(paper); //关于 paper: ArrayList<Question> paper = Bank.getPaper(questionNum); //随机生成试卷
int score=teacher.Checkpaper(paper, answers);
System.out.println(student.getUsername() + "的成绩为:" + score);


/*
       // 这一部分是getPaper方法的代码
        public ArrayList<Question>getPaper(int QuestionNum){ //ArratList数组类型的getPaper方法
        返回question类型的paper
        HashSet<Question> paper = new HashSet<Question>();//定义HashSet存储试卷paper,以避免重复
        利用ArrayList的有序性使得题目体现题序
        ArrayList<Question> questionBank = new ArrayList<Question>(this.Questionbank);
        while (paper.size() < QuestionNum) {
            int index = new Random().nextInt(this.Questionbank.size());//Questionbank.size()题库长度
            Random().nextInt(integer n) 生成一个随机的integer值,该值介于[0,n)的区间
            paper.add(questionBank.get(index));
        }
        return new ArrayList<Question>(paper);
    }
*/

```java
/*
 Question是一个关于类的数据类型
public class Question {
    private String Title;//题干
    private String Answer;//正确答案
    
    public Question(String Title,String Answer) { //Question含参构造
    this.Title=Title;
    this.Answer=Answer;
    }
    public String getTitle() {
        return Title;
    }
    public String getAnswer() {
        return Answer;
    }
*/
下面附上student,teacher类的一些代码
Student:

```java
public class Student{
public String[] exam(ArrayList<Question>paper) {
        Scanner input=new Scanner(System.in);
        String[] answers=new String[paper.size()];
        
        for (int i = 0; i < paper.size(); i++) {
            Question question = paper.get(i);//paper.get(i)访问i+1个元素
            System.out.println((i + 1) + "." + question.getTitle());
            System.out.println("请输入你的答案:");
            answers[i] = input.nextLine();
            //input.close();
        }
        return answers;
    }
}

Teacher:

public class Teacher {
    public int Checkpaper(ArrayList<Question> paper, String[] answers){
        int score = 0;//初始化分数score为0
        for (int i = 0; i < paper.size(); i++) {
            Question question = paper.get(i);//question,paper的类型等同
            if (question.getAnswer().equalsIgnoreCase(answers[i]))// 比较时忽略选项的大小写
                score += 100 / paper.size();
        }
        return score;
    }

}

运行结果: 再点击获取试题加载前面四行代码后,两个JFrame窗口都卡住关不掉了,点击里面的button 也没有反应

img

  • 写回答

2条回答 默认 最新

  • CSDN专家-sinJack 2022-11-05 19:57
    关注
    String[] answers = student.exam(paper);
    int score=teacher.Checkpaper(paper, answers);
    

    这里是不是报错了,卡在调用方法中。
    Debug调试这几行代码,以及调用的方法内具体实现。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 11月15日
  • 已采纳回答 11月7日
  • 修改了问题 11月5日
  • 修改了问题 11月5日
  • 展开全部

悬赏问题

  • ¥20 西门子S7-Graph,S7-300
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改