我正在使用NetBeans为我做一个小程序。我不需要在JPanel上创建很多JButton但我无法做到。 JPanel是由JFrame上的NetBeans编辑器生成的(也是由NetBeans编辑器创建的)
这是代码:
public static ArrayList<Account> accounts = Account.accounts;
public verCuenta() {
initComponents();
panel.setVisible(true);
Account ac;
JButton button;
int size= accounts.size();
for(int i=0;i<size;i++){
button = new JButton(accounts.get(i).getName());
button.setVisible(true);
button.addActionListener(null);
button.setPreferredSize(new Dimension(50,30));
panel.add(button);
}
答案 0 :(得分:1)
我设法让一个按钮弹出, 也许首先从这开始,然后尝试获得几个,然后让你的循环工作
// Create a new button:
JButton b1 = new JButton("ok");
// Set the location and size of the button:
b1.setSize(100, 26);
// Add the button to the window:
jPanel1.add(b1);
//Repaint the Panel to make visible
jPanel1.repaint();
只有在设置尺寸时才能使用,只需在Netbeans中查看。 您也可以使用
指定位置b1.setLocation(100, 100);
顺便说一下,整个框架都是格子袋布局
答案 1 :(得分:-1)
您需要将所有元素添加到面板,然后调用setVisible
方法。
调用setVisible
方法后的添加本身并不考虑。