2016-04-14 3 views
0

Так что мой текущий тест генерирует панели и кнопки, которые идут в стеклах, есть массив, который содержит все ответы и вопросы, как вы можете увидеть нижеОпределение правильного ответа в моей викторине

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(); 

} 

}

Я не совсем уверен, как заставить кнопки соответствовать правильному ответу, любым советам? Я попытался возиться со счетчиками и так, но мне не удалось заставить его работать в конце. любая помощь приветствуется!

+0

Пожалуйста, покажите нам, что вы пробовали. Прямо сейчас вы просите нас пройти тест для вас. Предоставьте нам минимальный, полный и проверяемый пример [MVCE] (http://stackoverflow.com/help/mcve) – wiredniko

+0

Покажите нам код для класса 'Problem' – JimHawkins

ответ

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 
     } 
    } 
}); 
+0

Как мне это сделать? – breadkun

Смежные вопросы