gpt4 book ai didi

java - 获取Swing中复选框的数量

转载 作者:行者123 更新时间:2023-11-29 06:56:03 25 4
gpt4 key购买 nike

我有一个带有大约 50 个复选框的 swing,下面是 3 个的示例代码。

JCheckBox checkboxOne = new JCheckBox("One");
JCheckBox checkboxTwo = new JCheckBox("Two");
JCheckBox checkboxThree = new JCheckBox("Three");


// add these check boxes to the container...

// add an action listener
ActionListener actionListener = new ActionHandler();
checkboxOne.addActionListener(actionListener);
checkboxTwo.addActionListener(actionListener);
checkboxThree.addActionListener(actionListener);
 
// code of the action listener class
 
class ActionHandler implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent event) {
        JCheckBox checkbox = (JCheckBox) event.getSource();
        if (checkbox == checkboxOne) {
            System.out.println("Checkbox #1 is clicked");
        } else if (checkbox == checkboxTwo) {
            System.out.println("Checkbox #2 is clicked");
        } else if (checkbox == checkboxThree) {
            System.out.println("Checkbox #3 is clicked");
        }
    }
}

在这里,我想遍历 50 个复选框,例如创建可用复选框的 ArrayList,并循环检查哪些复选框被选中。我无法理解如何创建复选框的 ArrayList。

我提到了 Array of checkboxes in java ,但我无法理解如何使用它?

请让我知道如何做到这一点。

最佳答案

创建JCheckBoxArrayList 并按顺序添加它们。然后,您可以使用 indexOf() 函数来检索数字,如下所示:

public class TestFrame extends JFrame {

public TestFrame() {
setLayout(new GridLayout());
setSize(500, 500);

JCheckBox checkboxOne = new JCheckBox("One");
JCheckBox checkboxTwo = new JCheckBox("Two");
JCheckBox checkboxThree = new JCheckBox("Three");

final ArrayList<JCheckBox> checkBoxes = new ArrayList<>();

add(checkboxOne);
add(checkboxTwo);
add(checkboxThree);

checkBoxes.add(checkboxOne);
checkBoxes.add(checkboxTwo);
checkBoxes.add(checkboxThree);

ActionListener actionListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
JCheckBox checkbox = (JCheckBox) event.getSource();
int index = checkBoxes.indexOf(checkbox) + 1;
System.out.println("Checkbox #" + index + " is clicked");
}
};
checkboxOne.addActionListener(actionListener);
checkboxTwo.addActionListener(actionListener);
checkboxThree.addActionListener(actionListener);
}

public static void main(String[] args) {
TestFrame frame = new TestFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}

请注意,这是对您的代码的改编。这个示例尽可能接近您的代码,因此存在的唯一修改应该反射(reflect)我试图传达的观点。

编辑

由于您修改了问题并提出了一个新问题,这里是答案的第二部分:

and in my action listener, I was trying to get the checked boxes values, but it is throwing null as name and though I've checked, the output shows as not selected.

修改您的代码以使用 getText() 而不是 getName(),例如:

JButton btnNewButton = new JButton("New button");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
System.out.println(checkBoxes.size());
for (int i = 0; i < checkBoxes.size(); i++) {
if (checkBoxes.get(i).isSelected()) {
System.out.println(" Checkbox " + i + " and " + checkBoxes.get(i).getText() + " is selected");
} else {
System.out.println(
" Checkbox " + i + " and " + checkBoxes.get(i).getText() + " is noooooot selected");
}
}
}

});

关于java - 获取Swing中复选框的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33940111/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com