我该如何循环问题?

时间:2015-03-16 08:10:55

标签: java applet

我正在尝试创建一个多选择琐事小程序。在用户回答问题后,将显示他们的分数。但是我希望它回到第一帧(或者你称之为的任何东西),这样游戏根本不会结束,但我不知道该怎么做。谁可以帮助我?

此外,我希望下次用户播放时,问题会以随机顺序排列。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class experiment extends JApplet implements ActionListener{//1st brace

    int totalScore,score1,score2,score3,score4,score5,score6;

    private JLabel message= new JLabel ("You have "+ totalScore + " correct answers");

    private JLabel welcome= new JLabel("WELCOME TO THE TRIVIA GAME!");
    private JButton play =new JButton("                  Play                      ");

    Font font1 = new Font("Times New Roman", Font.BOLD, 30);
    Font font2 = new Font("Times New Roman", Font.BOLD, 20);

    private JLabel correct= new JLabel ("You are correct.This question is worth 1 point.");
    private JLabel correct2= new JLabel ("You are correct.This question is worth 2 points.");
    private JLabel correct3= new JLabel ("You are correct.This question is worth 3 points.");
    private JLabel correct4= new JLabel ("You are correct.This question is worth 5 points.");
    private JLabel correct5= new JLabel ("You are correct.This question is worth 8 points.");
    private JLabel correct6= new JLabel ("You are correct.This question is worth 13 points.");

    private JLabel wrong= new JLabel ("You got the wrong answer. The correct answer is Venus.  ");
    private JLabel wrong2= new JLabel ("You got the wrong answer. The correct answer is YES.  ");
    private JLabel wrong3= new JLabel("You got the wrong answer.The correct answer is Californium.");
    private JLabel wrong4= new JLabel("You got the wrong answer.The correct answer is Shailene Woodley.");
    private JLabel wrong5= new JLabel("You got the wrong answer.The correct answer is EL James.");
    private JLabel wrong6= new JLabel ("You got the wrong answer. The correct answer is Paulina Vega.");

    private JButton next= new JButton("      Next     ");
    private JButton next2= new JButton("     Next     ");
    private JButton next3= new JButton("     Next     ");
    private JButton next4= new JButton("     Next     ");
    private JButton next5= new JButton("     Next     ");
    private JButton next6= new JButton("     Next     ");


    private JLabel q1 = new JLabel ("Which of these planets rotate clockwise?");
    private JButton q1choiceA= new JButton ("              Mars             ");
    private JButton q1choiceB= new JButton ("              Venus            ");
    private JButton q1choiceC= new JButton ("              Earth            ");
    private JButton q1choiceD= new JButton ("              Pluto            ");

    private JLabel q2 = new JLabel ("In seahorses, it's the male who gives birth to the young.");
    private JButton q2choiceA= new JButton ("             True              ");
    private JButton q2choiceB= new JButton ("             False             ");
    private JButton q2choiceC= new JButton ("             Maybe             ");
    private JButton q2choiceD= new JButton ("         I'm not sure.        ");

    private JLabel q3 = new JLabel ("What is the substance which costs $68 million per one gram? ");
    private JButton q3choiceA= new JButton ("             Gold               ");
    private JButton q3choiceB= new JButton ("            Silver               ");
    private JButton q3choiceC= new JButton ("             Zinc                  ");
    private JButton q3choiceD= new JButton ("         Californium          ");



    private JLabel q4 = new JLabel ("In the movie series Divergent, who played the character of Tris?");
    private JButton q4choiceA= new JButton ("       Shailene Woodley         ");
    private JButton q4choiceB= new JButton ("       Jennifer Lawrence        ");
    private JButton q4choiceC= new JButton ("            Lily Collins              ");
    private JButton q4choiceD= new JButton ("            Dakota Johnson       ");

    private JLabel q5= new JLabel ("Who wrote the book Fifty Shades of Grey?"); 
    private JButton q5choiceA= new JButton("        Nicholas Sparks           ");
    private JButton q5choiceB= new JButton("            Cecelia Ahern          ");
    private JButton q5choiceC= new JButton("               EL James            ");
    private JButton q5choiceD= new JButton("          Suzanne Collins        ");

    private JLabel q6= new JLabel ("Who is the reigning Miss Universe 2015?");
    private JButton q6choiceA= new JButton("        Mary Jean Lastimosa       ");
    private JButton q6choiceB= new JButton("              Paulina Vega              ");
    private JButton q6choiceC= new JButton("               Kaci Fennell             ");
    private JButton q6choiceD= new JButton("       Maria Gabriela Isler       ");


    public void init(){//2nd brace

        setSize(560,500);
        setLayout(new FlowLayout(FlowLayout.CENTER,250,70));
         Container j= this.getContentPane();
         j.setBackground(Color.PINK);

        welcome.setFont(font1);
        welcome.setForeground(Color.BLUE);
        play.setFont(font2);
        play.setBackground(Color.GRAY);

        add(welcome);
        add(play);

        play.addActionListener(this);
    }//2nd brace

    public void actionPerformed(ActionEvent a){//3rd brace

        //question1
         if(a.getSource()==play){//4th


             welcome.setVisible(false);
             play.setVisible(false);

            setLayout(new FlowLayout(FlowLayout.CENTER,100,30));

            q1.setFont(font2);
            q1choiceA.setFont(font2);
            q1choiceB.setFont(font2);
            q1choiceC.setFont(font2);
            q1choiceD.setFont(font2);

            q1choiceA.setBackground(Color.CYAN);
            q1choiceB.setBackground(Color.CYAN);
            q1choiceC.setBackground(Color.CYAN);
            q1choiceD.setBackground(Color.CYAN);

            add(q1);
            add(q1choiceA);
            add(q1choiceB);
            add(q1choiceC);
            add(q1choiceD);


            q1choiceA.addActionListener(this);
            q1choiceB.addActionListener(this);
            q1choiceC.addActionListener(this);
            q1choiceD.addActionListener(this);

            validate();
     }//4th

        //question1 choices
         if (a.getSource()==q1choiceB){//5
             q1choiceA.setEnabled(false);
             q1choiceC.setEnabled(false);
             q1choiceD.setEnabled(false);

             correct.setFont(font2);
             next.setFont(font2);
             next.setBackground(Color.GRAY);

            add(correct);
            add(next);


            next.addActionListener(this);

            score1=1;

             validate();
        }//6


     //question 1 choiceA
        else if (a.getSource()==q1choiceA){//7

            q1choiceB.setEnabled(false);
            q1choiceC.setEnabled(false);
            q1choiceD.setEnabled(false);

            wrong.setFont(font2);
            next.setFont(font2);
            next.setBackground(Color.GRAY);

             add(wrong);
             add(next);

             next.addActionListener(this);

             score1=0;

             validate();
        }//7
            //question1 choiceC
        else if (a.getSource()==q1choiceC){//8
             q1choiceA.setEnabled(false);
             q1choiceB.setEnabled(false);
             q1choiceD.setEnabled(false);

            wrong.setFont(font2);
            next.setFont(font2);
            next.setBackground(Color.GRAY);

             add(wrong);
             add(next);

             next.addActionListener(this);

             score1=0;

             validate();
         }//8
             //question1 choiceD
         else if (a.getSource()==q1choiceD){//9
            q1choiceA.setEnabled(false);
            q1choiceB.setEnabled(false);
            q1choiceC.setEnabled(false);

            wrong.setFont(font2);
            next.setFont(font2);
            next.setBackground(Color.GRAY);

            add(wrong);
            add(next);

            next.addActionListener(this);

            score1=0;

            validate();
        }//9

         if (a.getSource()==next){//10

            q1.setVisible(false);
            q1choiceA.setVisible(false);
            q1choiceB.setVisible(false);
            q1choiceC.setVisible(false);
            q1choiceD.setVisible(false);
            correct.setVisible(false);
            wrong.setVisible(false);
            next.setVisible(false);

            q2.setFont(font2);
            q2choiceA.setFont(font2);
            q2choiceB.setFont(font2);
            q2choiceC.setFont(font2);
            q2choiceD.setFont(font2);

            q2choiceA.setBackground(Color.CYAN);
            q2choiceB.setBackground(Color.CYAN);
            q2choiceC.setBackground(Color.CYAN);
            q2choiceD.setBackground(Color.CYAN);

            add(q2);
            add(q2choiceA);
            add(q2choiceB);
            add(q2choiceC);
            add(q2choiceD);

            q2choiceA.addActionListener(this);
            q2choiceB.addActionListener(this);
            q2choiceC.addActionListener(this);
            q2choiceD.addActionListener(this);

                validate();
         }//10

         if (a.getSource()==q2choiceA){//11


            q2choiceB.setEnabled(false);
            q2choiceC.setEnabled(false);
            q2choiceD.setEnabled(false);

            correct2.setFont(font2);
            next2.setFont(font2);

            next2.setBackground(Color.gray);

            add(correct2);
            add(next2);

            next2.addActionListener(this);

            score2=2;

            validate();
        }//11
        else if (a.getSource()==q2choiceB){//12

            q2choiceA.setEnabled(false);
            q2choiceC.setEnabled(false);
            q2choiceD.setEnabled(false);

            wrong2.setFont(font2);
            next2.setFont(font2);
            next2.setBackground(Color.gray);

            add(wrong2);
            add(next2);

            next2.addActionListener(this);

            score2=0;

            validate();
        }//12
         else if (a.getSource()==q2choiceC){//13

            q2choiceA.setEnabled(false);
            q2choiceB.setEnabled(false);
            q2choiceD.setEnabled(false);

             wrong2.setFont(font2);
             next2.setFont(font2);
            next2.setBackground(Color.gray);

            add(wrong2);
            add(next2);

            next2.addActionListener(this);

            score2=0;
             validate();
         }//13
        else if (a.getSource()==q2choiceD){//14

            q2choiceA.setEnabled(false);
            q2choiceB.setEnabled(false);
            q2choiceC.setEnabled(false);

            wrong2.setFont(font2);
            next2.setFont(font2);
            next2.setBackground(Color.gray);

             add(wrong2);
             add(next2);

              next2.addActionListener(this);

             score2=0;

             validate();
        }//14

            //Q3

         if (a.getSource()==next2){//15

            q2.setVisible(false);
            q2choiceA.setVisible(false);
            q2choiceB.setVisible(false);
            q2choiceC.setVisible(false);
            q2choiceD.setVisible(false);
            correct2.setVisible(false);
            wrong2.setVisible(false);
            next2.setVisible(false);

            q3.setFont(font2);
            q3choiceA.setFont(font2);
            q3choiceB.setFont(font2);
            q3choiceC.setFont(font2);
            q3choiceD.setFont(font2);

            q3.setBackground(Color.CYAN);
            q3choiceA.setBackground(Color.CYAN);
            q3choiceB.setBackground(Color.CYAN);
            q3choiceC.setBackground(Color.CYAN);
            q3choiceD.setBackground(Color.CYAN);

            add(q3);
            add(q3choiceA);
            add(q3choiceB);
            add(q3choiceC);
            add(q3choiceD);

            q3choiceA.addActionListener(this);
            q3choiceB.addActionListener(this);
            q3choiceC.addActionListener(this);
            q3choiceD.addActionListener(this);

            validate();

         }//15
        if (a.getSource()==q3choiceD){//15
            q3choiceA.setEnabled(false);
            q3choiceB.setEnabled(false);
            q3choiceC.setEnabled(false);

            correct3.setFont(font2);
            next3.setFont(font2);
            next3.setBackground(Color.GRAY);

            add(correct3);
            add(next3);

            score3=3;

            next3.addActionListener(this);

            validate();
        }//17
         else  if (a.getSource()==q3choiceA){//18

            q3choiceB.setEnabled(false);
            q3choiceC.setEnabled(false);
            q3choiceD.setEnabled(false);

            wrong3.setFont(font2);
            next3.setFont(font2);
            next3.setBackground(Color.gray);

            add(wrong3);
            add(next3);

            next3.addActionListener(this);

            score3=0;

            validate();
        }//18

         else  if (a.getSource()==q3choiceB){//19
            q3choiceA.setEnabled(false);
            q3choiceC.setEnabled(false);
            q3choiceD.setEnabled(false);

            wrong3.setFont(font2);
             next3.setFont(font2);
             next3.setBackground(Color.gray);

             add(wrong3);
             add(next3);

             next3.addActionListener(this);

             score3=0;


             validate();
         } //19

        else  if (a.getSource()==q3choiceC){//20
            q3choiceA.setEnabled(false);
            q3choiceB.setEnabled(false);
            q3choiceD.setEnabled(false);

            wrong3.setFont(font2);
            next3.setFont(font2);
            next3.setBackground(Color.gray);


             add(wrong3);
             add(next3);

             next3.addActionListener(this);

             score3=0;

             validate();
        } //20


        if (a.getSource()==next3){//21

            q3.setVisible(false);
            q3choiceA.setVisible(false);
            q3choiceB.setVisible(false);
            q3choiceC.setVisible(false);
            q3choiceD.setVisible(false);
            correct3.setVisible(false);
            wrong3.setVisible(false);
            next3.setVisible(false);

            q4.setFont(font2);
            q4choiceA.setFont(font2);
            q4choiceB.setFont(font2);
            q4choiceC.setFont(font2);
            q4choiceD.setFont(font2);

            q4.setBackground(Color.CYAN);
            q4choiceA.setBackground(Color.CYAN);
            q4choiceB.setBackground(Color.CYAN);
            q4choiceC.setBackground(Color.CYAN);
            q4choiceD.setBackground(Color.CYAN);


            add(q4);
            add(q4choiceA);
            add(q4choiceB);
            add(q4choiceC);
            add(q4choiceD);

            q4choiceA.addActionListener(this);
            q4choiceB.addActionListener(this);
            q4choiceC.addActionListener(this);
            q4choiceD.addActionListener(this);

            validate();
        }//22

        // Question4 choices
        if (a.getSource()==q4choiceA){//23
            q4choiceB.setEnabled(false);
            q4choiceC.setEnabled(false);
            q4choiceD.setEnabled(false);

            correct4.setFont(font2);
            next4.setFont(font2);
            next4.setBackground(Color.gray);

            add(correct4);
            add(next4);

            next4.addActionListener(this);

            score4 =5;

            validate();
        }//23

        else if (a.getSource()==q4choiceB){//23
            q4choiceA.setEnabled(false);
            q4choiceC.setEnabled(false);
            q4choiceD.setEnabled(false);

            wrong4.setFont(font2);
            next4.setFont(font2);
            next4.setBackground(Color.gray);


            add(wrong4);
            add(next4);

            next4.addActionListener(this);

            score4 =0;

            validate();
        }

        else if (a.getSource()==q4choiceC){//24
            q4choiceA.setEnabled(false);
            q4choiceB.setEnabled(false);
            q4choiceD.setEnabled(false);

            wrong4.setFont(font2);
            next4.setFont(font2);
            next4.setBackground(Color.gray);

            add(wrong4);
            add(next4);

            next4.addActionListener(this);

            score4 =0;

            validate();
        }//24
         else  if (a.getSource()==q4choiceD){//25
            q4choiceA.setEnabled(false);
            q4choiceB.setEnabled(false);
            q4choiceC.setEnabled(false);

            wrong4.setFont(font2);
            next4.setFont(font2);
            next4.setBackground(Color.gray);

            add(wrong4);
            add(next4);

            next4.addActionListener(this);

            score4 = 0;

            validate();
         }//25


        //entertainment q2

        if (a.getSource()==next4){//26
            q4.setVisible(false);
            q4choiceA.setVisible(false);
            q4choiceB.setVisible(false);
            q4choiceC.setVisible(false);
            q4choiceD.setVisible(false);
            next4.setVisible(false);
            correct4.setVisible(false);
            wrong4.setVisible(false);

            q5.setFont(font2);
            q5choiceA.setFont(font2);
            q5choiceB.setFont(font2);
            q5choiceC.setFont(font2);
            q5choiceD.setFont(font2);

            q5.setBackground(Color.CYAN);
            q5choiceA.setBackground(Color.CYAN);
            q5choiceB.setBackground(Color.CYAN);
            q5choiceC.setBackground(Color.CYAN);
            q5choiceD.setBackground(Color.CYAN);

            add(q5);
            add(q5choiceA);
            add(q5choiceB);
            add(q5choiceC);
            add(q5choiceD);

            q5choiceA.addActionListener(this);
            q5choiceB.addActionListener(this);
            q5choiceC.addActionListener(this);
            q5choiceD.addActionListener(this);

            validate();

        }

        if (a.getSource()==q5choiceC){//23
             q5choiceA.setEnabled(false);
             q5choiceB.setEnabled(false);
             q5choiceD.setEnabled(false);

             correct5.setFont(font2);
             next5.setFont(font2);
             next5.setBackground(Color.gray);

             correct5.setVisible(true);

             add(correct5);
             add(next5);

             next5.addActionListener(this);

             score5= 8;

            validate();
         }
        else if (a.getSource()==q5choiceA){//23
            q5choiceB.setEnabled(false);
            q5choiceC.setEnabled(false);
            q5choiceD.setEnabled(false);

             wrong5.setFont(font2);
             next5.setFont(font2);
             next5.setBackground(Color.gray);

             add(wrong5);
             add(next5);

             next5.addActionListener(this);

             score5 = 0;

            validate();
         }
        else if (a.getSource()==q5choiceB){
            q5choiceA.setEnabled(false);
            q5choiceC.setEnabled(false);
            q5choiceD.setEnabled(false);

            wrong5.setFont(font2);
            next5.setFont(font2);
             next5.setBackground(Color.gray);

            add(wrong5);
            add(next5);

            next5.addActionListener(this);

            score5 = 0;

            validate();
         }
        else if (a.getSource()==q5choiceD){
             q5choiceA.setEnabled(false);
             q5choiceB.setEnabled(false);
             q5choiceC.setEnabled(false);

             wrong5.setFont(font2);
             next5.setFont(font2);
             next5.setBackground(Color.gray);

             add(wrong5);
             add(next5);

             next5.addActionListener(this); 

             score5 = 0;

            validate();
         }


        //entertainment 

        if (a.getSource()==next5){//26

            q5.setVisible(false);
            q5choiceA.setVisible(false);
            q5choiceB.setVisible(false);
            q5choiceC.setVisible(false);
            q5choiceD.setVisible(false);
            next5.setVisible(false);
            correct5.setVisible(false);
            wrong5.setVisible(false);

            q6.setFont(font2);
            q6choiceA.setFont(font2);
            q6choiceB.setFont(font2);
            q6choiceC.setFont(font2);
            q6choiceD.setFont(font2);

            q6.setBackground(Color.CYAN);
            q6choiceA.setBackground(Color.CYAN);
            q6choiceB.setBackground(Color.CYAN);
            q6choiceC.setBackground(Color.CYAN);
            q6choiceD.setBackground(Color.CYAN);


            add(q6);
            add(q6choiceA);
            add(q6choiceB);
            add(q6choiceC);
            add(q6choiceD);


            q6choiceA.addActionListener(this);
            q6choiceB.addActionListener(this);
            q6choiceC.addActionListener(this);
            q6choiceD.addActionListener(this);

            validate();

        }


        //Qustion 6 choices

    if (a.getSource()==q6choiceB){//23
        q6choiceA.setEnabled(false);
        q6choiceC.setEnabled(false);
        q6choiceD.setEnabled(false);

         correct6.setFont(font2);
         next6.setFont(font2);
         next6.setBackground(Color.gray);

         add(correct6);
         add(next6);

         next6.addActionListener(this);

         score6= 13;

        validate();
     }
    else if (a.getSource()==q6choiceA){//23
        q6choiceB.setEnabled(false);
        q6choiceC.setEnabled(false);
        q6choiceD.setEnabled(false);

        wrong6.setFont(font2);
        next6.setFont(font2);
         next6.setBackground(Color.gray);

        add(wrong6);
        add(next6);

        next6.addActionListener(this);

        score6=0;

        validate();
     }
    else if (a.getSource()==q6choiceC){
        q6choiceA.setEnabled(false);
        q6choiceB.setEnabled(false);
        q6choiceD.setEnabled(false);

         wrong6.setFont(font2);
         next6.setFont(font2);
         next6.setBackground(Color.gray);

         add(wrong6);
         add(next6);

         next6.addActionListener(this);

         score6=0;

        validate();
     }
    else if (a.getSource()==q6choiceD){
        q6choiceA.setEnabled(false);
        q6choiceB.setEnabled(false);
        q6choiceC.setEnabled(false);

         wrong6.setFont(font2);
         next6.setFont(font2);
         next6.setBackground(Color.gray);

         add(wrong6);
         add(next6);

         next6.addActionListener(this);

         score6=0;

        validate();
     }

    if (a.getSource()== next6){

        totalScore = score1+ score2 + score3 + score4 + score5 + score6;

        JOptionPane.showMessageDialog(null,"You have "+ totalScore + " points.");

    }   


}//3rd brace    



    public static void main(String[]args){//23
        new experiment();
    }//23


}   //1st brace

0 个答案:

没有答案
相关问题