Array of checkboxes in java

后端 未结 2 1855
无人共我
无人共我 2021-01-21 04:29

I would like to have 6 checkboxes, and do some stuff after a botton is pressed do you have an example?

Also can an array of checkboxes be used?

I want to avoid:<

2条回答
  •  佛祖请我去吃肉
    2021-01-21 05:09

    Yes, have an array of JCheckBox, example:

    JCheckBox[] checkBoxes = {new JCheckBox("1"), new JCheckBox("2"), new JCheckBox("3"), new JCheckBox("4"), new JCheckBox("5"), new JCheckBox("6")};
    

    or

    JCheckBox[] checkBoxes = new JCheckBox[6];
    

    Then you will have to iterate through checkBoxes.length and instantiate it (if you didn't) and add your listener through addItemListener() and finally adding each checkbox to your JFrame.

    I hope this helps.

提交回复
热议问题