图中B选项是第一题选择后留下的,请问怎么设置才能不让该现象进入到后面的题JavaCheckBox

解决方案 »

  1.   

    点下一题的时候,手动把radio的值清掉。。
      

  2.   

    import java.io.*;
    import java.util.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class lesson5_3 
    {
      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 checkbox[];  
    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);   
    checkbox = new Checkbox[4];   
    for (int i = 0; i <=3; i++) 
    {
      checkbox[i] = new Checkbox("",true,age);  
      checkbox[i].addItemListener(this);   

    try
    {
    file = new FileReader("English.txt");  
    in = new BufferedReader(file);    
    }
    catch(IOException e)
    {
    System.out.println("Error:"+e.getMessage());
    }
    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(checkbox[i]);
    }
    p3.add(new Label("您 的 得 分 :"));p3.add(分数);  
    p4.add(start);p5.add(next);     
    box.add(p1);box.add(p2);box.add(p3);box.add(p4);
    box.add(p5);   
    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++)
      {
    checkbox[j-1].setLabel(str[j]);    

    }
    }
    else if (s.startsWith("endend")) 
    {
      题目.setText("学习完毕");
      for (int j = 0; j < 4; j++) 
      {
        checkbox[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) 
    {
    System.out.println("Error: "+e.getMessage()); 
    }
    reading();   
    }
    if (event.getSource()==next) 
    {
      reading();   
      for (int j = 0; j < 4; j++) 
      {
        checkbox[j].setEnabled(true);   
        checkbox[j].setState(false);
      }
    }
    }
    public void itemStateChanged(ItemEvent e)   
    {
    for (int j = 0; j < 4; j++) 
    {
      if (checkbox[j].getLabel().equals(str[5])&&checkbox[j].getState())
     
      {
        score++;
        分数.setText("得 分 : "+score);
      }
    if(checkbox[j].getLabel()!=(str[5])&&checkbox[j].getState())
    {
    score = score + 0; if(score<0) { score = 0; }
    分数.setText("得 分 : "+score);

    }
      //checkbox[j].setEnabled(false);   
    }
    }
    }附上代码