-1

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);
        
    }
}
Frakcool
  • 10,915
  • 9
  • 50
  • 89
polar. B
  • 35
  • 3
  • 1
    please explain this part: "it didn't work well" – jhamon Jul 27 '20 at 12:19
  • I had made some edit explaining about the problem I wen through – polar. B Jul 27 '20 at 12:27
  • 1
    Please explain what's the error on your program? This is a ton of code that no one wants to read. So please create and post a proper [mre] that demonstrates your issue, remove the unnecessary code and btw, don't use `setLayout(null);` or `.setBounds(...)` those are gonna cause you a lot of issue, like [this one](https://stackoverflow.com/a/42521097/2180785) as Swing has to deal with different PLAFs, OS, screen sizes and resolutions. – Frakcool Jul 27 '20 at 15:19
  • 1
    A classmate is having basically [the same issue as you](https://stackoverflow.com/q/63109970/2180785) why don't you work together to come up with a good solution? – Frakcool Jul 27 '20 at 15:49

1 Answers1

2

Phew! Where do I start with your program? It's wrong in a lot of ways:

  1. Swing has to deal with multiple PLAFs, OS, screen sizes and resolutions, that's why pixel perfect GUIs should be avoided, as you might run into this kind of issues (and you did), as I bet your questions aren't cropped in your computer and the buttons contain a letter and not just ... which means that your content isn't fitting the size of the button. So avoid the use of setLayout(null) and .setBounds(...). See Why is it frowned upon to use null layout? to expand on this.

    enter image description here

  2. For Quiz-like programs, one of the best layouts to use is CardLayout, this will allow you to switch between panels. Here is a simple example of how to use it.

  3. Avoid the use of setSize(...) on the JFrame, it's recommended to instead override getPreferredSize on the JPanel and then pack() your JFrame, this way, it will add the window decorations apart from your UI size, see Should I avoid the use of setPreferred|Maximum|MinimumSize? (Yes)

  4. Learn how to use arrays and collections such as List, this will greatly improve your code and reduce the repetitive lines of code in it, make it easier to read and understand.

  5. Be consistent with your variable namings, Java naming conventions state that your variables should be written in camelCase, without underscores.

    • FirstWordUpperCaseClass
    • firstWordLowerCaseVariable
    • firstWordLowerCaseMethod()
    • ALL_WORDS_UPPER_CASED_CONSTANT
  6. Instead of having all your questions and answers and which answer is the correct one in 3 different arrays, move them into a Model class and create N instances of it as needed for every question (again, lists and arrays are your friends here).

  7. The first screen you mention, should be a JDialog or JOptionPane, and I'd say the results screen as well.

  8. Also avoid the use of multiple JFrames, it creates a horrible UX, see The use of multiple JFrames, good / bad practice? (Bad)

With all the above recommendations, here's a very similar UI to yours, I didn't add all the functionality but this should be enough for a step in the right direction:

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.UIManager;

//This is the main class, it contains the CardLayout
public class QuizOne {
    private JFrame frame;
    
    private QuestionPane[] cards;
    private JPanel pane;
    private CardLayout cl;
    
    private JLabel timerLabel;
    
    private List<QuizQuestion> questions;
    
    private Timer temporaryTimer;
    
    public static void main(String[] args) {
        //We place our program on the EDT using Java 8 lambdas
        SwingUtilities.invokeLater(new QuizOne()::createAndShowGUI);
    }
    
    //Here's where the magic happens
    private void createAndShowGUI() {
        frame = new JFrame(getClass().getSimpleName()); //Get the class name and set it as the frame's title
        cl = new CardLayout(); //Create a new CardLayout
        
        pane = new JPanel(cl); //Set the CardLayout to this JPanel
        
        temporaryTimer = new Timer(500, event -> { //The timer to show the result of the answer for half second before switching to the new one.
            cl.next(pane); //This moves the CardLayout to the next one
            temporaryTimer.stop(); //We stop this timer when we switch to the next card.
        });
        
        generateQuestionsAndAnswers(); //We populate the model of questions with their answers here.
        
        cards = new QuestionPane[questions.size()];
        for (int i = 0; i < cards.length; i++) {
            cards[i] = new QuestionPane(questions.get(i), i, pane, cl, cards.length, frame, temporaryTimer); //We create a new QuestionPane and send some information as parameters
            
            pane.add(cards[i], "question" + i); //We add the card to the CardLayout pane
        }
        
        timerLabel = new JLabel("Time: ");
        
        UIManager.put("OptionPane.okButtonText", "Start Quiz"); //We change the "OK" from the JOptionPane button to "Start Quiz"
        int option = JOptionPane.showConfirmDialog(frame, new JLabel("Click button to start quiz"), "Welcome", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null);
        if (option == JOptionPane.OK_OPTION) {
            //Start your timer for the first question
        }
        
        frame.add(pane); //We add the CardLayout pane to our JFrame's CENTER position
        frame.add(timerLabel, BorderLayout.SOUTH); //And the timerLabel at the bottom
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true); //
    }
    
    //We create an ArrayList of QuizQuestion that each has their own question, the possible answers and the correct answer (index + 1)
    private void generateQuestionsAndAnswers() {
        questions = new ArrayList<>();
        questions.add(new QuizQuestion("Select the official name of the coronavirus.", new String[] {"COVID-19","Sars-CoV-2","Zaire ebolavirus","Influenza"}, 1));
        questions.add(new QuizQuestion("When did the corona virus first ecountered?", new String[] {"2018","2020","2017","2019"}, 4));
        questions.add(new QuizQuestion("What is the percentage of people recovering from the coronavirus?", new String[] {"63%","71%","80%","76%"}, 3));
        questions.add(new QuizQuestion("Which below is NOT the symptom of coronavirus?", new String[] {"Fever","Blurred vision","Dry Cough","Nasal Congestion"}, 2));
        questions.add(new QuizQuestion("Which part of the human body does the coronavirus attach itself to?", new String[] {"Red Blood Cells", "Antigens", "White Blood Cells", "Ace-2 recpetors in the airways"}, 4));
    }
}

@SuppressWarnings("serial")
class ResultsPane extends JPanel { //This is a class that will create a simple JPanel with vertical alignment to add the number of correct answers, accuracy and a text for the user if they want to retry
    public ResultsPane(int correctAnswers, int totalQuestions) {
        setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
        float percentage = ((float) (correctAnswers) / (float) (totalQuestions)) * 100;
        
        add(new JLabel("Correct Answers: " + correctAnswers + " / " + totalQuestions));
        add(new JLabel("Accuracy: " + percentage + "%"));
        add(new JLabel("Want to Retry?"));
    }
}

@SuppressWarnings("serial")
class QuestionPane extends JPanel { //This is the pane in which each card will be displayed
    private JButton[] answersButtons; //Array of buttons for the answers instead of 4 individual buttons
    
    private JLabel questionLabel;
    private JLabel questionNumber;
    private JLabel[] answerLabels; //Same for the labels
    
    private static int correctAnswers = 0; //This is static to count all the correct answers in all the instances
    
    public QuestionPane(QuizQuestion question, int currentQuestion, JPanel pane, CardLayout cl, int totalQuestions, JFrame frame, Timer timer) { //Probably this isn't the most elegant solution to send multiple objects as parameters here, as it makes the program tightly coupled.
        setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
        
        questionNumber = new JLabel("Question " + (currentQuestion + 1), SwingConstants.CENTER); //We set the question number on top and center the text
        
        questionLabel = new JLabel(question.getQuestion()); // We set the question text to this label
        answerLabels = new JLabel[question.getAnswers().length]; //We create our array of 4 labels and 4 buttons below
        answersButtons = new JButton[question.getAnswers().length];
        
        for (int i = 0; i < question.getAnswers().length; i++) {
            answersButtons[i] = new JButton(String.valueOf((char) ('A' + i))); // This will set the buttons text to A, B, C, D
            answersButtons[i].addActionListener(e -> { //ActionListener using Java 8 lambdas
                if (e.getActionCommand().charAt(0) - 'A' == question.getCorrectAnswer() - 1) { //Here we check if the button clicked was the one with the correct answer, converting the text from A-D to 0-3 and compare it to the index - 1 from the question model
                    correctAnswers++; //Increase the correctAnswer + 1
                    answerLabels[e.getActionCommand().charAt(0) - 'A'].setBackground(Color.GREEN); //Set the background color to green if it was correct
                } else {
                    answerLabels[e.getActionCommand().charAt(0) - 'A'].setBackground(Color.RED); //Or red otherwise
                }
                if (currentQuestion == totalQuestions - 1) { //If we reach the end of questions, show the results screen
                    int input = JOptionPane.showConfirmDialog(pane, new ResultsPane(correctAnswers, totalQuestions), "Results", JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE, null);
                    if (input == JOptionPane.YES_OPTION) {
                        //Reset everything and show your GUI again
                    } else {
                        frame.dispose(); //If user says they don't want to retry, dispose the frame.
                    }
                } else {
                    timer.start(); //Start the timer that will display the results for half a second.
                }
            });
        }
        
        add(questionNumber); //Add the question number
        add(questionLabel); //The question text
        
        for (int i = 0; i < question.getAnswers().length; i++) {
            JPanel answerPane = new JPanel(new FlowLayout(FlowLayout.LEFT)); //Create a new JPanel for each label and button and make them left aligned
            
            answerPane.add(answersButtons[i]); //Add every button
            answerLabels[i] = new JLabel(question.getAnswers()[i]); //Create a new label with each answer's text
            answerLabels[i].setOpaque(true); //Make them opaque (for the background colors later)
            answerPane.add(answerLabels[i]); //And add them to the pane
            
            add(answerPane); //Then add the pane to the wrapping pane
        }
    }
}

//A simple model for your questions.
class QuizQuestion {
    private String question;
    private String[] answers;
    private int correctAnswer;
    
    public QuizQuestion(String question, String[] answers, int correctAnswer) {
        super();
        this.question = question;
        this.answers = answers;
        this.correctAnswer = correctAnswer;
    }
    
    public String getQuestion() {
        return question;
    }
    
    public void setQuestion(String question) {
        this.question = question;
    }
    
    public String[] getAnswers() {
        return answers;
    }
    
    public void setAnswers(String[] answers) {
        this.answers = answers;
    }
    
    public int getCorrectAnswer() {
        return correctAnswer;
    }
    
    public void setCorrectAnswer(int correctAnswer) {
        this.correctAnswer = correctAnswer;
    }
}

And some SS of the program, note that I didn't include formatting as I'm leaving that part to you, trying to keep things simple here.

enter image description here enter image description here enter image description here enter image description here

Frakcool
  • 10,915
  • 9
  • 50
  • 89