gpt4 book ai didi

java - 使用 For 创建 JToggleButtons

转载 作者:行者123 更新时间:2023-12-01 15:40:45 25 4
gpt4 key购买 nike

我想创建 100 个 JToggleButtons 并使用 for 循环来完成并将它们保存在链接列表中。然后以网格袋布局显示它们。

ArrayList<JToggleButton> buttons = new ArrayList<JToggleButton>(); 
for(int i=0; i<100; i++){
buttons.add(new JToggleButton(""));// = new JToggleButton("");
GridBagConstraints gbc_ = new GridBagConstraints();
gbc_tglbtnNewToggleButton.fill = GridBagConstraints.VERTICAL;
gbc_tglbtnNewToggleButton.insets = new Insets(0, 0, 5, 5);
gbc_tglbtnNewToggleButton.gridx = i;
gbc_tglbtnNewToggleButton.gridy = j;
frame.getContentPane().add(tglbtnNewToggleButton, gbc_tglbtnNewToggleButton);
}

我尝试过类似的方法,但我做不到。

最佳答案

您没有将创建的按钮添加到内容 Pane 中。您创建了一个名为 gbc_ 的约束,但从不使用它。将代码更改为

gbc_tglbtnNewToggleButton.fill = GridBagConstraints.VERTICAL;
gbc_tglbtnNewToggleButton.insets = new Insets(0, 0, 5, 5);
gbc_tglbtnNewToggleButton.gridy = j;
for (int i = 0; i < 100; i++) {
JToggleButton button = new JToggleButton("");
buttons.add(button);
gbc_tglbtnNewToggleButton.gridx = i;
buttons.add(button, gbc_tglbtnNewToggleButton);
}

关于java - 使用 For 创建 JToggleButtons,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8071846/

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