java 如何取消选中复选框?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15812904/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 20:56:32  来源:igfitidea点击:

how to deselect a checkbox?

javaswingjcheckbox

提问by Jhi

I want to deselect a checkbox when i click the Clear button. the below code doesn't work.

当我单击“清除”按钮时,我想取消选择一个复选框。下面的代码不起作用。

        jCheckBox1.setSelected(false);

        jCheckBox2.setSelected(false);

        jCheckBox3.setSelected(false);

        jCheckBox4.setSelected(false);

回答by Alya'a Gamal

it worked and have no problem ,

它有效并且没有问题,

For example:

例如:

final JCheckBoxcb1 = new JCheckBox("A");
final JCheckBoxcb2 = new JCheckBox("B");
JPanel panel = new JPanel();
JFrame frame = new JFrame("");
Button boton = new Button("Clear");
panel.add(boton);
panel.add(cb1);
panel.add(cb2);
frame.add(panel);
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setVisible(true);
boton.addActionListener(new ActionListener() {
 public void actionPerformed(ActionEvent e) {
      cb1.setSelected(false);
      cb2.setSelected(false);
     }
  });