Java 创建一个使用数组填充它的 JCheckbox 数组方法

Java creating a JCheckbox array method that uses an array to populate it

我坚持使用 Java 方法来允许在我的代码中重用。我有一个字符串数组,我想分配给我的复选框。我正在毫无问题地浏览这些内容。我需要在另一个函数中重用复选框列表中的数据,所以我认为将数组移动到一个单独的函数是正确的选择。这是我目前所拥有的:

public JCheckBox[] checkBoxGroup(){
        JCheckBox[] checkBoxList = new JCheckBox[numberCheck()];//set length
        for (int i = 0; i < numberCheck(); i++) {
            checkBoxList[i] = MainForm.units[i];
        }
        return checkBoxList;
//in my main form i have this code:
//loop though and assign to the new method checkBoxGroup
for(int i = 0; i < numberCheck(); i++) {
                    checkBoxGroup(units[i]); //Not working

你能帮我把字符串数组“units”的值赋给新方法吗?我有点迷路了。我试图将单位作为数组和字符串解析到方法中,但都不起作用。我相信这很简单,但缺乏知识。

感谢您抽出宝贵的时间,我已经使用这段代码解决了这个问题

public JCheckBox[] checkBoxGroup(){
        JCheckBox[] checkBoxList = new JCheckBox[numberCheck()];//set length
        for (int i = 0; i < numberCheck(); i++) {
            checkBoxList[i] = new JCheckBox(units[i]);
            checkBoxList[i].setBackground(Color.WHITE);
            checkBoxList[0].setText("Select All");
        }
        
        return checkBoxList;
    } ```

Sometimes taking the time to ask a question helps to solve it :)