Alex
Alex

Reputation: 183

How to display score after a quiz using javax swing?

I am trying to write a program to take a quiz and then display the score at the end of 5 q. But my score always shows 0 regardless of what I do.

I have attached most part of the code so that it can be inserted into an IDE for checking.

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

class OnlineExam extends JFrame
{
    CardLayout cardlayout;
JPanel jpanel1,jpanel2,jpanel3,jpanel4,cardpanel,buttonpanel;
JLabel label1,label2,label3,label4,label5,label6,qlabel1,qlabel2,qlabel3,qlabel4,qlabel5,alabel1,alabel2,alabel3,alabel4,alabel5,jscore;
JTextArea field1,field2;
    JButton b1,b2,ab11,ab12,ab13,ab14,ab21,ab22,ab23,ab24,ab25,ab31,ab32,ab33,ab34,ab35,ab41,ab42,ab43,ab44,ab45,ab51,ab52,ab53,ab54,ab55,result;
    int score;

OnlineExam()
{
JFrame frame=new JFrame("Exam On C Programming");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,500);

    cardlayout=new CardLayout();

        cardpanel=new JPanel();
cardpanel.setLayout(cardlayout);

//JPanel1::
jpanel1=new JPanel();
jpanel1.setBackground(Color.pink);

label1=new JLabel("Enter Username");
label1.setFont(new Font("Arial",Font.BOLD,16));

label2=new JLabel("Enter password");
label2.setFont(new Font("Arial",Font.BOLD,16));

field1=new JTextArea(1,40);
field2=new JTextArea(1,40);

jpanel1.add(label1);
jpanel1.add(field1);
jpanel1.add(label2);
jpanel1.add(field2);

b1=new JButton("Next");
b1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
  {
  cardlayout.show(cardpanel,"2");
}
});
jpanel1.add(b1);

//jpanel 2::

        jpanel2=new JPanel();
jpanel2.setBackground(Color.pink);

        jpanel2.setLayout(new BoxLayout(jpanel2,BoxLayout.Y_AXIS));

label3=new JLabel("                          EXAM INSTRUCTIONS                      ");
label3.setFont(new Font("Arial",Font.BOLD,20));

label4=new JLabel("1. The exam consists of 5 questions. Each question is of 1 mark.");
label4.setFont(new Font("Arial",Font.PLAIN,16));

label5=new JLabel("2. The total time alloted is 5 min.");
label5.setFont(new Font("Arial",Font.PLAIN,16));

       label6=new JLabel("3. There will be no negative marking.");
  label6.setFont(new Font("Arial",Font.PLAIN,16));

  jpanel2.add(label3);
  jpanel2.add(label4);
  jpanel2.add(label5);
  jpanel2.add(label6);

  b2=new JButton("Ready for Exam?  Click here.");
  b2.addActionListener(new ActionListener()
  {
        public void actionPerformed(ActionEvent e)
      {
    cardlayout.show(cardpanel,"3");
  }
  });
       jpanel2.add(b2);

  jpanel3=new JPanel();
  jpanel3.setBackground(Color.yellow);
       jpanel3.setLayout(new BoxLayout(jpanel3,BoxLayout.Y_AXIS));

  qlabel1=new JLabel("1. Command Line Arguments have how many parameters?");
       qlabel2=new JLabel("2. Graph data structure is used in____");
       qlabel3=new JLabel("3. C language is a ________ language");
       qlabel4=new JLabel("4. what is sequence of extensions of file created after we hit the run button? ");
       qlabel5=new JLabel("5. Tower of Hanoi problem can be solved using____");

  alabel1=new JLabel("Result");
  alabel2=new JLabel("Result");
  alabel3=new JLabel("Result");
  alabel4=new JLabel("Result");
  alabel5=new JLabel("Result");

  ab11=new JButton("1");
  ab11.addActionListener(new ActionListener(){
  public void actionPerformed(ActionEvent e)
  {
  alabel1.setText("Wrong");
}
});

  ab12=new JButton("2");
  ab12.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e)
    {
    alabel1.setText("Correct");
    score++;
  }
  });

  ab13=new JButton("3");
  ab13.addActionListener(new ActionListener(){
  public void actionPerformed(ActionEvent e)
  {
  alabel1.setText("Wrong");
}
});

ab14=new JButton("None");
  ab14.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e)
    {
    alabel1.setText("Wrong");
  }
  });

  ab21=new JButton("running appliances");
  ab21.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e)
    {
    alabel2.setText("Wrong");
  }
  });

  ab22=new JButton("sharing media files");
  ab22.addActionListener(new ActionListener(){
  public void actionPerformed(ActionEvent e)
  {
  alabel2.setText("Wrong");
}
});

ab23=new JButton("finding places");
  ab23.addActionListener(new ActionListener(){
  public void actionPerformed(ActionEvent e)
  {
  alabel2.setText("Correct");
   score++;
}
});

  ab24=new JButton("playing video games");
  ab24.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e)
    {
    alabel2.setText("Wrong");
  }
  });

  ab31=new JButton("structural");
  ab31.addActionListener(new ActionListener(){
  public void actionPerformed(ActionEvent e)
  {
  alabel3.setText("Wrong");
}
});

ab32=new JButton("procedural");
  ab32.addActionListener(new ActionListener(){
  public void actionPerformed(ActionEvent e)
  {
  alabel3.setText("Correct");
  score++;
}
});

ab33=new JButton("object oriented");
  ab33.addActionListener(new ActionListener(){
  public void actionPerformed(ActionEvent e)
  {
  alabel3.setText("Wrong");
}
});

ab34=new JButton("None");
  ab34.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e)
    {
    alabel3.setText("Wrong");
  }
  });

  ab41=new JButton(".c->.obj->.bak");
  ab41.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e)
    {
    alabel4.setText("Correct");
     score++;
  }
  });

  ab42=new JButton(".c->.bak->.obj");
  ab42.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e)
    {
    alabel4.setText("Wrong");
  }
  });

  ab43=new JButton(".obj->.bak->.c");

  ab43.addActionListener(new ActionListener()
      {
    public void actionPerformed(ActionEvent e)
    {
    alabel4.setText("Wrong");
  }
  });

  ab44=new JButton("None");
  ab44.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e)
    {alabel4.setText("Wrong");
  }
  });

  ab51=new JButton("binary tree");
  ab51.addActionListener(new ActionListener(){
  public void actionPerformed(ActionEvent e)
  {
  alabel5.setText("Wrong");
}
});
  ab52=new JButton("queue");
  ab52.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e)
    {
    alabel5.setText("Wrong");
  }
  });

  ab53=new JButton("recursion");
  ab53.addActionListener(new ActionListener(){
  public void actionPerformed(ActionEvent e)
  {
  alabel5.setText("Correct");
  score++;
}
});
ab54=new JButton("None");
  ab54.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e)
    {
    alabel4.setText("Wrong");
  }
  });

  jpanel3.add(qlabel1);
  jpanel3.add(ab11);
  jpanel3.add(ab12);
  jpanel3.add(ab13);
  jpanel3.add(ab14);
  jpanel3.add(alabel1);

       jpanel3.add(qlabel2);
  jpanel3.add(ab21);
  jpanel3.add(ab22);
  jpanel3.add(ab23);
  jpanel3.add(ab24);
  jpanel3.add(alabel2);

  jpanel3.add(qlabel3);
  jpanel3.add(ab31);
  jpanel3.add(ab32);
  jpanel3.add(ab33);
  jpanel3.add(ab34);
  jpanel3.add(alabel3);

  jpanel3.add(qlabel4);
  jpanel3.add(ab41);
  jpanel3.add(ab42);
  jpanel3.add(ab43);
  jpanel3.add(ab44);
  jpanel3.add(alabel4);

  jpanel3.add(qlabel5);
  jpanel3.add(ab51);
  jpanel3.add(ab52);
  jpanel3.add(ab53);
  jpanel3.add(ab54);
  jpanel3.add(alabel5);

       result=new JButton("Click to see your reportcard");
       result.addActionListener(new ActionListener()
       {
  public void actionPerformed(ActionEvent e)
  {
 cardlayout.show(cardpanel,"4");
  }
  });
       jpanel3.add(result);

       jpanel4=new JPanel();
       jscore=new JLabel();
       if(score==5)
  {
    jscore.setText("******Congratulations!!! A perfect score indeed!!!!!!******");
  }
       else if(score>=3)
       {
  jscore.setText("You have passed. Your score is : "+ score);
  }
  else
  {
  jscore.setText("You have failed. Your score is :"+ score);
  }

       jpanel4.add(jscore);
  cardpanel.add(jpanel1,"1");
  cardpanel.add(jpanel2,"2");
  cardpanel.add(jpanel3,"3");
       cardpanel.add(jpanel4,"4");

  add(cardpanel,BorderLayout.CENTER);
       //add(buttonpanel,BorderLayout.SOUTH);

  frame.setVisible(true);
       frame.setResizable(true);
}
public static void main(String[] args)
          {
   OnlineExam frame = new OnlineExam();
                frame.setTitle("EXAM ON C PROGRAMMING");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,500);
frame.setVisible(true);
                frame.pack();
            }

}

Upvotes: 0

Views: 524

Answers (1)

Mustafa Poya
Mustafa Poya

Reputation: 3027

the problem is with location of your code:

if (score == 5) {
    jscore.setText("******Congratulations!!! A perfect score indeed!!!!!!******");
} else if (score >= 3) {
    jscore.setText("You have passed. Your score is : " + score);
} else {
    jscore.setText("You have failed. Your score is :" + score);
}

according to your code this section runs only one time and the jscore text does not change after changing the correct answers so I moved this section of your code inside result button to check it after each click of this as:

result = new JButton("Click to see your reportcard");
result.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        cardlayout.show(cardpanel, "4");

        if (score == 5) {
            jscore.setText("******Congratulations!!! A perfect score indeed!!!!!!******");
        } else if (score >= 3) {
            jscore.setText("You have passed. Your score is : " + score);
        } else {
            jscore.setText("You have failed. Your score is :" + score);
        }
    }
});

the complete code:

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

class OnlineExam extends JFrame {
    CardLayout cardlayout;
    JPanel jpanel1, jpanel2, jpanel3, jpanel4, cardpanel, buttonpanel;
    JLabel label1, label2, label3, label4, label5, label6, qlabel1, qlabel2, qlabel3, qlabel4, qlabel5, alabel1,
            alabel2, alabel3, alabel4, alabel5, jscore;
    JTextArea field1, field2;
    JButton b1, b2, ab11, ab12, ab13, ab14, ab21, ab22, ab23, ab24, ab25, ab31, ab32, ab33, ab34, ab35, ab41, ab42,
            ab43, ab44, ab45, ab51, ab52, ab53, ab54, ab55, result;
    int score = 0;

    OnlineExam() {
        JFrame frame = new JFrame("Exam On C Programming");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(500, 500);

        cardlayout = new CardLayout();

        cardpanel = new JPanel();
        cardpanel.setLayout(cardlayout);

//JPanel1::
        jpanel1 = new JPanel();
        jpanel1.setBackground(Color.pink);

        label1 = new JLabel("Enter Username");
        label1.setFont(new Font("Arial", Font.BOLD, 16));

        label2 = new JLabel("Enter password");
        label2.setFont(new Font("Arial", Font.BOLD, 16));

        field1 = new JTextArea(1, 40);
        field2 = new JTextArea(1, 40);

        jpanel1.add(label1);
        jpanel1.add(field1);
        jpanel1.add(label2);
        jpanel1.add(field2);

        b1 = new JButton("Next");
        b1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                cardlayout.show(cardpanel, "2");
            }
        });
        jpanel1.add(b1);

//jpanel 2::

        jpanel2 = new JPanel();
        jpanel2.setBackground(Color.pink);

        jpanel2.setLayout(new BoxLayout(jpanel2, BoxLayout.Y_AXIS));

        label3 = new JLabel("                          EXAM INSTRUCTIONS                      ");
        label3.setFont(new Font("Arial", Font.BOLD, 20));

        label4 = new JLabel("1. The exam consists of 5 questions. Each question is of 1 mark.");
        label4.setFont(new Font("Arial", Font.PLAIN, 16));

        label5 = new JLabel("2. The total time alloted is 5 min.");
        label5.setFont(new Font("Arial", Font.PLAIN, 16));

        label6 = new JLabel("3. There will be no negative marking.");
        label6.setFont(new Font("Arial", Font.PLAIN, 16));

        jpanel2.add(label3);
        jpanel2.add(label4);
        jpanel2.add(label5);
        jpanel2.add(label6);

        b2 = new JButton("Ready for Exam?  Click here.");
        b2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                cardlayout.show(cardpanel, "3");
            }
        });
        jpanel2.add(b2);

        jpanel3 = new JPanel();
        jpanel3.setBackground(Color.yellow);
        jpanel3.setLayout(new BoxLayout(jpanel3, BoxLayout.Y_AXIS));

        qlabel1 = new JLabel("1. Command Line Arguments have how many parameters?");
        qlabel2 = new JLabel("2. Graph data structure is used in____");
        qlabel3 = new JLabel("3. C language is a ________ language");
        qlabel4 = new JLabel("4. what is sequence of extensions of file created after we hit the run button? ");
        qlabel5 = new JLabel("5. Tower of Hanoi problem can be solved using____");

        alabel1 = new JLabel("Result");
        alabel2 = new JLabel("Result");
        alabel3 = new JLabel("Result");
        alabel4 = new JLabel("Result");
        alabel5 = new JLabel("Result");

        ab11 = new JButton("1");
        ab11.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                alabel1.setText("Wrong");
            }
        });

        ab12 = new JButton("2");
        ab12.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                alabel1.setText("Correct");
                score++;
            }
        });

        ab13 = new JButton("3");
        ab13.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                alabel1.setText("Wrong");
            }
        });

        ab14 = new JButton("None");
        ab14.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                alabel1.setText("Wrong");
            }
        });

        ab21 = new JButton("running appliances");
        ab21.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                alabel2.setText("Wrong");
            }
        });

        ab22 = new JButton("sharing media files");
        ab22.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                alabel2.setText("Wrong");
            }
        });

        ab23 = new JButton("finding places");
        ab23.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                alabel2.setText("Correct");
                score++;
            }
        });

        ab24 = new JButton("playing video games");
        ab24.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                alabel2.setText("Wrong");
            }
        });

        ab31 = new JButton("structural");
        ab31.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                alabel3.setText("Wrong");
            }
        });

        ab32 = new JButton("procedural");
        ab32.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                alabel3.setText("Correct");
                score++;
            }
        });

        ab33 = new JButton("object oriented");
        ab33.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                alabel3.setText("Wrong");
            }
        });

        ab34 = new JButton("None");
        ab34.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                alabel3.setText("Wrong");
            }
        });

        ab41 = new JButton(".c->.obj->.bak");
        ab41.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                alabel4.setText("Correct");
                score++;
            }
        });

        ab42 = new JButton(".c->.bak->.obj");
        ab42.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                alabel4.setText("Wrong");
            }
        });

        ab43 = new JButton(".obj->.bak->.c");

        ab43.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                alabel4.setText("Wrong");
            }
        });

        ab44 = new JButton("None");
        ab44.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                alabel4.setText("Wrong");
            }
        });

        ab51 = new JButton("binary tree");
        ab51.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                alabel5.setText("Wrong");
            }
        });
        ab52 = new JButton("queue");
        ab52.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                alabel5.setText("Wrong");
            }
        });

        ab53 = new JButton("recursion");
        ab53.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                alabel5.setText("Correct");
                score++;
            }
        });
        ab54 = new JButton("None");
        ab54.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                alabel4.setText("Wrong");
            }
        });

        jpanel3.add(qlabel1);
        jpanel3.add(ab11);
        jpanel3.add(ab12);
        jpanel3.add(ab13);
        jpanel3.add(ab14);
        jpanel3.add(alabel1);

        jpanel3.add(qlabel2);
        jpanel3.add(ab21);
        jpanel3.add(ab22);
        jpanel3.add(ab23);
        jpanel3.add(ab24);
        jpanel3.add(alabel2);

        jpanel3.add(qlabel3);
        jpanel3.add(ab31);
        jpanel3.add(ab32);
        jpanel3.add(ab33);
        jpanel3.add(ab34);
        jpanel3.add(alabel3);

        jpanel3.add(qlabel4);
        jpanel3.add(ab41);
        jpanel3.add(ab42);
        jpanel3.add(ab43);
        jpanel3.add(ab44);
        jpanel3.add(alabel4);

        jpanel3.add(qlabel5);
        jpanel3.add(ab51);
        jpanel3.add(ab52);
        jpanel3.add(ab53);
        jpanel3.add(ab54);
        jpanel3.add(alabel5);

        result = new JButton("Click to see your reportcard");
        result.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                cardlayout.show(cardpanel, "4");

                if (score == 5) {
                    jscore.setText("******Congratulations!!! A perfect score indeed!!!!!!******");
                } else if (score >= 3) {
                    jscore.setText("You have passed. Your score is : " + score);
                } else {
                    jscore.setText("You have failed. Your score is :" + score);
                }
            }
        });
        jpanel3.add(result);

        jpanel4 = new JPanel();
        jscore = new JLabel();

        jpanel4.add(jscore);
        cardpanel.add(jpanel1, "1");
        cardpanel.add(jpanel2, "2");
        cardpanel.add(jpanel3, "3");
        cardpanel.add(jpanel4, "4");

        add(cardpanel, BorderLayout.CENTER);
        // add(buttonpanel,BorderLayout.SOUTH);

        frame.setVisible(true);
        frame.setResizable(true);
    }

    public static void main(String[] args) {
        OnlineExam frame = new OnlineExam();
        frame.setTitle("EXAM ON C PROGRAMMING");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(500, 500);
        frame.setVisible(true);
        frame.pack();
    }

}

Upvotes: 1

Related Questions