标题 应用举例 限时回答问题
在功能实现上还是有问题,在一个问题显示出来,点中某个复选框后,问题切换后,该复选框还是选中的状态,但是点那个复选框,分数还是能加一,我在网上找的解决方法是调用temp.setSelected(false);但这样运行后,每次选中复选框,那个勾选的对号不显示,而且题目显示也有问题,每次都跳过一个问题,只显示了一半问题,这个问题还在找答案中。
package 回答问题窗口;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class QuestionWindow extends JFrame implements ActionListener,ItemListener{
JLabel showtime,showscore;
JTextArea showquestion;
JCheckBox box[];
JButton redo;
JPanel panel;
static Timer time;
File file=new File("问题.txt");
FileReader reader;
BufferedReader buffer;
String answer=null;
int max=8,score=0;
public QuestionWindow() {
panel=new JPanel();
showtime=new JLabel();
showscore=new JLabel();
showquestion=new JTextArea(2,20);
box=new JCheckBox[4];
for(char a='A';a<='D';a++) {
String temp;
temp=String.valueOf(a);
box[a-'A']=new JCheckBox(temp);
}
redo=new JButton("再做一遍");
time=new Timer(1000,this);
redo.addActionListener(this);
init();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
void init() {
this.add(panel);
panel.setLayout(new FlowLayout());
panel.add(showtime);
panel.add(showquestion);
for(int i=0;i<4;i++) {
box[i].addItemListener(this);
panel.add(box[i]);
}
showscore.setText("分数:"+String.valueOf(score));
panel.add(showscore);
panel.add(redo);
redo.setEnabled(false);
setFilePoint();
}
public void setFilePoint() { //文件指针放在第一行
try {
reader=new FileReader(file);
buffer=new BufferedReader(reader);
max=8;
score=0;
showQuestion();
redo.setEnabled(false);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
void showQuestion() { //显示题目
String temp1=null,temp2=null,temp3=null;
showquestion.setText(null);
try {
temp1=buffer.readLine();
if(temp1==null) {
answer=null;
buffer.close();
reader.close();
showquestion.setText("题目完毕");
time.stop();
redo.setEnabled(true);
return;
}
showquestion.setText(temp1);
temp2=buffer.readLine();
showquestion.append('\n'+temp2);
temp3=buffer.readLine();
answer=temp3.replaceAll("-", "");
max=8;
time.start();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("无法读文件");
}
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(time)) {
if(max==0) showQuestion();
showtime.setText("剩:"+String.valueOf(max)+"秒"+" 问题:");
max--;
}
if(e.getSource().equals(redo)) {
setFilePoint();
}
}
@Override
public void itemStateChanged(ItemEvent e) {
JCheckBox temp=(JCheckBox)e.getItemSelectable();
String str=temp.getText();
if(str.compareToIgnoreCase(answer)==0) {
score++;
showscore.setText(null);
showscore.setText("分数:"+String.valueOf(score));
time.stop();
}
showQuestion();
}
}