簡體   English   中英

檢查是否已在循環中創建了哪些jRadioButton

[英]Check which jRadioButtons have been selected if they have been created in a loop

我有一個創建行的循環,其中每一行都有另一個循環來創建6個JRadioButton。

每行只能選擇一個JRadioButton(應該如此),但是我發現很難使用動作偵聽器來檢查是否已從每一行中選擇了正確的JRadioButton,然后將空的JLabel更改為顯示“正確” ”,但我認為,一旦我完成了第一部分,這將很簡單。

這是我正在使用的相關代碼:

    //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)部分中創建的那些。

抱歉,如果我繼續這樣做,但是我發現這很難解釋,因此希望有人能夠提供幫助!

您可以在每個按鈕上使用setActionCommand("" + j)來將每個按鈕與其索引相關聯,然后使用bg.getSelection().getActionCommand()獲得任何組中所選按鈕的索引。 然后,您可以將一個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正是以這種方式使用的(請參見oracle教程 ),以找出在按鈕組中選中了哪個按鈕。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM