Array of checkboxes in java

后端 未结 2 1853
无人共我
无人共我 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:05

    You can do something like this:

        List<Checkbox> checkboxes = new ArrayList<Checkbox>();
        String labels[] = {"A", "B", "C", "D", "E", "F"};
        for (int i = 0; i < labels.length; i++) {
            Checkbox checkbox = new Checkbox(labels[i]);
            checkboxes.add(checkbox); //for further use you add it to the list
        }
    

    And then the List use it in your ActionListener to reference the checkboxes. You can also use an array if you like.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题