我写了一个简单的自动阅卷打分的程序,每道题目有四个答案备选,当选好答案并点击“next”摁钮后给出下一道题目以及该题目的备选答案,答案的选择采用Checkbox来选择。 问题就出在这里。当选择好一个答案并点击“next”摁钮进入下一道题目后,你所点击的那个Checkbox还处于被选择状态。我想在进入下一道题目后把所有checkbox都变为不被选中状态,于是查找API说明,找到setState()这个方法,于是就用checkbox[i].setState(false)来把所有checkbox设置为非选状态。但是奇怪的是竟然没有任何效果,再去查找API说明,觉得就是这个方法没错,问题出在哪里呢?小弟我没分了,实在抱歉,恳请大家帮我看看。编译器是Eclipse 3.4 相应代码段:@Override
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==next) //点击“next”摁钮时的反映
{
if(!(s.startsWith("endend")))
{
reading();
for(int i=0;i<4;i++)
{
checkbox[i].setState(false);
checkbox[i].setEnabled(true);
}
this.validate();
}
}
else if(e.getSource()==start && restart.equals("begin")) //点击开始摁钮时的反应
{
restart = "last";
score=0;
grade.setText("得分:"+score);
try
{
fr = new FileReader(f);
bfr = new BufferedReader(fr);
reading();
this.validate();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==next) //点击“next”摁钮时的反映
{
if(!(s.startsWith("endend")))
{
reading();
for(int i=0;i<4;i++)
{
checkbox[i].setState(false);
checkbox[i].setEnabled(true);
}
this.validate();
}
}
else if(e.getSource()==start && restart.equals("begin")) //点击开始摁钮时的反应
{
restart = "last";
score=0;
grade.setText("得分:"+score);
try
{
fr = new FileReader(f);
bfr = new BufferedReader(fr);
reading();
this.validate();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}
在下一步按钮上监听状态来改变checkbox的状态吧next.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent evt) {
//鼠标单击next按钮时执行的代码
}
});
如果是B/S,这样操作只有页面刷新后才会显示出效果,而未执行完就没有刷新,肯定没有效果,除非用js实现
我刚学java,C/S和B/S的细节还不是很清楚。我也说不清楚,因为我只是一个初学者,我还是把我所有代码贴出来吧。我刚刚把你给我那段代码写入actionPerformed(ActionEvent e)里,结果还是无效。
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.StringTokenizer;import javax.swing.*;public class EWindows extends Frame implements ActionListener, ItemListener
{
String str[] = new String[7],s;
FileReader fr;
TextField subject,grade;
Checkbox[] checkbox;
CheckboxGroup test = new CheckboxGroup();
int score;
String restart = "begin";
BufferedReader bfr;
Button start,next;
File f = new File("D:\\编程工具\\JAVA程序\\english.txt");
private class WindowCloser extends WindowAdapter
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
}
public EWindows(String ftitle) //构造函数,设置界面
{
super(ftitle);
start = new Button("开始");
next = new Button("下一题目");
start.addActionListener(this);
next.addActionListener(this);
subject = new TextField(70);
grade = new TextField(10);
checkbox =new Checkbox[4];
for(int i=0;i<4;i++)
{
checkbox[i] = new Checkbox("",false,test);
checkbox[i].addItemListener(this);
}
Box box = Box.createVerticalBox();
Panel p1=new Panel(),p2=new Panel(),p3=new Panel(),p4 =new Panel();
p1.add(new Label("题目"));
p1.add(subject);
p2.add(new Label("成绩"));
p2.add(grade);
p3.add(new Label("选择答案"));
for(int i=0;i<4;i++)
{
p3.add(checkbox[i]);
}
p4.add(start);
p4.add(next);
box.add(p1);
box.add(p2);
box.add(p3);
box.add(p4);
try
{
fr = new FileReader(f);
bfr = new BufferedReader(fr);
}
catch (IOException e)
{
e.printStackTrace();
}
this.add(box,BorderLayout.CENTER);
this.setBounds(200,200,600,300);
this.addWindowListener(new WindowCloser());
this.setVisible(true);
this.validate();
}
public void reading() //读取函数,读取对应文件内的题目
{
int i=0;
try
{
s=bfr.readLine();
if(!(s.startsWith("endend"))) //没有读到末尾时
{
StringTokenizer st = new StringTokenizer(s,"#");
while(st.hasMoreTokens())
{
str[i] = st.nextToken();
i++;
}
subject.setText(str[0]);
for(int j=0;j<4;j++)
{
checkbox[j].setLabel(str[j+1]);
}
}
else if(s.startsWith("endend")) //读到末尾时
{
subject.setText("考试完毕");
for(int j=0;j<4;j++)
{
checkbox[j].setLabel("end");
bfr.close();
fr.close();
}
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
@Override
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==next) //点击“next”摁钮时的反映
{
if(!(s.startsWith("endend")))
{
reading();
for(int i=0;i<4;i++)
{
checkbox[i].setEnabled(true);
}
this.validate();
}
}
else if(e.getSource()==start && restart.equals("begin")) //点击开始摁钮时的反应
{
restart = "last";
score=0;
grade.setText("得分:"+score);
try
{
fr = new FileReader(f);
bfr = new BufferedReader(fr);
reading();
this.validate();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
} @Override
public void itemStateChanged(ItemEvent e) //点击checkbox时的反应
{
for(int i=0;i<4;i++)
{
if(checkbox[i].getState() && checkbox[i].getLabel().equals(str[5]))
{
score++;
grade.setText("得分:"+score);
}
checkbox[i].setEnabled(false);
}
}
}