首页 > 解决方案 > 无法在基于 GUI 的程序 java 中添加标题页

问题描述

在我编写此代码时,我想添加开始页面,您可以在其中单击开始按钮以开始实际测验。我已经制作了实际的测验结构,但是当我尝试制作起始页时,它并没有很好地工作。当我将它组合起来时,起始页面确实有效,我能够继续进行测验部分。但是,测验并没有正常工作,因为它没有显示完整的 15 个问题,而是只显示了几个问题,并直接将其移至结果。

这是我的测验代码,下面的代码是我在起始页上提出的代码。我不知道如何将这些加在一起。如果有人给我一些帮助,那将非常有帮助。

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);
        
    }
}

这是我的起始页的代码.. 我不是 java 的专家...

  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
    

这是合并的部分

 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);
        
    }
}

标签: javaswinguser-interface

解决方案


呸!我从哪里开始你的程序?在很多方面都是错误的:

  1. Swing 必须处理多个 PLAF、操作系统、屏幕尺寸和分辨率,这就是为什么应该避免使用像素完美的 GUI,因为您可能会遇到此类问题(并且您确实遇到了),因为我敢打赌您的问题不会被裁剪计算机和按钮包含一个字母,而不仅仅是...这意味着您的内容不适合按钮的大小。所以避免使用setLayout(null)and .setBounds(...)。请参阅为什么不赞成使用空布局?对此进行扩展。

    在此处输入图像描述

  2. 对于类似测验的程序,最好使用的布局之一是CardLayout,这将允许您在面板之间切换。是一个如何使用它的简单示例。

  3. 避免使用setSize(...)on JFrame,建议先覆盖然后再覆盖你的getPreferredSize,这样,除了你的 UI 大小之外,它还会添加窗口装饰,请参阅我应该避免使用 setPreferred|Maximum|MinimumSize 吗?(是的)JPanelpack()JFrame

  4. 学习如何使用数组和集合等List,这将极大地改进你的代码并减少其中的重复代码行,使其更易于阅读和理解。

  5. 与你的变量命名保持一致,Java 命名约定规定你的变量应该写成camelCase,不带下划线。

    • FirstWordUpperCaseClass
    • firstWordLowerCaseVariable
    • firstWordLowerCaseMethod()
    • ALL_WORDS_UPPER_CASED_CONSTANT
  6. 与其拥有所有问题和答案以及哪个答案是 3 个不同数组中正确的一个,不如将它们移动到 Model 类中,并根据每个问题的需要创建它的 N 个实例(同样,列表和数组在这里是你的朋友)。

  7. 您提到的第一个屏幕应该是JDialogor JOptionPane,我也会说结果屏幕。

  8. 还要避免使用多个JFrames,它会创建一个可怕的 UX,请参阅The use of multiple JFrames, good/bad practice?(坏的)

有了上述所有建议,这里的 UI 与您的非常相似,我没有添加所有功能,但这应该足以朝着正确的方向迈出一步:

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;
    }
}

还有一些程序的 SS,请注意我没有包括格式,因为我把那部分留给你,试图在这里保持简单。

在此处输入图像描述 在此处输入图像描述 在此处输入图像描述 在此处输入图像描述


推荐阅读