Java 动态添加按钮作为数组

5

可能重复:
java - 如何在单击时动态添加Swing组件到GUI中?

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

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

我按照要求做了,但是没有得到任何结果,我犯了什么错误?

编辑

我有一个名为“choices”的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();
    }

然后我进入游戏类(也是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(panel);
        pack();
        setVisible(true);
    }
    structure.Setting setting;
}

1
Setting.lengthwidth是什么? - irrelephant
@irrelephant setting.length 返回数字 8。 - William Kinaan
4个回答

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

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

评论在哪里?我看不到你是否在写。 - William Kinaan
我想建议您通过Game类的构造函数参数传递settings - KV Prajapati
请从这里查看代码,它只在Game类中,我从Choice类中调用它,非常简单。http://www.mediafire.com/download.php?96cj26hlxb7nxl4 - William Kinaan
我做了这个:不是调用我的游戏类,而是调用你的测试类,它起作用了 :) 非常感谢你。 - William Kinaan
@tottiroma - 我已经删除/标记了无建设性的评论。 - KV Prajapati
显示剩余2条评论

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

我按照你说的做了,但还是什么都没有 :( :( - William Kinaan
1
那么你的问题出在其他地方,你需要分享更多的代码。你需要提供一个SSCCE,我们可以运行它来演示你所面临的特定问题。 - MadProgrammer
现在你有两个人可以产生输出,但是如果没有你问题的工作示例,我们只能猜测出错了什么。 - MadProgrammer
我会尝试你的代码,稍等一下,非常感谢。 - William Kinaan
1
AVD 先到了,公平起见。 - MadProgrammer
显示剩余2条评论

1

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

第一步是通过执行this.pack();来打包框架,使其大小刚好适合所有对象。

我假设你已经将其设置为可见了?

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


这是一个jFrame,我按照你说的方式进行了尝试(请参见编辑后的代码),但仍然没有任何结果。 - William Kinaan

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

我在 panel.add(b); 后面添加了你说的内容,但还是没有任何显示;( - William Kinaan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接