Java动态添加按钮作为数组

可能重复:
java – 如何在点击时动态添加swing组件给gui?

我想动态添加按钮数组。 我试过这样的:

this.pack(); Panel panel = new Panel(); panel.setLayout(new FlowLayout()); this.add(panel); panel.setVisible(true); for (int i = 0; i < Setting.length; i++) { for (int j = 0; j < Setting.width; j++) { JButton b = new JButton(i+""); b.setSize(30, 30); b.setLocation(i * 30, j * 30); panel.add(b); b.setVisible(true); } } 

但没有得到任何东西,我犯了什么错误?

编辑

我有jFrame类“选择”,我有一个按钮,当我按下按钮时,这应该发生:

 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: structure.Setting s = new Setting(8, 8, 3, 1, 1); Game g = new Game(); g.setSetting(s); this.dispose(); g.show(); } 

然后我去了函数setSetting的Game类(也是jFrame类),它是这样的:

 void setSetting(Setting s) { this.setting = s; structure.Game game = new structure.Game(setting); JPanel panel = new JPanel(new GridLayout(5, 5, 4, 4)); for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 5; j++) { JButton b = new JButton(String.valueOf(i)); panel.add(b); } } add(panel); pack(); setVisible(true); } structure.Setting setting; } 

您可以使用GridLayout添加相同的高度/宽度按钮:

在此处输入图像描述

 public class Game extends JFrame { private JPanel panel; public Game(int rows,int cols,int hgap,int vgap){ panel=new JPanel(new GridLayout(rows, cols, hgap, vgap)); for(int i=1;i<=rows;i++) { for(int j=1;j<=cols;j++) { JButton btn=new JButton(String.valueOf(i)); panel.add(btn); } } add(panel); pack(); setVisible(true); } } 

按钮处理程序中的代码应该是:

 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { Game g = new Game(5,5,3,3); } 

请注意,您还可以通过Game构造函数传递Setting对象引用(当您可以动态添加小部件时),而不是调用setSetting方法。

JPanel已经在布局管理器的控制之下,设置按钮的大小和位置是无关紧要的,因为一旦validation了面板,它们就会改变。

相反,尝试使用按钮填充面板后添加面板。

用例子更新

没有进一步的证据,我们只是猜测……你现在有两个没有问题的人。

在此处输入图像描述

 Panel panel = new Panel(); for (int i = 0; i < Setting.length; i++) { for (int j = 0; j < Setting.width; j++) { jButton b = new jButton(i + ""); panel.add(b); } } this.add(panel); public class BadBoy { public static void main(String[] args) { new BadBoy(); } public BadBoy() { EventQueue.invokeLater(new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { } JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(new BorderLayout()); int buttonCount = (int) Math.round(Math.random() * 20); int columnCount = (int) Math.round(Math.random() * 20); JPanel buttonPane = new JPanel(new GridLayout(0, columnCount)); for (int i = 0; i < buttonCount; i++) { JButton b = new JButton(i + ""); buttonPane.add(b); } frame.add(buttonPane); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); } public class ButtonPane extends JPanel { public ButtonPane() { } } } 

我猜this扩展或是某种框架?

第一步是通过执行this.pack();来打包框架this.pack(); 设置框架大小ti恰好适合所有对象。

我假设你把它设置为可见?

现在你应该能够看到按钮了。 如果你想要一个不同的布局使用panel.setLayout(new SomeLayout);

 Try to use setBounds(x,y,width,heigth) method 

setBound方法

 for (int i = 0; i < 1; i++) { for (int j = 0; j <1; j++) { JButton b = new JButton("button"); b.setBounds(500, 500, 100, 20); this.add(b); } } this.repaint(); this.validate()