While I was writing this code,I wanted to add the start page where you are able to click the start button to start the actual quiz. I had made the actual quiz structure, but while I was trying to make the start page, it didn't work well. When I combined it the start page did work and I was able to move on to the quiz part. However, the quiz did not worked properly as it did not show full 15 questions instead it only showed a few and straightly moved it to the result.
This is my code for the quiz and the below code is the code I came up with the start page. I don't know how to add this together. It would be really helpful if someone gives me some help.
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class TriviaOne implements ActionListener{
//Questions that will be in the trivia
String[] questions = {
"Select the official name of the coronavirus.",
"When did the corona virus first ecountered?",
"What is the percentage of people recovering from the coronavirus?",
"Which below is NOT the symptom of coronavirus?",
"Which part of the human body does the coronavirus attach itself to?",
"How many hour can the coronavirus survive on plastic and stainless steel surfaces?",
"Whihc human organs in the body does the coronavirus attack?",
"How large is the coronavirus?",
"Which is a safe distance to stay apart from people? ",
"Who has the highest risk of getting infected by coronvirus?",
"When should face masks be worn?",
"Which is more effective for removing the coronavirus from your hands?",
"Which industry includes workers with increased exposure-risk?",
"What is the period of quartine?",
"What is the name of the city where coronavirus was first detected?"
};
//the 4 answer choices that leads the user to choose
String[][] options = {
{"COVID-19","Sars-CoV-2","Zaire ebolavirus","Influenza"},
{"2018","2020","2017","2019"},
{"63%","71%","80%","76%"},
{"Fever","Blurred vision","Dry Cough","Nasal Congestion"},
{"Red Blood Cells", "Antigens", "White Blood Cells", "Ace-2 recpetors in the airways"},
{"4-8 hours", "72 hours and more", "45-60 hours", "90 hours and more" },
{"Liver", "Lungs", "Heart", "Kidney"},
{"8000 billionths of metre in diameter", "800 billionths of metre in diameter","80 billionths of metre in diameter","8 billionths of metre in diameter"},
{"3 feet(1 meter)", "2 feet(60 cm)", "1 foot (30cm)", "4.2 feet(1.3 meter)"},
{"Children", "Pregnant Women", "People over 60 years of age", "30-40 years agr of men"},
{"Public Transport", "Confined or Crowed spaces", "Small restaurants or shops", "All of the above"},
{"Soap and water", "Alcohol-based hand sanitiser","Detergent", "Face cleanser"},
{"Health care", "Airline operations", "Waste management", "All of the above"},
{"21 days","7 days", "14 days", "6 days"},
{"Wuhan", "Hubei", "Hunan","Shanghai"}
};
//correct answers for the following questions
char[] answers = {
'A',
'D',
'C',
'B',
'D',
'B',
'B',
'C',
'A',
'C',
'D',
'A',
'D',
'C',
'A'
};
char guess;
char answer;
int index;
int correct_guesses =0;
int total_questions = questions.length;
int result;
int seconds=15;
JFrame frame = new JFrame();
JTextField textfield = new JTextField();
JTextArea textarea = new JTextArea();
JButton buttonA = new JButton();
JButton buttonB = new JButton();
JButton buttonC = new JButton();
JButton buttonD = new JButton();
JLabel answer_labelA = new JLabel();
JLabel answer_labelB = new JLabel();
JLabel answer_labelC = new JLabel();
JLabel answer_labelD = new JLabel();
JLabel time_label = new JLabel();
JLabel seconds_left = new JLabel();
JTextField number_right = new JTextField();
JTextField percentage = new JTextField();
Timer timer = new Timer(1500, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
seconds--;
seconds_left.setText(String.valueOf(seconds));
if(seconds<=0) {
displayAnswer();
}
}
});
public TriviaOne() {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(700,700);
frame.getContentPane().setBackground(new Color(204,229,255));
frame.setLayout(null);
frame.setResizable(false);
textfield.setBounds(0,0,700,70);
textfield.setBackground(new Color(153, 204, 255));
textfield.setForeground(new Color(0,25,51));
textfield.setFont(new Font("PT Serif",Font.BOLD,33));
textfield.setBorder(BorderFactory.createBevelBorder(0));
textfield.setHorizontalAlignment(JTextField.CENTER);
textfield.setEditable(false);
textarea.setBounds(0,70,700,90);
textarea.setLineWrap(true);
textarea.setWrapStyleWord(true);
textarea.setBackground(new Color(204,229,255));
textarea.setForeground(new Color(0,25,51));
textarea.setFont(new Font("PT Serif",Font.PLAIN,30));
textarea.setLocation(16, 84);
textarea.setEditable(false);
buttonA.setBounds(50,200,50,50);
buttonA.setFont(new Font("PT Serif",Font.BOLD,33));
buttonA.setFocusable(false);
buttonA.addActionListener(this);
buttonA.setText("A");
buttonB.setBounds(50,300,50,50);
buttonB.setFont(new Font("PT Serif",Font.BOLD,33));
buttonB.setFocusable(false);
buttonB.addActionListener(this);
buttonB.setText("B");
buttonC.setBounds(50,400,50,50);
buttonC.setFont(new Font("PT Serif",Font.BOLD,33));
buttonC.setFocusable(false);
buttonC.addActionListener(this);
buttonC.setText("C");
buttonD.setBounds(50,500,50,50);
buttonD.setFont(new Font("PT Serif",Font.BOLD,33));
buttonD.setFocusable(false);
buttonD.addActionListener(this);
buttonD.setText("D");
answer_labelA.setBounds(130,175,500,100);
answer_labelA.setBackground(new Color(50,50,50));
answer_labelA.setForeground(new Color(0,25,51));
answer_labelA.setFont(new Font("PT Serif",Font.PLAIN,26));
answer_labelB.setBounds(130,275,500,100);
answer_labelB.setBackground(new Color(50,50,50));
answer_labelB.setForeground(new Color(0,25,51));
answer_labelB.setFont(new Font("PT Serif",Font.PLAIN,26));
answer_labelC.setBounds(130,375,500,100);
answer_labelC.setBackground(new Color(50,50,50));
answer_labelC.setForeground(new Color(0,25,51));
answer_labelC.setFont(new Font("PT Serif",Font.PLAIN,26));
answer_labelD.setBounds(130,475,500,100);
answer_labelD.setBackground(new Color(204,229,255));
answer_labelD.setForeground(new Color(0,25,51));
answer_labelD.setFont(new Font("PT Serif",Font.PLAIN,26));
seconds_left.setBounds(150,575,100,70);
seconds_left.setBackground(new Color(204,229,255));
seconds_left.setForeground(new Color(102, 102, 255));
seconds_left.setFont(new Font("PT Serif",Font.PLAIN,37));
seconds_left.setOpaque(true);
seconds_left.setHorizontalAlignment(JTextField.CENTER);
seconds_left.setText(String.valueOf(seconds));
time_label.setBounds(50,575,100,70);
time_label.setBackground(new Color(204,229,255));
time_label.setForeground(new Color(102, 102, 255));
time_label.setFont(new Font("PT Serif",Font.PLAIN,35));
time_label.setHorizontalAlignment(JTextField.CENTER);
time_label.setText("Timer");
number_right.setBounds(225,225,200,100);
number_right.setBackground(new Color(153,204,255));
number_right.setForeground(new Color(0, 102, 204));
number_right.setFont(new Font("PT Serif",Font.BOLD,50));
number_right.setBorder(BorderFactory.createBevelBorder(1));
number_right.setHorizontalAlignment(JTextField.CENTER);
number_right.setEditable(false);
percentage.setBounds(225,325,200,100);
percentage.setBackground(new Color(153,204,255));
percentage.setForeground(new Color(0,102,204));
percentage.setFont(new Font("PT Serif",Font.BOLD,50));
percentage.setBorder(BorderFactory.createBevelBorder(1));
percentage.setHorizontalAlignment(JTextField.CENTER);
percentage.setEditable(false);
frame.add(time_label);
frame.add(seconds_left);
frame.add(answer_labelA);
frame.add(answer_labelB);
frame.add(answer_labelC);
frame.add(answer_labelD);
frame.add(buttonA);
frame.add(buttonB);
frame.add(buttonC);
frame.add(buttonD);
frame.add(textarea);
frame.add(textfield);
frame.setVisible(true);
nextQuestion();
}
public void nextQuestion() {
if(index>=total_questions) {
results();
}
else {
textfield.setText("Question "+(index+1));
textarea.setText(questions[index]);
answer_labelA.setText(options[index][0]);
answer_labelB.setText(options[index][1]);
answer_labelC.setText(options[index][2]);
answer_labelD.setText(options[index][3]);
timer.start();
}
}
@Override
public void actionPerformed(ActionEvent e) {
buttonA.setEnabled(false);
buttonB.setEnabled(false);
buttonC.setEnabled(false);
buttonD.setEnabled(false);
if(e.getSource()==buttonA) {
answer= 'A';
if(answer == answers[index]) {
correct_guesses++;
}
}
if(e.getSource()==buttonB) {
answer= 'B';
if(answer == answers[index]) {
correct_guesses++;
}
}
if(e.getSource()==buttonC) {
answer= 'C';
if(answer == answers[index]) {
correct_guesses++;
}
}
if(e.getSource()==buttonD) {
answer= 'D';
if(answer == answers[index]) {
correct_guesses++;
}
}
displayAnswer();
}
public void displayAnswer() {
timer.stop();
buttonA.setEnabled(false);
buttonB.setEnabled(false);
buttonC.setEnabled(false);
buttonD.setEnabled(false);
if(answers[index] != 'A')
answer_labelA.setForeground(new Color(255, 0, 0));
if(answers[index] != 'B')
answer_labelB.setForeground(new Color(255, 0, 0));
if(answers[index] != 'C')
answer_labelC.setForeground(new Color(255, 0, 0));
if(answers[index] != 'D')
answer_labelD.setForeground(new Color(255, 0, 0));
Timer pause = new Timer(2000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
answer_labelA.setForeground(new Color(0,25,51));
answer_labelB.setForeground(new Color(0,25,51));
answer_labelC.setForeground(new Color(0,25,51));
answer_labelD.setForeground(new Color(0,25,51));
answer = ' ';
seconds=15;
seconds_left.setText(String.valueOf(seconds));
buttonA.setEnabled(true);
buttonB.setEnabled(true);
buttonC.setEnabled(true);
buttonD.setEnabled(true);
index++;
nextQuestion();
}
});
pause.setRepeats(false);
pause.start();
}
public void results(){
buttonA.setEnabled(false);
buttonB.setEnabled(false);
buttonC.setEnabled(false);
buttonD.setEnabled(false);
result = (int)((correct_guesses/(double)total_questions)*100);
textfield.setText("RESULTS!");
textarea.setText("");
answer_labelA.setText("");
answer_labelB.setText("");
answer_labelC.setText("");
answer_labelD.setText("");
number_right.setText("("+correct_guesses+"/"+total_questions+")");
percentage.setText(result+"%");
frame.add(number_right);
frame.add(percentage);
}
}
This is the code for my start page.. I'm not that of a expert of a java btw...
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class Quiz implements ActionListener {
JFrame frame = new JFrame();
JTextField titleName = new JTextField();
JTextArea subtitle = new JTextArea();
JButton startButton = new JButton();
public Quiz() {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(700,700);
frame.getContentPane().setBackground(new Color(204,229,255));
frame.setLayout(null);
frame.setResizable(false);
titleName.setBounds(100, 100, 450, 150);
titleName.setBackground(new Color(204,229,255));
titleName.setForeground(new Color(0,102,204));
titleName.setFont(new Font("Roboto Condensed",Font.BOLD,80));
titleName.setText("TRIVIA");
subtitle.setText("Made by ");
subtitle.setForeground(new Color(0,102,204));
subtitle.setFont(new Font("Roboto Condensed",Font.BOLD,30));
startButton = new JButton("START");
startButton.setFont(new Font("Roboto Condensed", Font.PLAIN,25));
startButton.setBounds(200, 400, 250, 100);
startButton.setBackground(new Color(153, 204, 255));
startButton.addActionListener(this);
frame.add(titleName);
frame.add(subtitle);
frame.add(startButton);
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
This is the combined part
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class TriviaOne implements ActionListener{
//Questions that will be in the trivia
String[] questions = {
"Select the official name of the coronavirus.",
"When did the corona virus first ecountered?",
"What is the percentage of people recovering from the coronavirus?",
"Which below is NOT the symptom of coronavirus?",
"Which part of the human body does the coronavirus attach itself to?",
"How many hour can the coronavirus survive on plastic and stainless steel surfaces?",
"Whihc human organs in the body does the coronavirus attack?",
"How large is the coronavirus?",
"Which is a safe distance to stay apart from people? ",
"Who has the highest risk of getting infected by coronvirus?",
"When should face masks be worn?",
"Which is more effective for removing the coronavirus from your hands?",
"Which industry includes workers with increased exposure-risk?",
"What is the period of quartine?",
"What is the name of the city where coronavirus was first detected?"
};
//the 4 answer choices that leads the user to choose
String[][] options = {
{"COVID-19","Sars-CoV-2","Zaire ebolavirus","Influenza"},
{"2018","2020","2017","2019"},
{"63%","71%","80%","76%"},
{"Fever","Blurred vision","Dry Cough","Nasal Congestion"},
{"Red Blood Cells", "Antigens", "White Blood Cells", "Ace-2 recpetors in the airways"},
{"4-8 hours", "72 hours and more", "45-60 hours", "90 hours and more" },
{"Liver", "Lungs", "Heart", "Kidney"},
{"8000 billionths of metre in diameter", "800 billionths of metre in diameter","80 billionths of metre in diameter","8 billionths of metre in diameter"},
{"3 feet(1 meter)", "2 feet(60 cm)", "1 foot (30cm)", "4.2 feet(1.3 meter)"},
{"Children", "Pregnant Women", "People over 60 years of age", "30-40 years agr of men"},
{"Public Transport", "Confined or Crowed spaces", "Small restaurants or shops", "All of the above"},
{"Soap and water", "Alcohol-based hand sanitiser","Detergent", "Face cleanser"},
{"Health care", "Airline operations", "Waste management", "All of the above"},
{"21 days","7 days", "14 days", "6 days"},
{"Wuhan", "Hubei", "Hunan","Shanghai"}
};
//correct answers for the following questions
char[] answers = {
'A',
'D',
'C',
'B',
'D',
'B',
'B',
'C',
'A',
'C',
'D',
'A',
'D',
'C',
'A'
};
char guess;
char answer;
int index;
int correct_guesses =0;
int total_questions = questions.length;
int result;
int seconds=15;
JFrame frame = new JFrame();
JTextField textfield = new JTextField();
JTextArea textarea = new JTextArea();
JButton buttonA = new JButton();
JButton buttonB = new JButton();
JButton buttonC = new JButton();
JButton buttonD = new JButton();
JLabel answer_labelA = new JLabel();
JLabel answer_labelB = new JLabel();
JLabel answer_labelC = new JLabel();
JLabel answer_labelD = new JLabel();
JLabel time_label = new JLabel();
JLabel seconds_left = new JLabel();
JTextField number_right = new JTextField();
JTextField percentage = new JTextField();
Timer timer = new Timer(1500, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
seconds--;
seconds_left.setText(String.valueOf(seconds));
if(seconds<=0) {
displayAnswer();
}
}
});
public TriviaOne() {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(700,700);
frame.getContentPane().setBackground(new Color(204,229,255));
frame.setLayout(null);
frame.setResizable(false);
textfield.setBounds(0,0,700,70);
textfield.setBackground(new Color(153, 204, 255));
textfield.setForeground(new Color(0,25,51));
textfield.setFont(new Font("PT Serif",Font.BOLD,33));
textfield.setBorder(BorderFactory.createBevelBorder(0));
textfield.setHorizontalAlignment(JTextField.CENTER);
textfield.setEditable(false);
textarea.setBounds(0,70,700,90);
textarea.setLineWrap(true);
textarea.setWrapStyleWord(true);
textarea.setBackground(new Color(204,229,255));
textarea.setForeground(new Color(0,25,51));
textarea.setFont(new Font("PT Serif",Font.PLAIN,30));
textarea.setLocation(16, 84);
textarea.setEditable(false);
buttonA.setBounds(50,200,50,50);
buttonA.setFont(new Font("PT Serif",Font.BOLD,33));
buttonA.setFocusable(false);
buttonA.addActionListener(this);
buttonA.setText("A");
buttonB.setBounds(50,300,50,50);
buttonB.setFont(new Font("PT Serif",Font.BOLD,33));
buttonB.setFocusable(false);
buttonB.addActionListener(this);
buttonB.setText("B");
buttonC.setBounds(50,400,50,50);
buttonC.setFont(new Font("PT Serif",Font.BOLD,33));
buttonC.setFocusable(false);
buttonC.addActionListener(this);
buttonC.setText("C");
buttonD.setBounds(50,500,50,50);
buttonD.setFont(new Font("PT Serif",Font.BOLD,33));
buttonD.setFocusable(false);
buttonD.addActionListener(this);
buttonD.setText("D");
answer_labelA.setBounds(130,175,500,100);
answer_labelA.setBackground(new Color(50,50,50));
answer_labelA.setForeground(new Color(0,25,51));
answer_labelA.setFont(new Font("PT Serif",Font.PLAIN,26));
answer_labelB.setBounds(130,275,500,100);
answer_labelB.setBackground(new Color(50,50,50));
answer_labelB.setForeground(new Color(0,25,51));
answer_labelB.setFont(new Font("PT Serif",Font.PLAIN,26));
answer_labelC.setBounds(130,375,500,100);
answer_labelC.setBackground(new Color(50,50,50));
answer_labelC.setForeground(new Color(0,25,51));
answer_labelC.setFont(new Font("PT Serif",Font.PLAIN,26));
answer_labelD.setBounds(130,475,500,100);
answer_labelD.setBackground(new Color(204,229,255));
answer_labelD.setForeground(new Color(0,25,51));
answer_labelD.setFont(new Font("PT Serif",Font.PLAIN,26));
seconds_left.setBounds(150,575,100,70);
seconds_left.setBackground(new Color(204,229,255));
seconds_left.setForeground(new Color(102, 102, 255));
seconds_left.setFont(new Font("PT Serif",Font.PLAIN,37));
seconds_left.setOpaque(true);
seconds_left.setHorizontalAlignment(JTextField.CENTER);
seconds_left.setText(String.valueOf(seconds));
time_label.setBounds(50,575,100,70);
time_label.setBackground(new Color(204,229,255));
time_label.setForeground(new Color(102, 102, 255));
time_label.setFont(new Font("PT Serif",Font.PLAIN,35));
time_label.setHorizontalAlignment(JTextField.CENTER);
time_label.setText("Timer");
number_right.setBounds(225,225,200,100);
number_right.setBackground(new Color(153,204,255));
number_right.setForeground(new Color(0, 102, 204));
number_right.setFont(new Font("PT Serif",Font.BOLD,50));
number_right.setBorder(BorderFactory.createBevelBorder(1));
number_right.setHorizontalAlignment(JTextField.CENTER);
number_right.setEditable(false);
percentage.setBounds(225,325,200,100);
percentage.setBackground(new Color(153,204,255));
percentage.setForeground(new Color(0,102,204));
percentage.setFont(new Font("PT Serif",Font.BOLD,50));
percentage.setBorder(BorderFactory.createBevelBorder(1));
percentage.setHorizontalAlignment(JTextField.CENTER);
percentage.setEditable(false);
frame.add(time_label);
frame.add(seconds_left);
frame.add(answer_labelA);
frame.add(answer_labelB);
frame.add(answer_labelC);
frame.add(answer_labelD);
frame.add(buttonA);
frame.add(buttonB);
frame.add(buttonC);
frame.add(buttonD);
frame.add(textarea);
frame.add(textfield);
frame.setVisible(true);
nextQuestion();
}
public void nextQuestion() {
if(index>=total_questions) {
results();
}
else {
textfield.setText("Question "+(index+1));
textarea.setText(questions[index]);
answer_labelA.setText(options[index][0]);
answer_labelB.setText(options[index][1]);
answer_labelC.setText(options[index][2]);
answer_labelD.setText(options[index][3]);
timer.start();
}
}
@Override
public void actionPerformed(ActionEvent e) {
buttonA.setEnabled(false);
buttonB.setEnabled(false);
buttonC.setEnabled(false);
buttonD.setEnabled(false);
if(e.getSource()==buttonA) {
answer= 'A';
if(answer == answers[index]) {
correct_guesses++;
}
}
if(e.getSource()==buttonB) {
answer= 'B';
if(answer == answers[index]) {
correct_guesses++;
}
}
if(e.getSource()==buttonC) {
answer= 'C';
if(answer == answers[index]) {
correct_guesses++;
}
}
if(e.getSource()==buttonD) {
answer= 'D';
if(answer == answers[index]) {
correct_guesses++;
}
}
displayAnswer();
}
public void displayAnswer() {
timer.stop();
buttonA.setEnabled(false);
buttonB.setEnabled(false);
buttonC.setEnabled(false);
buttonD.setEnabled(false);
if(answers[index] != 'A')
answer_labelA.setForeground(new Color(255, 0, 0));
if(answers[index] != 'B')
answer_labelB.setForeground(new Color(255, 0, 0));
if(answers[index] != 'C')
answer_labelC.setForeground(new Color(255, 0, 0));
if(answers[index] != 'D')
answer_labelD.setForeground(new Color(255, 0, 0));
Timer pause = new Timer(2000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
answer_labelA.setForeground(new Color(0,25,51));
answer_labelB.setForeground(new Color(0,25,51));
answer_labelC.setForeground(new Color(0,25,51));
answer_labelD.setForeground(new Color(0,25,51));
answer = ' ';
seconds=15;
seconds_left.setText(String.valueOf(seconds));
buttonA.setEnabled(true);
buttonB.setEnabled(true);
buttonC.setEnabled(true);
buttonD.setEnabled(true);
index++;
nextQuestion();
}
});
pause.setRepeats(false);
pause.start();
}
public void results(){
buttonA.setEnabled(false);
buttonB.setEnabled(false);
buttonC.setEnabled(false);
buttonD.setEnabled(false);
result = (int)((correct_guesses/(double)total_questions)*100);
textfield.setText("RESULTS!");
textarea.setText("");
answer_labelA.setText("");
answer_labelB.setText("");
answer_labelC.setText("");
answer_labelD.setText("");
number_right.setText("("+correct_guesses+"/"+total_questions+")");
percentage.setText(result+"%");
frame.add(number_right);
frame.add(percentage);
}
}