Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java中的JCheckBox在所有复选框中打勾,按下按钮_Java_Swing_Actionlistener_Jcheckbox - Fatal编程技术网

Java中的JCheckBox在所有复选框中打勾,按下按钮

Java中的JCheckBox在所有复选框中打勾,按下按钮,java,swing,actionlistener,jcheckbox,Java,Swing,Actionlistener,Jcheckbox,当我动态地按下按钮时,我想去掉GUI中所有复选框的勾号。可能吗 JButton clean = new JButton("Clean"); clean.addActionListener(new MyCleanListener()); buttonBox.add(clean); public class MyCleanListener implements ActionListener{ public void actionPerformed(ActionEvent a){ if

当我动态地按下按钮时,我想去掉GUI中所有复选框的勾号。可能吗

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);
  }
}
谢谢大家的帮助。
public class MyCleanListener implements ActionListener{
      public void actionPerformed(ActionEvent a){
        for (int i = 0; i < 256; i++){
          c = checkboxList.get(i);
          c.setSelected(false);
          }
        }
    }
公共类MyCleanListener实现ActionListener{ 已执行的公共无效操作(操作事件a){ 对于(int i=0;i<256;i++){ c=复选框列表。获取(i); c、 已选择(假); } } }


这里是我的决定。

记住所有需要在列表或其他数据结构中取消勾选的复选框,然后您可以在clean listener的action performed方法中迭代并取消勾选所有复选框

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

从面板中获取所有JCheckBox组件并删除所选内容。

我想取消勾选
-->您的意思是阻止所有鼠标和按键事件进入JCheckBox吗?我的方法是处理类似情况(可能不是最好的,但已经足够好)-将所有要在JPanel中或以其他方式一起处理的复选框分组,然后遍历这个组的成员。顺便说一句:您可以
setSelected(false)无论它的当前状态如何,都没有太多开销。