Error: The type of the expression must be an array type but it resolved to boolean

Error: The type of the expression must be an array type but it resolved to boolean

我有 6 个按钮和 6 个布尔值,Actionhandler 应该为它的按钮设置布尔值,如果单击它则为 true。为了方便起见,我制作了一个 boolean[] 数组,但是 setter 函数给出了错误:

"The type of the expression must be an array type but it resolved to boolean" ;

ActionListener中的setter需要设置什么?

我试过:


        public class Gui {

    static JButton ko[] = new JButton[6];

    public Gui() {

        int y=0;
        for (int i = 0; i < ko.length; i++) {
            ko[i] = new JButton();
            ko[i].addActionListener(new ActionHandler());
            ko[i].setBorder(bor);
            if(i==0||i==2||i==4) {
                ko[i].setBounds(650, 200+60*y, 250, 30);
            }else {
                ko[i].setBounds(950, 200+60*y, 250, 30);
                y++;
            }
            jfMenu.add(ko[i]);

        }
    }

    public static JButton[] getKo() {
        return ko;
    }

    public static void setKo(JButton[] ko) {
        Gui.ko = ko;
    }


}

public void actionPerformed(ActionEvent e) {

    for (int i = 0; i < Var.getKo().length; i++) {
        if(e.getSource() == Var.getKo()) {
            Var.setKo(true[i]);
        }else {
            Var.getKo()[i] = false;
        }
    }

}

public class Var {

    static boolean ko[] = new boolean[6];

    public Var() {

        for (int i = 0; i < ko.length; i++) {
            ko[i] = false;
        }

    }
    public static boolean[] getKo() {
        return ko;
    }

    public static void setKo(boolean[] ko) {
        Var.ko = ko;
    }
}


这里不用setter,你的数组已经存在,已经初始化,不用再设置了。

您需要的是更改其某些索引的值。这可以通过使用 getter 来完成,以获取对数组的引用,然后 select 正确的索引并为其赋值。

点赞

public void actionPerformed(ActionEvent e) {
    for (int i = 0; i < Var.getKo().length; i++) {
        Var.getKo()[i] = e.getSource() == Var.getKo();
    }
}