Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.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 在JButton中显示if语句setText_Java_Eclipse_If Statement_Jbutton_Settext - Fatal编程技术网

Java 在JButton中显示if语句setText

Java 在JButton中显示if语句setText,java,eclipse,if-statement,jbutton,settext,Java,Eclipse,If Statement,Jbutton,Settext,我正在创建一个游戏,并尝试使用setText方法在JButton中显示答案。这就是我试图用按钮做的 enter code here btnAnswer3 = new JButton(setQuestion); 这就是我正在尝试使用的方法 enter code here public void setQuestion() { if (textAreaQuestion.equals("Which Prime Minister of England was from Huddersfield

我正在创建一个游戏,并尝试使用setText方法在JButton中显示答案。这就是我试图用按钮做的

enter code here btnAnswer3 = new JButton(setQuestion);
这就是我正在尝试使用的方法

enter code here public void setQuestion()
{
    if (textAreaQuestion.equals("Which Prime Minister of England was from Huddersfield?")){
        btnAnswer3.setText("C. Harold Wilson");
    }   else{
        if (textAreaQuestion.equals("Where did Bruce Lee open his first Martial Arts School?")){
            btnAnswer3.setText("C. Seattle");
        } else{
            if (textAreaQuestion.equals("Who was the Prime Minister of England in 1940?")){
                btnAnswer3.setText("C. Winston Churchill");
            }
        }
    }
}
有人能告诉我为什么这是不工作,以及它应该如何使用我的代码来完成

下面是文本区域问题的代码

enter code here textAreaQuestion = new JTextArea();
    textAreaQuestion.setEditable(false);
    questions.setViewportView(textAreaQuestion);
这是另一个类中问题的代码

enter code here private ArrayList<QuestionDetails> Questions = new ArrayList<QuestionDetails>();





public Questions()
{
    Questions.add(new QuestionDetails("Which Prime Minister of England was from Huddersfield?","Winston Churchill","Tony Blair","Harold Macmillon"));
    Questions.add(new QuestionDetails("Who was the Prime Minister of England in 1940?","John Kennedy","Harold Wilson","Harold Macmillon")); 
    Questions.add(new QuestionDetails("Where did Bruce Lee open his first Martial Arts School?","Baltimore","Hong Kong","Hollywood"));
}


public QuestionDetails generateResponse()
{
    Random r = new Random();
    int index = r.nextInt(Questions.size());
    return Questions.get(index);
}
这是GUI类中问题代码的下一部分

enter code here displayQuestion();
    displayAnswer1();
    displayAnswer2();
    displayAnswer4();

    //This code will display the question and answers
}
enter code here public void displayQuestion()
{
    QuestionDetails q = questHandler.generateResponse();
    String question = q.getQuestion();
    textAreaQuestion.setText(question);
    //This will display the array of questions

}

关于,

如果textAreaQuestion是
JTextArea
您必须执行
textAreaQuestion.getText()

因此:


它不起作用的原因是因为答案使用了数组。在这种情况下,setText方法将没有任何用处

Questions.add(new QuestionDetails("Which Prime Minister of England was from Huddersfield?","Winston Churchill","Tony Blair","Harold Macmillon"));
Questions.add(new QuestionDetails("Who was the Prime Minister of England in 1940?","John Kennedy","Harold Wilson","Harold Macmillon")); 
Questions.add(new QuestionDetails("Where did Bruce Lee open his first Martial Arts School?","Baltimore","Hong Kong","Hollywood"));}

public QuestionDetails generateResponse(){   Random r = new Random();
int index = r.nextInt(Questions.size());
return Questions.get(index);}
要解决这个问题,您需要像这样从数组中删除答案

enter code here Questions.add(new QuestionDetails("Which Prime Minister of England was from Huddersfield?"));
    Questions.add(new QuestionDetails("Who was the Prime Minister of England in 1940?")); 
    Questions.add(new QuestionDetails("Where did Bruce Lee open his first Martial Arts School?")); }
那么这个方法就会有效地工作

enter code here if (textAreaQuestion.getText().equals("Which Prime Minister of England was from Huddersfield?")){
                btnAnswer3.setText("C."+" Harold Wilson");
            }
                if (textAreaQuestion.getText().equals("Which Prime Minister of England was from Huddersfield?")){
                    btnAnswer1.setText("A."+" Tony Blair");
                }
                    if (textAreaQuestion.getText().equals("Which Prime Minister of England was from Huddersfield?")){
                        btnAnswer2.setText("B."+" Harold Wilson");
                    }
                        if (textAreaQuestion.getText().equals("Which Prime Minister of England was from Huddersfield?")){
                            btnAnswer4.setText("D."+" Harold Wilson");
                        }   else{
                    }

textAreaQuestion是字符串吗?
textAreaQuestion
的类型是什么?它被声明为私有JTextArea textAreaQuestion;不,对不起。它只留下空按钮,您可以编辑您的问题并将代码张贴在您正在设置的
textAreaQuestion
enter code here if (textAreaQuestion.getText().equals("Which Prime Minister of England was from Huddersfield?")){
                btnAnswer3.setText("C."+" Harold Wilson");
            }
                if (textAreaQuestion.getText().equals("Which Prime Minister of England was from Huddersfield?")){
                    btnAnswer1.setText("A."+" Tony Blair");
                }
                    if (textAreaQuestion.getText().equals("Which Prime Minister of England was from Huddersfield?")){
                        btnAnswer2.setText("B."+" Harold Wilson");
                    }
                        if (textAreaQuestion.getText().equals("Which Prime Minister of England was from Huddersfield?")){
                            btnAnswer4.setText("D."+" Harold Wilson");
                        }   else{
                    }