Java将按钮动态添加为数组 [英] Java add buttons dynamically as an array
问题描述
可能重复:
java - 如何在点击时动态地将swing组件添加到gui?
我想动态添加按钮数组。
我试过这样:
this.pack();
面板小组=新小组();
panel.setLayout(new FlowLayout());
this.add(面板);
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);
}
}
但没有得到任何东西,我犯了什么错make?
编辑
我有jFrame类选择我有一个按钮,当我按下按钮,这应该发生:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt){
// TODO在这里添加你的处理代码:
structure.Setting s = new Setting(8,8,3,1,1);
游戏g =新游戏();
g.setSetting(s);
this.dispose();
g.show();
}
然后我去Game类(也是jFrame类)到函数setSetting它是这样的:
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(面板);
pack();
setVisible(true);
}
structure.Setting setting;
}
您可以使用 GridLayout 添加相同的高度/宽度按钮:
公共类游戏扩展了JFrame {
私有JPanel面板;
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(面板);
pack();
setVisible(true);
}
}
按钮处理程序中的代码应为:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt){
Game g = new Game(5,5,3) ,3);
}
请注意,您也可以传递设置
对象引用通过 Game
构造函数(当你可以动态添加小部件时)而不是调用 setSetting
方法。 / p>
Possible Duplicate:
java - How would I dynamically add swing component to gui on click?
I want to add array of buttons dynamically. I tried like this:
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);
}
}
but didn't get anything , what mistake did I make?
Edit
I have jFrame class "choices" on it i have a button , when I press the button, this is supposed to happen:
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();
}
then i go to the Game class (also jFrame class) to the function setSetting and it is like this:
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;
}
You may use GridLayout to add equal height/width buttons:
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);
}
}
and code in button's handler should be:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
Game g = new Game(5,5,3,3);
}
Note that you can also pass Setting
object reference via Game
constructor (when you may add widgets dynamically) instead of calling setSetting
method.
这篇关于Java将按钮动态添加为数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!