我需要在计算要添加的按钮数量时添加按钮。如何在程序运行时添加按钮
我有我的按钮创建代码hereish ..:
private void loadButtons()
{
if (active_puzzle != null)
{
int devider = 5;
int count = 0;
JButton puzzleButton[] = new JButton[active_puzzle.getNumberOfPieces()];
for(int row = 0; row < active_puzzle.getRows(); row++)
{
for(int column = 0; column < active_puzzle.getColumns(); column++)
{
puzzleButton[count] = new JButton(new ImageIcon(active_puzzle.getPieces()[count].getPieceImage()));
}
}
}
}
现在怎么我告诉大家,新的按钮需要添加到屏幕上的程序?
谢谢
对代码块使用一致的逻辑缩进。代码的缩进旨在帮助人们理解程序流程。有关动态添加组件的提示,请参阅[嵌套布局示例](http://stackoverflow.com/a/5630271/418556)。它在按钮点击时动态添加标签。 –
你能提供完整的课程代码吗?你必须先将它们添加到JFrame,Panel或类似的东西,然后在添加按钮之后刷新UI,以便它们将被displayd。 – JBA