简体   繁体   English

JButton没有出现在GridBagLayout中

[英]JButtons not appearing in GridBagLayout

I am trying to create a 10 x 10 grid of perfectly square JButtons with no spacing in between. 我正在尝试创建一个10 x 10的完美正方形JButton网格,它们之间没有间距。 I believe that in order to accomplish this my only solution is the GridBagLayout. 我相信,为了完成此任务,我唯一的解决方案是GridBagLayout。 However, I'm stuck on step one - I can't get them to appear when I use a loop. 但是,我坚持执行第一步-使用循环时无法让它们出现。 Here is what I have. 这就是我所拥有的。

Class with main() function: 具有main()函数的类:

public class PixelArtist {
    public static void main(String[] args) {
        try {
            PixelArtistGUI frame = new PixelArtistGUI();
            frame.setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

GUI Handler class: GUI处理程序类:

public class PixelArtistGUI extends JFrame {
    public PixelArtistGUI() {
        setTitle("PixelArtist");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);

        JPanel contentPane = new JPanel(new GridBagLayout());
        this.add(contentPane);
        contentPane.setSize(500, 500);
        GridBagConstraints c = new GridBagConstraints();

        JButton b;

        for (int j = 0; j < 10; j++) {
            for (int i = 0; i < 10; i++) {
                b = new JButton();
                c.gridx = i;
                c.gridy = j;
                contentPane.add(b,c);
            }
        }

    }
}

All the above does is open a very small, seemingly empty JFrame shown here: 以上所有操作都是打开一个很小的,看似空的JFrame,如下所示:

按钮未出现在JFrame中

The last thing I have done is I added the contentPane.setSize(500, 500); 我做的最后一件事是添加contentPane.setSize(500, 500); line, but that does not seem to be making any difference. 线,但这似乎没有任何区别。

I am sure it's something simple - thank you in advance for the help. 我相信这很简单-预先感谢您的帮助。

This should show something (I tested it). 这应该显示一些东西(我测试过)。 Check for the lines // <<<<<< where I added statements: 检查行// <<<<<<我在其中添加语句的行:

  • Set the panel as the content pane 将面板设置为内容窗格
  • Set the preferred size for the content pane 设置内容窗格的首选大小
  • Calling pack to size the frame to the preferred size 调用pack将框架调整为首选大小
  • Show the frame using SwingUtilities.invokeLater to play nice with the Event Dispatching Thread 使用SwingUtilities.invokeLater显示框架以与事件调度线程一起很好地播放

import java.awt.*;
import javax.swing.*;

public class PixelArtistGUI extends JFrame {
    public PixelArtistGUI() {
        setTitle("PixelArtist");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);

        JPanel contentPane = new JPanel(new GridBagLayout());
        setContentPane(contentPane); // <<<<<
        contentPane.setPreferredSize(new Dimension(500, 500)); // <<<<<
        GridBagConstraints c = new GridBagConstraints();

        JButton b;

        for (int j = 0; j < 10; j++) {
            for (int i = 0; i < 10; i++) {
                b = new JButton();
                c.gridx = i;
                c.gridy = j;
                contentPane.add(b,c);
            }
        }
        pack(); // <<<
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() { // <<<
            @Override
            public void run() {
                try {
                    new PixelArtistGUI().setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

You should at least use 您至少应该使用

public void add(Component comp,Object constraints)

Ie: 即:

contentPane.add(b,c);

And as FredK says add the contentPane to the frame 正如FredK所说,将contentPane添加到框架

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM