从Jbutton

时间:2017-06-16 12:51:37

标签: java swing

我有一个GUI,可以显示五个随机选择的问题。问题限制受到按下下一个问题按钮的次数的限制。当用户完成五个问题后,我希望他们按下启动新的圆形按钮,该按钮应该重新启动程序 我之前从未做过这样的事情,我看过的代码我不明白它是如何工作的。

这是我的代码:一切都在这里工作,我只需要添加重启按钮/代码

int nextQuestionClicked;//For number of times next question is clicked

//Method to generate random questions
private String setQuestions(){
   int match = (int) Math.floor(Math.random()*cities.size());
   String whatCity = cities.get(match);
   String displayCity = "Where is " + whatCity + " located?";
   return displayCity;
   }

//What happens when next question is pressed
private void displayQuestionActionPerformed(java.awt.event.ActionEvent evt) {                                                
   boolean trueOrFalse;

   submitButton.setEnabled(true);

   displayQuestion.setEnabled(false);

   outputTextQuestion.setText(null);
   outputTextQuestion.setText(setQuestions());
   outputAnswers.setText(null);        
   inputAnswer.setText(null);       
   outputDegree.setText(null);

   nextQuestionClicked++;

   int buttonLimit = 4;

   if (nextQuestionClicked  <= buttonLimit) 
   {

       int correctAnswer = -1;

        for (int x = 0; x < cities.size(); x++)
        {
            if (trueOrFalse = outputTextQuestion.getText().contains(cities.get(x)))
            {
                correctAnswer = x;
                break;
            }
        }
        randomAnswers = new ArrayList <Integer>();
        Collections.addAll(randomAnswers, correctAnswer);

        for (int x=0; x < 3; x++)
        {
           int r = correctAnswer;
           while (randomAnswers.contains(r))
            {
                r = ((int)(Math.random()*100))%cities.size();

            }
            Collections.addAll(randomAnswers, r);
        }

        Collections.shuffle(randomAnswers);
        outputAnswers.setText(null);

        for (int r=0; r<randomAnswers.size(); r++) 
        {
             int hint = randomAnswers.get(r);
             outputAnswers.append(provinces.get(hint) + "\n");
        }

        inputAnswer.requestFocus();
}

   else{
        displayQuestion.setEnabled(false);
        submitButton.setEnabled(false);
        newRound.setEnabled(true);
        outputTextQuestion.setText("Start New Round!!");
        outputDegree.setText(null);
        }
}  

private void newRoundActionPerformed(java.awt.event.ActionEvent evt) {                                         
    outputTextQuestion.setText(null);
    displayQuestion.setEnabled(true);
    submitButton.setEnabled(false);
//What code do I do here to restart the program?
}

请帮忙!

提前致谢!!

1 个答案:

答案 0 :(得分:1)

尝试这样的事情:

private void newRoundActionPerformed(java.awt.event.ActionEvent evt) {                                         
  nextQuestionClicked = 0;
  displayQuestionActionPerformed(evt);
}

如果你想展示其他代码而不是第一个问题,你可以添加一些其他代码而不是displayQuestionActionPerformed() ...