Mögliche Duplizieren:
java - How would I dynamically add swing component to gui on click?Java Schaltflächen hinzufügen dynamisch als Array
Ich möchte dynamisch Array von Schaltflächen hinzuzufügen. Ich habe versucht, dies wie:
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);
}
}
aber nichts bekommen, was Fehler habe ich machen?
bearbeiten
Ich habe JFrame Klasse „Auswahl“ an i einen Knopf haben, wenn ich die Taste drücken, wird dies geschehen soll:
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();
}
dann gehen Sie auf die Spielklasse i (auch JFrame Klasse) an die Funktion setSetting und es ist so:
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;
}
Was sind 'Setting.length' und' width'? – irrelephant
@irrelephant setting.length returns Nummer 8 –