gpt4 book ai didi

java - 使用循环添加多个单选按钮?

转载 作者:行者123 更新时间:2023-12-02 04:54:23 25 4
gpt4 key购买 nike

假设我们有这样的东西......

JRadioButton jb1 = new JRadioButton();
JRadioButton jb2 = new JRadioButton();
JRadioButton jb3 = new JRadioButton();
JRadioButton jb4 = new JRadioButton();
JRadioButton jb5 = new JRadioButton();
JRadioButton jb6 = new JRadioButton();
JRadioButton jb7 = new JRadioButton();
JRadioButton jb8 = new JRadioButton();
JRadioButton jb9 = new JRadioButton();
JRadioButton jb10 = new JRadioButton();
JRadioButton jb11 = new JRadioButton();
JRadioButton jb12 = new JRadioButton();
JRadioButton jb13 = new JRadioButton();
JRadioButton jb14 = new JRadioButton();
JRadioButton jb15 = new JRadioButton();
JRadioButton jb16 = new JRadioButton();
JRadioButton jb17 = new JRadioButton();
JRadioButton jb18 = new JRadioButton();
JRadioButton jb19 = new JRadioButton();
JRadioButton jb20 = new JRadioButton();

有没有一种方法可以使用循环添加所有这些单选按钮,而不是一次一行,或者另一种更干净的方式,这只是马虎地循环。

应该澄清一点我的意思是,有没有办法编写一个循环来将所有这些按钮添加到面板中?

最佳答案

这应该很简单:

JRadioButton[] buttons = new JRadioButton[20];
for (i = 0; i < buttons.length; i++)
buttons[i] = new JRadioButton();

关于java - 使用循环添加多个单选按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28914186/

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