在我的测验中确定正确的答案

时间:2016-04-14 14:39:02

标签: java swing

所以我当前的测验会生成窗格中的窗格和按钮,有一个数组可以保存所有的答案和问题,如下所示

int total = 0;
int counter = 0;
JTabbedPane quiz = new JTabbedPane();
Problem[] probList = new Problem[6];
JLabel lblOutput;
JPanel finishQuiz = new JPanel();
JButton finish = new JButton("Finish Quiz");

public QuizEasy(){

    problems();
    CreateTabs();
    setTitle("Easy Questions");
    setSize(680,300);

    getContentPane().add(quiz);



    ButtonHandler phandler = new ButtonHandler();
    finish.addActionListener(phandler);
    setVisible(true);



}

public void CreateTabs() {
    JPanel question = null;
    JLabel lbQuestion = null;
    JButton ansA = null;
    JButton ansB = null;
    JButton ansC = null;
    for (int i=0;i<probList.length;i++) {
        question = new JPanel();
        lbQuestion = new JLabel(probList[i].getQuestion());
        question.add(lbQuestion);
        ansA = new JButton(probList[i].getAnsA());
        ansB = new JButton(probList[i].getAnsB());
        ansC = new JButton(probList[i].getAnsC());
        lblOutput = new JLabel("Please click on your answer");
        question.add(ansA);
        question.add(ansB);
        question.add(ansC);
        question.add(lblOutput);
        quiz.addTab("Question " + (i+1), question); 
    }
    quiz.addTab("Finish Quiz", finishQuiz);
    finishQuiz.add(finish);
}


public void problems(){
    probList[0] = new Problem(
              "What is the meaning of life?",
              "Only god knows",
              "42",
              "huh?",
              "B"
            );
    probList[1] = new Problem(
              "What level woodcutting do you need to cut oaks in runescape",
              "15",
              "20",
              "99",
              "C"
            );
    probList[2] = new Problem(
              "Who has recieved the highest amount of oscars?",
              "Walt Disney",
              "You",
              "Leonardo Di Caprio",
              "A"
            );
    probList[3] = new Problem(
              "What is the most spoken native tounge in the world?",
              "English",
              "Kek",
              "Mandarin",
              "C"
            );
    probList[4] = new Problem(
              "Deva was the Roman name for which British city?",
              "Bristol",
              "Chester",
              "London",
              "B"
            );
    probList[5] = new Problem(
              "Which herb is one of the main ingredients of Pesto Sauce?",
              "Basil",
              "Chester",
              "London",
              "A"
            );

}



class ButtonHandler implements ActionListener{
    public void actionPerformed(ActionEvent e){
        showSummary();
    }
}

public void showSummary(){
    JOptionPane.showMessageDialog(null,"You have completed the quiz, here are your results" + counter
            );
    System.exit(0);
}

public static void main(String[] args) {

    QuizEasy tab = new QuizEasy();

}

}

我不太确定如何使按钮对应正确的答案,任何建议?我试图搞乱计数器等等,但我最终没办法让它运转起来。任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:0)

您可以将JButton分配给数组中的每个位置。

JButton button1 = new JButton("Button 1");

然后你需要一个像这样的动作监听器

button1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e)
    {
        Object source = e.getSource();
        if (source instanceof JButton) {
            JButton btn = (JButton)source;
            // Go ahead and do what you like
        }
    }
});