检查是否已在循环中创建了哪些jRadioButton [英] Check which jRadioButtons have been selected if they have been created in a loop

查看:71
本文介绍了检查是否已在循环中创建了哪些jRadioButton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个创建行的循环,其中每一行都有另一个循环来创建6个JRadioButton。

I have a loop that creates rows, where each row has another loop to create 6 JRadioButtons.

每行只能选择一个JRadioButton(因为它应该是),但是我发现很难使用动作侦听器来检查是否已从每行中选择了正确的JRadioButton,然后更改一个空的JLabel以显示正确,但是我认为一旦完成管理第一部分。

Only one JRadioButton can be selected from each row (as it should be), but I'm finding it difficult to use Action Listeners to check whether the correct JRadioButton has been selected from each row, and then change an empty JLabel to show "correct", but I think that will be simple once I've managed the first part.

这是我正在使用的相关代码:

This is the relevant code I'm working with:

    //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);
    }

此外,我要检查的正确JRadioButton是那些是在if(ranNumber == j)部分中创建的。

Also, the correct JRadioButtons I'm looking to be checked are the ones that are created in the if(ranNumber == j) section.

很抱歉,如果我一直拖拖拉拉,但我发现这很难解释,因此希望有人会能够帮助!

Sorry if I've dragged on but I've found this difficult to explain, so hopefully someone will be able to help!

推荐答案

您可以在每个上使用 setActionCommand( + j)按钮,将每个按钮与其索引相关联,然后使用 bg.getSelection()。getActionCommand()获取任意组中所选按钮的索引。然后,您可以将一个ActionListener分配给一个组中的所有按钮,并确保操作侦听器知道它附加到了哪一行。

You could use setActionCommand("" + j) on each button to associate each button with its index, and then get the selected button's index in any group with bg.getSelection().getActionCommand(). You can then assign a single ActionListener to all the buttons in one group, and make sure the action listener knows which row it's attached to.

// 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 此处的Oracle教程),以这种方式找出在按钮组中选中了哪个按钮。

As it stands, there is a lot to your sample that doesn't quite make sense, so it's hard to suggest more than this (for example - a while(true) loop that breaks on it's first iteration). But setActionCommand is meant to be used (oracle tutorial here) in exactly this way to find out which button is checked in a button group.

这篇关于检查是否已在循环中创建了哪些jRadioButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆