gpt4 book ai didi

java - 创建 boolean 值以在选择正确的 JRadioButton 时显示 true

转载 作者:太空宇宙 更新时间:2023-11-04 06:14:33 24 4
gpt4 key购买 nike

上一个相关问题:Check which jRadioButtons have been selected if they have been created in a loop

我现在想创建一个 boolean 值,当从每一行中选择所有正确的 JRadioButtons 时,该 boolean 值将为 true。

澄清一下,只有当选择了所有单选按钮(每一行中的一个)时,这才是正确的。

下面的代码仅在选择正确的 JRadioButton 时才打印行和 btn 的一行,但我不确定现在如何将其实现为我正在寻找的内容。提前致谢

//loop for making flow layout for each line of random letters
//counter for having number of the row next to each row in order of selection
int counter = x;
for(int i = 0; i < x; i++)
{
//new jpanel created for every row needed for the word
JPanel jpLine = new JPanel(new FlowLayout());

//new jlabel made with counter number for each row
JLabel count = new JLabel(Integer.toString(counter));
jpLine.add(count);
counter--;
//random number from 0-5 generated for each row
Random number = new Random();
int low = 0;
int high = 5;
int ranNumber = number.nextInt((high - low) + low);

//buttongroup outside loop so only one button can be pressed for each row
ButtonGroup bg = new ButtonGroup();

//get selected button's index in any group with bg.getSelection().getActionCommand()
final int row = i;
final ButtonGroup btnG = bg;
ActionListener listener = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{

//correct.setText("correct");

System.out.println("row " + row);
System.out.println("btn " + btnG.getSelection().getActionCommand());



}
};

//loop for making the number of letters in each row - always going to be 6 letters to choose from in each row
for(int j = 0; j < 5; j++)
{
//if the random number generated for each row equals the loop
//then new radiobutton will be created for the ith letter of the reversed
//answer, starting from charAt 0 and going to the last character
if(ranNumber == j)
{
JRadioButton answerLetter = new JRadioButton("<html><font color = 'white'>" + answerForGrid.charAt(i) + "</font></html>");

bg.add(answerLetter);
answerLetter.setBackground(Color.decode("#566771"));
answerLetter.setOpaque(true);
jpLine.add(answerLetter);

//use setActionCommand("" + j) on each button to associate each button with its index
answerLetter.setActionCommand("" + j);
answerLetter.addActionListener(listener);
}

//ranLetter is generated randomly from the alphabet string, so random letters are
//created for each jradiobutton
final String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
final int N = alphabet.length();

Random letter = new Random();
char ranLetter;

while(true)
{
ranLetter = alphabet.charAt(letter.nextInt(N));
break;
}

JRadioButton k = new JRadioButton("<html><font color = 'white'>" + ranLetter + "</font></html>");

bg.add(k);
k.setBackground(Color.decode("#566771"));
k.setOpaque(true);
jpLine.add(k);
}
//add each row of letters (jpLine) to this loops jpanel
jpCenterCenter.add(jpLine);
}

最佳答案

使用您的代码,您可以在初始循环之前声明一个 boolean 数组,如下所示:

int counter = x;
boolean[] responses = new boolean[x]
for(int i = 0; i < x; i++)
{
//new jpanel created for every row needed for the word
JPanel jpLine = new JPanel(new FlowLayout());

然后您想要编辑您的actionListener,以在选择时将该行的 boolean 值切换为true。需要注意的是,如果这样做,您需要将监听器添加到其他按钮,以便如果用户选择正确的选项,然后切换到错误的选项, boolean 值将返回为 false。

ActionListener listener = new ActionListener() 
{
public void actionPerformed(ActionEvent e)
{

//correct.setText("correct");

String action = e.getActionCommand();

if (action.equals("Incorrect")) {
reponses[row] = false;
}
else {
responses[row] = true;
System.out.println("row " + row);
System.out.println("btn " + action);
}


}
};

不要忘记将其添加到此处的其他按钮:

JRadioButton k = new JRadioButton("<html><font color = 'white'>" + ranLetter + "</font></html>");
k.setActionCommand("Incorrect");
k.addActionListener(listener);

然后您可以添加一个单独的方法来检查所有响应是否都设置为 true:

private boolean checkAnswers(boolean[] responses) {
for (boolean b : responses) {
if (!b) return false
}
return true;
}

关于java - 创建 boolean 值以在选择正确的 JRadioButton 时显示 true,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28267021/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com