因为代码比较长,提取了一部分核心的发出来
下面有程序运行图,涵盖了一些具体功能
问题是在事件监听器中,用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 也没有反应