重新验证jframe的问题

时间:2011-01-15 01:47:33

标签: java swing jframe

我有这个代码应该采取单选按钮输入做一点数学并显示一个弹出窗口。它做得很好。但是它应该重新验证并询问下一个问题。 当我得到第二个问题时,无论你点击哪个单选按钮,答案总是作为​​isSelected(true)值出现。所以第一次通过它工作时要清楚,但是当第二个问题出现时,它每次只需要默认的单选按钮。

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]);
}

}

3 个答案:

答案 0 :(得分:1)

而不是询问JRadioButton是否认为它已被选中:

 if(gui.a1.isSelected()){

你应该问ButtonGroup

 if(gui.bg.isSelected(gui.a1.getModel())){

答案 1 :(得分: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."));

答案 2 :(得分:0)

如果不能运行代码(文本文件丢失)很难分辨,但您可以尝试清除moveOn方法中按钮组的选择

bg.clearSelection();