Java 如果在循环中创建了JradioButton,请检查已选择哪些JradioButton
我有一个创建行的循环,每行有另一个循环来创建6个jradiobutton 每行只能选择一个JRadioButton(应该是这样),但是我发现很难使用操作侦听器来检查是否从每行选择了正确的JRadioButton,然后将空JLabel更改为显示“correct”,但我认为在管理完第一部分后,这将很简单 这是我正在使用的相关代码:Java 如果在循环中创建了JradioButton,请检查已选择哪些JradioButton,java,swing,actionlistener,jradiobutton,Java,Swing,Actionlistener,Jradiobutton,我有一个创建行的循环,每行有另一个循环来创建6个jradiobutton 每行只能选择一个JRadioButton(应该是这样),但是我发现很难使用操作侦听器来检查是否从每行选择了正确的JRadioButton,然后将空JLabel更改为显示“correct”,但我认为在管理完第一部分后,这将很简单 这是我正在使用的相关代码: //loop for making flow layout for each line of random letters //counter for h
//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)
{
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);
}
//用于为每行随机字母创建流布局的循环
//计数器,用于按选择顺序在每行旁边显示行数
int计数器=x;
对于(int i=0;i
此外,我希望检查的正确jradiobutton是在if(ranNumber==j)部分中创建的
抱歉,如果我拖了很久,但我发现这很难解释,所以希望有人能帮上忙 您可以在每个按钮上使用
setActionCommand(“+j)
将每个按钮与其索引相关联,然后使用bg.getSelection().getActionCommand()
在任何组中获取所选按钮的索引。然后,您可以将单个ActionListener分配给一个组中的所有按钮,并确保ActionListener知道它连接到哪一行
// in your "i" loop
final int row = i; // must be final to be used inside the anonymous class
final ButtonGroup btnG = bg;
final String answerAction = "" + ranNumber; // hard to tell if this is what you mean
ActionListener listener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("row " + row);
String selectedAction = btnG.getSelection().getActionCommand();
System.out.println("btn " + selection);
boolean isCorrectForThisRow = (answerAction == selectedAction);
System.out.println("is correct: " + isCorrectForThisRow);
}
};
// in your "j" loop, with either the answerLetter or k buttons
// as required
k.setActionCommand("" + j);
k.addActionListener(listener);
就目前而言,您的示例中有很多地方不太合理,因此很难提出更多建议(例如,一个
while(true)
循环在第一次迭代时中断)。但是,setActionCommand
正是以这种方式使用()来确定在按钮组中选中了哪个按钮。谢谢,这很清楚,不过还有一个问题,因为我对ActionListeners还是很陌生,当在ActionPerformed部分中选择了所有6个正确的JradioButton时,如何创建一个布尔值?它不必是布尔值,但这就是我的思维过程带我去的地方。@AEkon。我会说这是一个后续问题,所以我建议您更新您的代码,然后。这里的问题是。