对于我的CS项目,我正在做一个多选题测验。每个测验都有一个问题和四个可能的答案。正确的答案被保存为一个字符串。所有错误的答案都保存在一个字符串数组中。我想为每个人制作一个按钮。但我不希望正确的答案始终处于相同的位置,所以我想随机放置它。在我随机放置它之后,我不知道如何为字符串数组制作按钮。帮帮我!在Java中,如何使数组中的每个元素成为一个按钮?
` 公共显示器(){
answer1 = new JButton("1");
answer2 = new JButton("2");
answer3 = new JButton("3");
answer4 = new JButton("");
question = new JLabel ("question?");
}
public Display(String question1, String [] answers, String correct, String pictureName){
//create a panel to hold buttons
SimplePicture background = new SimplePicture(pictureName);
JLabel picture = background.getJLabel();
question = new JLabel(question1);
//assign answers to buttons
//generate a random number to determine where correct goes
int index = (int)(Math.random()*4);
//place correct answer in a certain button
if (index == 0){
answer1 = new JButton(correct);
}
else if (index == 1){
answer2 = new JButton(correct);
}
else if (index == 2){
answer3 = new JButton(correct);
}
else if (index == 3){
answer4 = new JButton(correct);
}
//fill other spots with answers
for (int i=0; i < answers.length; i++){
this is where I need help
}
}`
也许某种形式的视觉会有帮助。我不理解你想要的结果。 –