Java将按钮动态添加为数组

时间:2012-12-19 06:02:25

标签: java arrays swing jpanel jbutton

  

可能重复:
  java - How would I dynamically add swing component to gui on click?

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

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;
}

4 个答案:

答案 0 :(得分:3)

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

enter image description here

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);
}

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

答案 1 :(得分:2)

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

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

使用示例更新

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

enter image description here

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() {
        }
    }
}

答案 2 :(得分:1)

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

第一步是通过执行this.pack();打包框架来设置适合所有对象的框架尺寸。

我假设您已将其设置为可见?

现在你应该可以看到按钮了。 如果您想要不同的布局,请使用panel.setLayout(new SomeLayout);

答案 3 :(得分:1)

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()