辛苦各位了TT
JOptionPane.showMessageDialog(this,"测试得分:" + score + "\n测试评语:" + comment);
System.exit(0);
}
辛苦各位了TT
JOptionPane.showMessageDialog(this,"测试得分:" + score + "\n测试评语:" + comment);
System.exit(0);
}
public class TestFrame extends JFrame {//窗体类,继承了JFrame
Exam exam;//一些成员变量
Student student;
JPanel[] panels;
JLabel timeLabel;
JButton button;
boolean timeOver = false;
public TestFrame(Student student) {//构造函数
this.student = student;//形参保存至成员变量
exam = Database.getOneExam();//初始化试卷
setLayout(null);//挂起布局逻辑
setTitle("在线心理测试");//设置窗口标题
setResizable(false);//不可修改窗口大小
setLocationRelativeTo(null);//设置居中显示
setSize(700, 640);//设置窗口大小
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭按钮退出程序
initComponents();//执行函数
new TimingThread().start();//启动timer
setVisible(true);//设置可见
}
//初始化控件
//这都是窗体布局,你亲自运行一下看看效果会比较好,干讲没什么概念
public void initComponents() {
JPanel tipPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
tipPanel.setBounds(0,0,getWidth(),50);
add(tipPanel);
timeLabel = new JLabel("考试时间:");
timeLabel.setFont(new Font("微软雅黑",Font.BOLD,24));
tipPanel.add(timeLabel);
//盒子+滚动布局
Box box = Box.createVerticalBox();
JScrollPane scrollPane = new JScrollPane(box);
scrollPane.setBounds(0,50,getWidth() - 20,getHeight() - 150);
add(scrollPane);
//设置20个面板装20道题
ArrayList<Question> questions = exam.getQuestions();
panels = new JPanel[20];
for (int i = 0; i < panels.length; i++) {
JPanel panel = new JPanel(new FlowLayout());
panels[i] = panel;
box.add(panel);
//获取本题
Question question = questions.get(i);
//题目标题标签
JLabel label = new JLabel((i + 1) + "." + question.getTitle());
panel.add(label);
//创建按钮组
ButtonGroup group = new ButtonGroup();
JRadioButton[] boxes = new JRadioButton[4];
for (int j = 0; j < 4; j++) {
JRadioButton radioButton = new JRadioButton(question.getOption(j));
group.add(radioButton);
panel.add(radioButton);
boxes[j] = radioButton;
}
}
button = new JButton("交卷");
button.setBounds(280,560,100,30);
add(button);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
handIn();//点交卷按钮执行的函数
}
});
}
//交卷
public void handIn(){//这里注释已经够多了,具体哪句看不懂可以粘到网上搜一搜
ArrayList<Question> questions = exam.getQuestions();
for (int i = 0; i < panels.length; i++) {
JPanel panel = panels[i];
//获取题目
Question question = questions.get(i);
//获取面板的子控件
Component[] components = panel.getComponents();
for (int j = 1; j < components.length; j++) {
JRadioButton radioButton = (JRadioButton) components[j];
if (radioButton.isSelected()){
//设置问题答案
question.setAnswer(j - 1);
break;
}
}
}
if (!timeOver){
for (Question question : exam.getQuestions()) {
if(question.getScore() == -1){
if(JOptionPane.showConfirmDialog(this,"还有未答的题,确定要交卷吗?") != 0){
return;
}else {
break;
}
}
}
}
int score = 0;
for (Question question : exam.getQuestions()) {
if (question.getScore() != -1){
score += question.getScore();
}
}
String comment;
if (score < 10){
comment = "重度抑郁";
}else if (score < 30){
comment = "中度抑郁";
}else if (score < 50){
comment = "轻度抑郁";
}else if (score < 70){
comment = "心情不好";
}else{
comment = "身心健康";
}
student.setScore(score);
student.setComment(comment);
//添加测评记录
Database.students.add(student);
Database.writeData();
JOptionPane.showMessageDialog(this,"测试得分:" + score + "\n测试评语:" + comment);
System.exit(0);
}