2016-04-14 67 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

向我們展示「問題」類的代碼 – 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