Java将按钮动态添加为数组

Java将按钮动态添加为数组,java,arrays,swing,jpanel,jbutton,Java,Arrays,Swing,Jpanel,Jbutton,可能重复: 我想动态添加按钮数组。 我试着这样做: 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.wi

可能重复:

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

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);
        }
    }
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;
}
然后我转到Game类(也是jFrame类)的函数setSetting,它是这样的:

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);
        }
    }
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;
}
void设置(设置s){
此设置=s;
结构.游戏=新结构.游戏(设置);
JPanel面板=新JPanel(新网格布局(5,5,4,4));

对于(inti=1;i我猜
这个
是扩展还是某种框架

第一步是通过执行
this.pack();
设置适合所有对象的帧大小来打包帧

我想你已经把它设为可见了

现在你应该可以看到按钮了。 如果需要不同的布局,请使用
面板.setLayout(新建SomeLayout);

您可以使用添加等高/等宽按钮:

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


请注意,您还可以通过
游戏
构造函数传递
设置
对象引用(当您可以动态添加小部件时)与调用
setSetting
方法不同。

版面管理器已经控制了
JPanel
,设置按钮的大小和位置是不相关的,因为一旦验证了面板,它们就会改变

请尝试在填充按钮后添加面板

用示例更新

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

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

Panel Panel=new Panel();
对于(int i=0;i
方法

for(int i=0;i<1;i++){

对于(int j=0;j什么是
设置。长度
宽度
?@IRelephant设置。长度返回数字8
是jFrame,我像你说的那样尝试了(参见编辑的代码),但仍然没有结果我在
面板后添加了你说的内容。添加(b);
但仍然没有结果;(我做了你说的但仍然没有结果:(:(那么你的问题是在其他地方,你需要共享更多的代码。你需要提供一个我们可以运行的代码来演示你所面临的特定问题。我给你的代码都是我的代码,实际上它只是一个jFrame,我试着在循环上打印一些东西,这样循环运行得很好,现在,你有两个人可以生成o输出,但是没有你的问题的工作示例,我们只是猜测什么是错误的。首先,只有公平的评论在哪里?我看不出你是否在写。我想建议你通过
游戏
类的构造函数参数传递
设置
。请从这里查看代码,它只是在游戏类上,我从choice类中调用它,非常简单,我这样做:调用我的游戏类时,我调用了你的测试类,它工作了:)非常感谢much@tottiroma-我已删除/标记非建设性评论。
Try to use setBounds(x,y,width,heigth) method
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()