资源下载dynamicaly添加按钮作为一个数组 [英] Java add buttons dynamicaly as an array

查看:155
本文介绍了资源下载dynamicaly添加按钮作为一个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/4279435/java-how-would-i-dynamically-add-swing-component-to-gui-on-click\">java - ?我将如何动态地添加Swing组件到GUI上点击


我要动态地添加按钮的数组。
我想是这样的:

  this.pack();
    面板面板=新面板();
    panel.setLayout(新的FlowLayout());
    this.add(面板);
    panel.setVisible(真);
    的for(int i = 0; I&LT; Setting.length;我++){
        对于(INT J = 0; J&LT; Setting.width; J ++){
            JButton的B =的新的JButton(1 +);
            b.setSize(30,30);
            b.setLocation(我* 30,J * 30);
            panel.add(二);
            b.setVisible(真);
        }
    }

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

修改

我已经JFrame类选择就可以了,我有一个按钮,当我preSS的按钮,这是应该发生:

 私人无效jButton1ActionPerformed(EVT java.awt.event.ActionEvent中){
        // TODO添加处理code在这里:
       structure.Setting S =新的设置(8,8,3,1,1);
       游戏G =新游戏();
       g.setSetting(多个);
       this.dispose();
       g.show();
    }

然后我去Game类(也JFrame类)的功能setSetting,它是这样的:

 无效setSetting(设置S){
        this.setting =秒;
        structure.Game游戏=新structure.Game(设置);
        的JPanel面板=新JPanel(新的GridLayout(5,5,4,4));
        的for(int i = 1; I&LT; = 5;我++){
            对于(INT J = 1; J&LT; = 5; J ++){
                JButton的B =的新的JButton(将String.valueOf(I));
                panel.add(二);
            }
        }
        加(面板);
        包();
        调用setVisible(真);
    }
    structure.Setting设置;
}


解决方案

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

 公共类游戏扩展的JFrame {
    私人的JPanel面板;
    公共游戏(INT行,诠释COLS,参数hgap,诠释vgap){
        面板=新JPanel(新网格布局(行,COLS,hgap指定,vgap));
        的for(int i = 1; I&LT; =行;我++)
        {
            对于(INT J = 1; J&LT; = COLS; J ++)
            {
                JButton的BTN =的新的JButton(将String.valueOf(I));
                panel.add(BTN);
            }
        }
        加(面板);
        包();
        调用setVisible(真);
    }
}

和$ C $在按钮的处理程序C应该:

 私人无效jButton1ActionPerformed(EVT java.awt.event.ActionEvent中){
   游戏G =新游戏(5,5,3,3);
}

请注意,您也可以通过通过游戏设置对象引用构造函数(当你可以动态地添加小部件),而不是调用的 setSetting 方法。

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.

这篇关于资源下载dynamicaly添加按钮作为一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆