2011-01-15 8 views
3

ラジオボタンの入力が必要なこのコードを少し計算してポップアップを表示する必要があります。それはうまくいく。それから、それを検証して次の質問をすることになっています。 2番目の質問になると、クリックするラジオボタンに関係なく、常にisSelected(true)の値が返されます。最初の時間はクリアされていますが、2回目の質問が表示されたら、デフォルトのラジオボタンを押すだけです。jframeの再検証に関する問題

public class EventHandler implements ActionListener { 

private Main gui; 

public EventHandler(Main gui){ 
    this.gui = gui; 
} 
public void actionPerformed(ActionEvent e){ 
    String answer = ""; 
    double val = 1; 
    //get current answer set 
    String [] anArr = gui.getAnswers(gui.currentStage, gui.currentQuestion); 


    if(e.getSource() == gui.exit){ 
     System.exit(0); 
    } 
    if(e.getSource() == gui.submit){ 

     if(gui.a1.isSelected()){ 
      answer = anArr[0]; 
      val = gui.getScore(1); 
     } 
     if(gui.a2.isSelected()){ 
      answer = anArr[1]; 
      val = gui.getScore(2); 
     } 
     if(gui.a3.isSelected()){ 
      answer = anArr[2]; 
      val = gui.getScore(3); 
     } 
     if(gui.a4.isSelected()){ 
      answer = anArr[3];; 
      val = gui.getScore(4); 
     } 
     JOptionPane.showMessageDialog(null, popupMessage(answer, val), "Your Answer", 1); 

     //compute answer here 

     //figure out what next question is to send 
     gui.moveOn(); 
     gui.setQA(gui.currentStage, gui.currentQuestion); 
     //resets gui 
     gui.goWest(); 

     gui.q.revalidate(); 


    } 
} 
public String popupMessage(String ans, double val){ 
    //displays popup after an answer has been choosen 

    gui.computeScore(val); 
    String text = " You Answered " + ans + " Your score is now " + gui.yourScore ; 

    return text; 
} 
} 




public class Main extends JFrame { 
public JLabel question; 
public JButton exit; 
public JButton submit; 
public JRadioButton a1; 
public JRadioButton a2; 
public JRadioButton a3; 
public JRadioButton a4; 
public ButtonGroup bg; 
public double yourScore = 1; 
public int currentQuestion = 1; 
public String currentStage = "startup"; 
JPanel q; 


public Main(){ 
    setTitle("Ehtics Builder"); 
    setLocation(400,400); 
    setLayout(new BorderLayout(5,5)); 
    setQA("startup", 1); 

    goNorth(); 
    goEast(); 
    goWest(); 
    goSouth(); 
    goCenter(); 

    pack(); 
    setVisible(true); 
    setDefaultCloseOperation(EXIT_ON_CLOSE); 
} 
public void goNorth(){ 

} 
public void goWest(){ 

    q = new JPanel(); 
    q.setLayout(new GridLayout(0,1)); 
    q.add(question); 
    bg.add(a1); 
    bg.add(a2); 
    bg.add(a3); 
    bg.add(a4); 
    a1.setSelected(true); 
    q.add(a1); 
    q.add(a2); 
    q.add(a3); 
    q.add(a4); 

    add(q, BorderLayout.WEST); 
    System.out.println(); 
} 
public void goEast(){ 

} 
public void goSouth(){ 
    JPanel p = new JPanel(); 
    p.setLayout(new FlowLayout(FlowLayout.CENTER)); 
    exit = new JButton("Exit"); 
    submit = new JButton("Submit"); 
    p.add(exit); 
    p.add(submit); 
    add(p, BorderLayout.SOUTH); 

    EventHandler myEventHandler = new EventHandler(this); 
    exit.addActionListener(myEventHandler); 
    submit.addActionListener(myEventHandler); 
} 
public void goCenter(){ 

} 


public static void main(String[] args) { 
    Main open = new Main(); 
} 
public String getQuestion(String type, int num){ 
    //reads the questions from a file 
    String question = ""; 
    String filename = ""; 
    String [] ques; 
    num = num - 1; 
    if(type.equals("startup")){ 
     filename = "startup.txt"; 
    }else if(type.equals("small")){ 
     filename = "small.txt"; 
    }else if(type.equals("mid")){ 
     filename = "mid.txt"; 
    }else if(type.equals("large")){ 
     filename = "large.txt"; 
    }else{ 
     question = "error"; 
     return question; 
    } 

    ques = readFile(filename); 

    for(int i = 0;i < ques.length;i++){ 
     if(i == num){ 
      question = ques[i]; 
     } 
    } 

    return question; 
} 
public String [] getAnswers(String type, int num){ 
    //reads the answers from a file 
    String filename = ""; 
    String temp = ""; 
    String [] group; 
    String [] ans; 
    num = num - 1; 
    if(type.equals("startup")){ 
     filename = "startupA.txt"; 
    }else if(type.equals("small")){ 
     filename = "smallA.txt"; 
    }else if(type.equals("mid")){ 
     filename = "midA.txt"; 
    }else if(type.equals("large")){ 
     filename = "largeA.txt"; 
    }else{ 
     System.out.println("Error"); 
    } 

    group = readFile(filename); 

    for(int i = 0;i < group.length;i++){ 
     if(i == num){ 
      temp = group[i]; 
     } 
    } 

    ans = temp.split("-"); 

    return ans; 
} 
public String [] getValues(String type, int num){ 
    //reads the answers from a file 
    String filename = ""; 
    String temp = ""; 
    String [] group; 
    String [] vals; 
    num = num - 1; 
    if(type.equals("startup")){ 
     filename = "startupV.txt"; 
    }else if(type.equals("small")){ 
     filename = "smallV.txt"; 
    }else if(type.equals("mid")){ 
     filename = "midV.txt"; 
    }else if(type.equals("large")){ 
     filename = "largeV.txt"; 
    }else{ 
     System.out.println("Error"); 
    } 

    group = readFile(filename); 

    for(int i = 0;i < group.length;i++){ 
     if(i == num){ 
      temp = group[i]; 
     } 
    } 

    vals = temp.split("-"); 

    return vals; 
} 
public String [] readFile(String filename){ 
    //reads the contentes of a file, for getQuestions and getAnswers 
    String text = ""; 
    int i = -1; 
    FileReader in = null; 
    File f = new File(filename); 
    try{ 
     in = new FileReader(f); 
    }catch(FileNotFoundException e){ 
     System.out.println("file does not exist"); 
    } 
    try{ 
     while((i = in.read()) != -1) 
      text += ((char)i); 
    }catch(IOException e){ 
     System.out.println("Error reading file"); 
    } 
    try{ 
     in.close(); 
    }catch(IOException e){ 
     System.out.println("Error reading file"); 
    } 

    String [] questions = text.split(":"); 
    return questions; 
} 
public void computeScore(double val){ 
    //calculates you score times the value of your answer 
    yourScore = val * yourScore; 
} 
public double getScore(int aNum){ 
    //gets the score of an answer, stage and q number is already set in the class 
    aNum = aNum - 1; 
    double val = 0; 
    double [] valArr = new double[4]; 
    for(int i = 0;i < getValues(currentStage, currentQuestion).length;i++){ 
     val = Double.parseDouble(getValues(currentStage, currentQuestion)[i]); 
     valArr[i] = val; 
    } 
    if(aNum == 0){ 
     val = valArr[0]; 
    } 
    if(aNum == 1){ 
     val = valArr[1]; 
    } 
    if(aNum == 2){ 
     val = valArr[2]; 
    } 
    if(aNum == 3){ 
     val = valArr[3]; 
    } 
    // use current stage and questiion and trhe aNum to get the value for that answer 
    return val; 
} 
public void nextQuestion(int num){ 
    //sets next question to use 
    currentQuestion = num; 
} 
public void nextStage(String sta){ 
    // sets next stage to use 
    currentStage = sta; 
} 
public void moveOn(){ 
    // uses the score and current question and stage to determine wher to go next and what stage to use next 

    nextQuestion(2); 
    nextStage("startup"); 
} 
public void setQA(String level, int num){ 
    String [] arr = getAnswers(level, num); 
    question = new JLabel(getQuestion(level, num)); 
    bg = new ButtonGroup(); 
    a1 = new JRadioButton(arr[0]); 
    a2 = new JRadioButton(arr[1]); 
    a3 = new JRadioButton(arr[2]); 
    a4 = new JRadioButton(arr[3]); 
} 

} 

答えて

0

(テキストファイルが失われている)のコードを実行できずに伝えるのは難しいですが、あなたのmoveOn方法でボタングループの選択をクリアする

bg.clearSelection(); 
1

の代わりに尋ねてみてください

if(gui.a1.isSelected()){ 

あなたはButtonGroup

に依頼する必要があります:それは、それが選択されていると考えているかどうか
if(gui.bg.isSelected(gui.a1.getModel())){ 
1

どのボタンが選択されているかをすべて確認すると、多くの類似したコードが表示されます。代わりに、JRadioButtonに与えられるActionクラスを作成してみてください。アクションを中央に位置する回答変数に設定します。質問が提出されたら、答えの値を読み取るだけです。

Actionクラスは次のようになります。

public class AnswerAction extends AbstractAction{ 
    private String value; 

    public AnswerAction(String value){ 
     super(value); 
     this.value = value; 
    } 

    @Override 
    public void actionPerformed(ActionEvent arg0) { 
     answer = value; 
    } 
} 

は、代わりに、すべてのボタンを再構築するのあなただけだろう:

button.setAction(new AnswerAction("All of the above."));