Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我正在尝试用java构建一个多项选择测试,出于某种原因,我的代码只适用于两个问题,不再适用_Java_Swing - Fatal编程技术网

我正在尝试用java构建一个多项选择测试,出于某种原因,我的代码只适用于两个问题,不再适用

我正在尝试用java构建一个多项选择测试,出于某种原因,我的代码只适用于两个问题,不再适用,java,swing,Java,Swing,这是我测验的代码 import java.util.*; import java.util.List; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Main2 { public class Questions { String Question; String userAns;

这是我测验的代码

import java.util.*;
    import java.util.List;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class Main2 {
        public class Questions {
            String Question;
            String userAns;
            String realAns;
        }
        public static void main(String[] args) {
            JFrame frame = new JFrame ("Screen");
            frame.setSize(2500, 2500);
            frame.setLayout(null);
            frame.setVisible(true);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            JPanel panel = new JPanel();
            frame.getContentPane().add(panel);
            panel.setSize(2500, 2500);
            panel.setLayout(null);  
            //panel.setBackground(Color.red);

            Question[] questions = new Question[2];
            System.out.println(questions.length);
            Question q1 = new Question();
            q1.Question = "1) What is your Name?";
            q1.A ="Bob";
            q1.B="Billy";
            q1.C="Joe";
            q1.D="Jill";
            questions[0] = q1;

            Question q2 = new Question();
            q2.Question = "2) What is your Age?";
            q2.A ="5";
            q2.B="69";
            q2.C="21";
            q2.D="12";
            questions[1] = q2;

            /*
            Question q3 = new Question();
            q3.Question = "3) When Is your Birthday?";
            q3.A = "May";
            q3.B="Jan";
            q3.C="Apr";
            q3.D="Aug";
            questions[2] = q3;
            */
            //When this question is added the code breaks down
            JLabel Question = new JLabel(questions[0].Question);
            Question.setBounds(50, 0, 1500, 50);
            panel.add(Question);
            Question.setFont(new Font(Question.getFont().getName(), Font.PLAIN, 25));
            Question.setVisible(true);

            JLabel incorrectAnswerLabel = new JLabel ("Incorrect Answer! Try Again");
            incorrectAnswerLabel.setBounds(300, 0, 1000, 500);
            panel.add(incorrectAnswerLabel);
            incorrectAnswerLabel.setVisible(false);
            incorrectAnswerLabel.setFont(new Font(incorrectAnswerLabel.getFont().getName(), Font.BOLD, 46));
            incorrectAnswerLabel.setForeground(Color.RED);

            JLabel correctAnswerLabel = new JLabel ("Correct Answer! Good Job");
            correctAnswerLabel.setBounds(300, 0, 1000, 500);
            panel.add(correctAnswerLabel);
            correctAnswerLabel.setVisible(false);
            correctAnswerLabel.setFont(new Font(correctAnswerLabel.getFont().getName(), Font.BOLD, 46));
            correctAnswerLabel.setForeground(Color.GREEN);

            JButton submitButton = new JButton();
            submitButton.setBounds(50, 250, 150, 50);
            submitButton.setText("Submit");
            panel.add(submitButton);
            submitButton.setVisible(true);



            JRadioButton OptionA = new JRadioButton(questions[0].A);
            OptionA.setBounds(50, 50, 100, 50);
            panel.add(OptionA);
            OptionA.setVisible(true);


            JRadioButton OptionB = new JRadioButton(questions[0].B);
            OptionB.setBounds(50, 100, 100, 50);
            panel.add(OptionB);
            OptionB.setVisible(true);

            JRadioButton OptionC = new JRadioButton(questions[0].C);
            OptionC.setBounds(50, 150, 100, 50);
            panel.add(OptionC);
            OptionC.setVisible(true);

            JRadioButton OptionD = new JRadioButton(questions[0].D);
            OptionD.setBounds(50, 200, 100, 50);
            panel.add(OptionD);
            OptionD.setVisible(true);



            ButtonGroup radioGroup = new ButtonGroup();
            radioGroup.add(OptionA);
            radioGroup.add(OptionB);
            radioGroup.add(OptionC);
            radioGroup.add(OptionD);

            for(int i = 0; i < questions.length-1; i++)
            {
                final int count = i;
                submitButton.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e)
                    {
                        if(submitButton.getText().equals("Submit"))
                        {

                            if(OptionA.isSelected()) {
                                submitButton.setText("Next Question");
                                correctAnswerLabel.setVisible(true);

                            }   
                            else 
                                incorrectAnswerLabel.setVisible(true);
                        }
                        else {
                            correctAnswerLabel.setVisible(false);
                            submitButton.setText("Submit");
                            Question q = questions[count+1];
                            Question.setText(q.Question);
                            setAnswers(q.A, q.B, q.C, q.D, OptionA, OptionB, OptionC, OptionD);
                        }
                    }

                });
            }


        }

        static void setAnswers(String A, String B,String C,String D, JRadioButton a, JRadioButton b, JRadioButton c, JRadioButton d)
        {
            List<String> answers = Arrays.asList(A, B,C,D);
            Collections.shuffle(answers);
            a.setText(answers.get(0));
            b.setText(answers.get(1));
            c.setText(answers.get(2));
            d.setText(answers.get(3));
        }

    }
import java.util.*;
导入java.util.List;
导入javax.swing.*;
导入java.awt.*;
导入java.awt.event.*;
公共类Main2{
公开课问题{
字符串问题;
字符串用户;
字符串realAns;
}
公共静态void main(字符串[]args){
JFrame=新JFrame(“屏幕”);
框架。设置尺寸(2500、2500);
frame.setLayout(空);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel面板=新的JPanel();
frame.getContentPane().add(面板);
面板设置尺寸(2500、2500);
panel.setLayout(空);
//面板.立根背景(颜色.红色);
问题[]问题=新问题[2];
System.out.println(问题长度);
问题q1=新问题();
问题1:你叫什么名字;
q1.A=“鲍勃”;
q1.B=“比利”;
q1.C=“乔”;
q1.D=“吉尔”;
问题[0]=q1;
问题q2=新问题();
q2.问题=“2)您的年龄是多少?”;
q2.A=“5”;
q2.B=“69”;
q2.C=“21”;
q2.D=“12”;
问题[1]=问题2;
/*
问题q3=新问题();
问题3.问题=“3)你的生日是什么时候?”;
q3.A=“可能”;
q3.B=“一月”;
q3.C=“四月”;
q3.D=“八月”;
问题[2]=q3;
*/
//添加此问题后,代码将崩溃
JLabel问题=新的JLabel(问题[0]。问题);
问题.挫折(50,0,1500,50);
小组委员会.增补(问题);
Question.setFont(新字体(Question.getFont().getName(),Font.PLAIN,25));
问题.setVisible(true);
JLabel incorrectAnswerLabel=新JLabel(“回答不正确!重试”);
不正确的应答标签。立根(300,0,1000,500);
面板。添加(不正确的应答标签);
不正确的应答标签。设置可见(假);
incorrectAnswerLabel.setFont(新字体(incorrectAnswerLabel.getFont().getName(),Font.BOLD,46));
不正确的回答标签。设置前景(颜色。红色);
JLabel correctAnswerLabel=新JLabel(“回答正确!干得好”);
修正应答标签.立根(300,0,1000,500);
面板。添加(正确应答标签);
correctAnswerLabel.setVisible(假);
setFont(新字体(correctAnswerLabel.getFont().getName(),Font.BOLD,46));
correctAnswerLabel.setForeground(颜色:绿色);
JButton submitButton=新JButton();
submitButton.setBounds(5025015050);
submitButton.setText(“提交”);
面板。添加(提交按钮);
submitButton.setVisible(true);
JRadioButton OptionA=新的JRadioButton(问题[0].A);
选项A.立根(50,50,100,50);
面板。添加(选项a);
选项a.设置可见(真);
JRadioButton OptionB=新的JRadioButton(问题[0].B);
选项B.立根(50、100、100、50);
面板。添加(选项B);
选项b.设置可见(真);
JRadioButton OptionC=新的JRadioButton(问题[0].C);
选择立根(50、150、100、50);
面板。添加(选项C);
OptionC.setVisible(真);
JRadioButton OptionD=新的JRadioButton(问题[0].D);
选择立根(50、200、100、50);
面板。添加(选项D);
OptionD.setVisible(真);
ButtonGroup radioGroup=新建ButtonGroup();
放射组添加(可选A);
添加放射组(选项B);
radioGroup.add(选项C);
放射组添加(可选);
for(int i=0;iaddActionListener(新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e)
{
if(submitButton.getText().equals(“Submit”))
{
if(OptionA.isSelected()){
submitButton.setText(“下一个问题”);
correctAnswerLabel.setVisible(真);
}   
其他的
不正确的answerlabel.setVisible(true);
}
否则{
correctAnswerLabel.setVisible(假);
submitButton.setText(“提交”);
问题q=问题[计数+1];
问题.setText(问题);
设定答案(q.A、q.B、q.C、q.D、选项A、选项B、选项C、选项D);
}
}
});
}
}
静态void setAnswers(字符串A、字符串B、字符串C、字符串D、JRadioButton A、JRadioButton B、JRadioButton C、JRadioButton D)
{
列表答案=数组.asList(A,B,C,D);
集合。洗牌(答案);
a、 setText(answers.get(0));
b、 setText(answers.get(1));
c、 setText(answers.get(2));
d、 setText(answers.get(3));
}
}
我的代码只需要两个问题就可以正常工作。当我在中添加第三个时,它甚至不会显示问题的文本。我试着调试它,看看我的代码是否还在运行。似乎它确实在运行。我工作了几个小时,但白费力气,急需帮助

注:

  • 正确的
    public class Main2 {
    
        public class Questions {
    
            String Question;
            String userAns;
            String realAns;
        }
    
        static int count = 0;
        public static void main(String[] args) {
            JFrame frame = new JFrame("Screen");
            frame.setSize(2500, 2500);
            frame.setLayout(null);
            frame.setVisible(true);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            JPanel panel = new JPanel();
            frame.getContentPane().add(panel);
            panel.setSize(2500, 2500);
            panel.setLayout(null);
            //panel.setBackground(Color.red);
    
            Question[] questions = new Question[3];
            System.out.println(questions.length);
            Question q1 = new Question();
            q1.Question = "1) What is your Name?";
            q1.A = "Bob";
            q1.B = "Billy";
            q1.C = "Joe";
            q1.D = "Jill";
            questions[0] = q1;
    
            Question q2 = new Question();
            q2.Question = "2) What is your Age?";
            q2.A = "5";
            q2.B = "69";
            q2.C = "21";
            q2.D = "12";
            questions[1] = q2;
    
            Question q3 = new Question();
            q3.Question = "3) When Is your Birthday?";
            q3.A = "May";
            q3.B = "Jan";
            q3.C = "Apr";
            q3.D = "Aug";
            questions[2] = q3;
    
            //When this question is added the code breaks down
            JLabel Question = new JLabel(questions[0].Question);
            Question.setBounds(50, 0, 1500, 50);
            panel.add(Question);
            Question.setFont(new Font(Question.getFont().getName(), Font.PLAIN, 25));
            Question.setVisible(true);
    
            JLabel incorrectAnswerLabel = new JLabel("Incorrect Answer! Try Again");
            incorrectAnswerLabel.setBounds(300, 0, 1000, 500);
            panel.add(incorrectAnswerLabel);
            incorrectAnswerLabel.setVisible(false);
            incorrectAnswerLabel.setFont(new Font(incorrectAnswerLabel.getFont().getName(), Font.BOLD, 46));
            incorrectAnswerLabel.setForeground(Color.RED);
    
            JLabel correctAnswerLabel = new JLabel("Correct Answer! Good Job");
            correctAnswerLabel.setBounds(300, 0, 1000, 500);
            panel.add(correctAnswerLabel);
            correctAnswerLabel.setVisible(false);
            correctAnswerLabel.setFont(new Font(correctAnswerLabel.getFont().getName(), Font.BOLD, 46));
            correctAnswerLabel.setForeground(Color.GREEN);
    
            JButton submitButton = new JButton();
            submitButton.setBounds(50, 250, 150, 50);
            submitButton.setText("Submit");
            panel.add(submitButton);
            submitButton.setVisible(true);
    
            JRadioButton OptionA = new JRadioButton(questions[0].A);
            OptionA.setBounds(50, 50, 100, 50);
            panel.add(OptionA);
            OptionA.setVisible(true);
    
            JRadioButton OptionB = new JRadioButton(questions[0].B);
            OptionB.setBounds(50, 100, 100, 50);
            panel.add(OptionB);
            OptionB.setVisible(true);
    
            JRadioButton OptionC = new JRadioButton(questions[0].C);
            OptionC.setBounds(50, 150, 100, 50);
            panel.add(OptionC);
            OptionC.setVisible(true);
    
            JRadioButton OptionD = new JRadioButton(questions[0].D);
            OptionD.setBounds(50, 200, 100, 50);
            panel.add(OptionD);
            OptionD.setVisible(true);
    
            ButtonGroup radioGroup = new ButtonGroup();
            radioGroup.add(OptionA);
            radioGroup.add(OptionB);
            radioGroup.add(OptionC);
            radioGroup.add(OptionD);
    
            submitButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (submitButton.getText().equals("Submit")) {
    
                        if (OptionA.isSelected()) {
                            submitButton.setText("Next Question");
                            correctAnswerLabel.setVisible(true);
    
                        } else {
                            incorrectAnswerLabel.setVisible(true);
                        }
                    } else {
                        correctAnswerLabel.setVisible(false);
                        submitButton.setText("Submit");
    
                        if(count < questions.length-1){
                            count = count+1;
                        }
                        Question q = questions[count];
                        Question.setText(q.Question);
                        setAnswers(q.A, q.B, q.C, q.D, OptionA, OptionB, OptionC, OptionD);
                    }
                }
    
            });
    
        }
    
        static void setAnswers(String A, String B, String C, String D, JRadioButton a, JRadioButton b, JRadioButton c, JRadioButton d) {
            List<String> answers = Arrays.asList(A, B, C, D);
            Collections.shuffle(answers);
            a.setText(answers.get(0));
            b.setText(answers.get(1));
            c.setText(answers.get(2));
            d.setText(answers.get(3));
        }
    
    }