简体   繁体   English

我正在为测验程序使用JTabbedPane,不确定如何获取按钮以显示答案

[英]I'm using JTabbedPane for my quiz program, not sure how to get my buttons to display the answers

As it says, I'm not sure how to loop through the array so it would display the question in each pane, I got 8 panes and each of them should contain a question, I've managed to get it working for one (question 1 displays first entry in the array) but not the others. 就像我说的那样,我不确定如何遍历数组,因此它会在每个窗格中显示问题,我有8个窗格,每个窗格都应包含一个问题,我设法使其适用于一个问题(问题1显示数组中的第一个条目),而不显示其他条目。

Any suggestions? 有什么建议么?

int total = 0;
int counter = 0;
JPanel question1 = new JPanel();
JPanel question2 = new JPanel();
JPanel question3 = new JPanel();
JPanel question4 = new JPanel();
JPanel question5 = new JPanel();
JPanel question6 = new JPanel();
JPanel question7 = new JPanel();
JPanel question8 = new JPanel();
JTabbedPane quiz = new JTabbedPane();
JLabel lblQuestion1 = new JLabel();
JLabel lblQuestion2 = new JLabel();
JLabel lblQuestion3 = new JLabel();
JLabel lblQuestion4 = new JLabel();
JLabel lblQuestion5 = new JLabel();
JLabel lblQuestion6 = new JLabel();
JLabel lblQuestion7 = new JLabel();
JLabel lblQuestion8 = new JLabel();
JButton question1A = new JButton("Answer A");
JButton question1B = new JButton("Answer B");
JButton question1C = new JButton("Answer C");
JButton question2A = new JButton("Answer A");
JButton question2B = new JButton("Answer B");
JButton question2C = new JButton("Answer C");
JButton question3A = new JButton("Answer A");
JButton question3B = new JButton("Answer B");
JButton question3C = new JButton("Answer C");
Problem[] probList = new Problem[5];

public QuizEasy(){

    createLabel();
    createTabs();
    questionsEasy();
    problems();
    nextQuestion();
    updateScreen();
    setTitle("Easy Questions");
    setSize(680,300);

    getContentPane().add(quiz);

    JButton finish = new JButton("Finish Quiz");
    question8.add(finish);


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



}

public void nextQuestion(){
    //go forward one question if possible
    counter++;
    if (counter >= probList.length){
        counter = probList.length - 1;
    } // end if
    updateScreen();
}

public void updateScreen(){
    Problem p = probList[counter];
    lblQuestion1.setText(p.getQuestion());
    question1A.setText(p.getAnsA());
    question1B.setText(p.getAnsB());
    question1C.setText(p.getAnsC());
    lblQuestion2.setText(p.getQuestion());
    question2A.setText(p.getAnsA());
    question2B.setText(p.getAnsB());
    question2C.setText(p.getAnsC());

}

public void questionsEasy(){


    question1.add(question1A);
    question1.add(question1B);
    question1.add(question1C);
    question2.add(question2A);
    question2.add(question2B);
    question2.add(question2C);
    question3.add(question3A);
    question3.add(question3B);
    question3.add(question3C);


}

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",
              "Ailien",
              "Mandarin",
              "C"
            );
    probList[4] = new Problem(
              "What is the airspeed velocity of an unladen swallow?",
              "42 beats per second",
              "10 miles per hour",
              "African or European?",
              "C"
            );

}

public void createLabel(){
    /*JLabel easyQuestion1 = new JLabel();
    easyQuestion1.setText("What level do you need in the game Runescape to cut oak?");
    question1.add(easyQuestion1);*/

    lblQuestion1 = new JLabel("Question");
    lblQuestion2 = new JLabel("Question");
    lblQuestion3 = new JLabel("Question");
    lblQuestion4 = new JLabel("Question");
    lblQuestion5 = new JLabel("Question");
    lblQuestion6 = new JLabel("Question");
    lblQuestion7 = new JLabel("Question");
    lblQuestion8 = new JLabel("Question");

    question1.add(lblQuestion1);
    question2.add(lblQuestion2);
    question3.add(lblQuestion3);
    question4.add(lblQuestion4);
    question5.add(lblQuestion5);
    question6.add(lblQuestion6);
    question7.add(lblQuestion7);
    question8.add(lblQuestion8);




}

public void createTabs(){
    quiz.addTab("Question 1", question1);
    quiz.addTab("Question 2", question2);
    quiz.addTab("Question 3", question3);
    quiz.addTab("Question 4", question4);
    quiz.addTab("Question 5", question5);
    quiz.addTab("Question 6", question6);
    quiz.addTab("Question 7", question7);
    quiz.addTab("Question 8", question8);
}

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"
            );
    System.exit(0);
}

public static void main(String[] args) {

    QuizEasy tab = new QuizEasy();

}

} }

To make things easier you could create a method called CreateTabs which would build every tab based on your list of problems(probList). 为了使事情变得容易,您可以创建一个称为CreateTabs的方法,该方法将根据问题列表(probList)构建每个选项卡。 See code below: 参见下面的代码:

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());
        question.add(ansA);
        question.add(ansB);
        question.add(ansC);
        quiz.addTab("Question " + (i+1), question); 
    }
}

In the QuizEasy constructor you would have something like: 在QuizEasy构造函数中,您将具有以下内容:

public QuizEasy() {
    problems();
    CreateTabs();
    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我创建了一个测验,但是我不确定如何使用Parse捕获捕获的按钮以检查答案 - I've created a quiz however I'm not sure how to implement the capturing of the buttons pressed to check against the answer using Parse 我的程序没有计算正确的输出,我不确定为什么 - My program is not computing the right output, and I'm not sure why 如何在程序底部找到这两个按钮 - How do I get these two buttons on the bottom of my program 如何确保我的 tensorFlow Java 程序在 Windows 上使用我的 GPU? - How can I make sure that my tensorFlow Java program is using my GPU on Windows? 我不确定如何在我的Java代码中正确地舍入它 - I'm not sure how to round this properly in my Java code 我不确定如何构造我的算术方法 - I'm not sure how to structure my arithmetic methods 我的程序产生找不到符号错误,我不确定我哪里出错了 - My program produces cannot find symbol error and I'm not sure where I am going wrong 我不确定我的程序正在读取我要求它输入的文件 - I'm not sure my program is reading the file input I've asked it to 如何将滚动标签的布局设置为JTabbedPane? - How can I set scroll tab layout to my JTabbedPane? 我无法计算总和。 我什至不确定我的逻辑是否正确 - I cannot get the sum to calculate. I'm not even sure my logic is correct
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM