I want to create 100 JToggleButtons and do it with a for loop and save them in a link list. Then show them in grid bag layout.

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);
    }

I tried something like that, but i can't.

有帮助吗?

解决方案

You're not adding the created button to the content pane. And you create a constraint named gbc_, but never use it. Change the code to

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);
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top