简体   繁体   English

Java中的JCheckBox可以在所有复选框中打勾

[英]JCheckBox in Java to take of tick in all checkbox pressing the button

I want to take off the tick from all checkbox what I have in GUI, when I pressed on button - dynamically. 当我动态按下按钮时,我想从所有复选框中剔除我在GUI中的内容。 Is it possible? 可能吗?

JButton clean = new JButton("Clean");
clean.addActionListener(new MyCleanListener());
buttonBox.add(clean);

public class MyCleanListener implements ActionListener{
  public void actionPerformed(ActionEvent a){
     if(jCheckBox.isSelected())
        c.setSelected(false);
  }
}

Thank everyone for your help. 感谢大家的帮助。

public class MyCleanListener implements ActionListener{ public void actionPerformed(ActionEvent a){ for (int i = 0; i < 256; i++){ c = checkboxList.get(i); c.setSelected(false); } } }

here my decision. 这是我的决定。

记住您需要在某些列表或其他数据结构中取消选中的所有复选框,然后可以在干净的侦听器的操作执行方法中对其进行迭代,并取消全部选中...

    panel.add(box1);
    panel.add(box2);
    panel.add(clean);

    clean.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            Component[] components = panel.getComponents();
            for (Component component : components) {
                if (component instanceof  JCheckBox) {
                    JCheckBox checkBox = (JCheckBox) component;
                    if(checkBox.isSelected()) {                     
                    checkBox.setSelected(false);
                    }
                }
            }
        }
    });

Get all JCheckBox components from the panel and remove selection. 从面板中获取所有JCheckBox组件并删除选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM