在课本上看了一个用java写的填空题小程序,但该程序不够好,当我们做完一题单击下一题目时,默认选了上一题选择的答案,我想把把它改为单击下一题时,选择框能够清空,没有选择任何答案,希望哪位高手帮帮忙,谢谢,我试过在if(event.getSource()==next)中加入if(checkbox1[j].getState())    checkbox1[j].setState(false);但是不行,为什么?
代码如下:import java.util.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*; 
public class Example10_8
{   public static void main(String args[])
    {  EWindow w=new EWindow();
       w.validate();
    }
}
class EWindow extends Frame implements ActionListener,ItemListener
{  String str[]=new String[7],s;
   FileReader file;
   BufferedReader in;  
   Button start,next;
   Checkbox checkbox1[];
   TextField 题目,分数;
   int score=0; 
   CheckboxGroup age=new CheckboxGroup();
   EWindow()
   {  super("英语单词学习");
      分数=new TextField(10);题目=new TextField(70);
      start=new Button("重新练习");
      start.addActionListener(this);
      next=new Button("下一题目");
      next.addActionListener(this);
      checkbox1=new Checkbox[4];
      for(int i=0;i<=3;i++)
       {  checkbox1[i]=new Checkbox("",false,age);
          checkbox1[i].addItemListener(this); 
       } 
      try {  file=new FileReader("English.txt");
             in=new BufferedReader(file);
          }
      catch(IOException e){}   
      setBounds(20,100,660,300); setVisible(true);
      Box box=Box.createVerticalBox();
      Panel p1=new Panel(),p2=new Panel(),
      p3=new Panel() ,p4=new Panel(),p5=new Panel();
      p1.add(new Label("题目:"));p1.add(题目);
      p2.add(new Label("选择答案:")); 
      for(int i=0;i<=3;i++)
           {  p2.add(checkbox1[i]);
           } 
      p3.add(new Label("您的得分:"));p3.add(分数);
      p4.add(start); p4.add(next);
      box.add(p1);box.add(p2);box.add(p3);box.add(p4);
      addWindowListener(new WindowAdapter()
                        {public void windowClosing(WindowEvent e)
                           {  System.exit(0); 
                           }
      
                        });
     add(box,BorderLayout.CENTER);
     reading();
   } 
   public void reading()
   {   int i=0; 
       try { s=in.readLine();
             if(!(s.startsWith("endend")))
                 {  StringTokenizer tokenizer=new StringTokenizer(s,"#"); 
                    while(tokenizer.hasMoreTokens())
                        {  str[i]=tokenizer.nextToken();
                           i++;
                        }
                     题目.setText(str[0]);
                     for(int j=1;j<=4;j++)
                       {  checkbox1[j-1].setLabel(str[j]);
                       } 
                  }
              else if(s.startsWith("endend"))
                  {  题目.setText("学习完毕"); 
                     for(int j=0;j<4;j++)
                       {  checkbox1[j].setLabel("end"); 
                          in.close();file.close();
                       } 
                  }
            }
         catch(Exception exp){ 题目.setText("无试题文件") ; } 
   }
   public void actionPerformed(ActionEvent event)
   {  if(event.getSource()==start)
         {  score=0;
            分数.setText("得分: "+score);
            try {  file=new FileReader("English.txt");
                   in=new BufferedReader(file);
                }
            catch(IOException e){}  
            reading(); 
         }
      if(event.getSource()==next)
         {  reading();
            for(int j=0;j<4;j++)
             {  checkbox1[j].setEnabled(true);             }        
         }
   }
   public void itemStateChanged(ItemEvent e)
   {  for(int j=0;j<4;j++)
        { if(checkbox1[j].getLabel().equals(str[5])&&checkbox1[j].getState())
             {  score++;
                分数.setText("得分: "+score);
                checkbox1[j].setState(false); 
             }
          checkbox1[j].setEnabled(false); 
            
        }
   }
}其中关联的English.txt文件如下He grabbed me     and pulled me onto the bus#a arm#an arm#the arm#by the arm#by the arm#
she cut cloth with     asissors#a couple of#a pair of#two#a#a pair of#
     Englishman like beer#Most#Most of the#Most of#The most#Most#

解决方案 »

  1.   

    是在if(event.getSource()==next)中加入吧,之前试过不行
      

  2.   

    awt里的checkbox设置初始状态是setState(boolean)   swing里的JCheckbox 才是setSelected()
      

  3.   

    setState
    public void setState(boolean state)将此复选框的状态设置为指定状态。布尔值 true 指示此复选框处于“开”状态,false 指示此复选框处于“关”状态。 
    注意,此方法应主要用于初始化复选框的状态。以编程方式设置复选框的状态,这不 会触发 ItemEvent。触发 ItemEvent 的惟一方式是用户交互。 
    参数:
    state - 以布尔值形式表示的复选框的状态
      

  4.   

    我把 checkbox1[j]=new Checkbox("",false,age);
              checkbox1[j].addItemListener(this); 
    加入到if(event.getSource()==next)里还是不行,到底怎样重新初始化,小妹刚学JAVA,很多不懂
      

  5.   

      private void clearState()
      {
      int len = checkbox1.length;
      for (int i = 0; i < len; i ++)
      {
      checkbox1[i].setCheckboxGroup(null);
      checkbox1[i].setState(false);
      }
      
      for (int i = 0; i < len; i ++)
      {
      checkbox1[i].setCheckboxGroup(age);
      }
      }在next按钮触发的时候加入
      

  6.   

    上面的原理是:先去掉Group, 然后清除选中,清除完了之后再恢复Group,PS, LZ这个代码的逻辑很乱, 每个工作用一个函数表示会好看很多.
      

  7.   

    在你的相应next的时间处理中加入以下代码:
    if(checkbox1[j].getState())
        {
         (checkbox1[j].getCheckboxGroup()).setSelectedCheckbox(null);
        }
    替换掉checkbox1[j].setState(false);