简体   繁体   English

JButton不在JFrame的JPanel上显示

[英]JButtons not showing up on JPanel on JFrame

Im trying to display 9 3x3 grids in a 3x3 grid. 我试图在3x3网格中显示9个3x3网格。 None of the buttons are showing up though. 但是,没有任何按钮显示。 I had to create all the individual buttons and i didnt know if they took up to much space of if there is an obvious error in my code. 我必须创建所有单独的按钮,但我不知道它们是否占用了很大的空间,或者我的代码中是否存在明显的错误。 Thanks for any input! 感谢您的输入!

 public class BoardGui extends JFrame{
    private JPanel center;
    private JPanel grid00;
    private JPanel grid01;
    private JPanel grid02;
    private JPanel grid10;
    private JPanel grid11;
    private JPanel grid12;
    private JPanel grid20;
    private JPanel grid21;
    private JPanel grid22;



    private JButton sq0000,sq0100,sq0200,sq1000,sq1100,
        sq1200,sq2000,sq2100,sq2200;
        private JButton sq0001,sq0101,sq0201,sq1001,sq1101,sq1201,sq2001
        ,sq2101,sq2201;
        private JButton sq0002,sq0102,sq0202,sq1002,sq1102,sq1202
        ,sq2002,sq2102,sq2202;
        private JButton sq0010,sq0110,sq0210,sq1010,sq1110,sq1210,
        sq2010,sq2110,sq2210;
        private JButton sq0011,sq0111,sq0211,sq1011,sq1111,
        sq1211,sq2011,sq2111,sq2211;
        private JButton sq0012,sq0112,sq0212,sq1012,sq1112,sq1212,
        sq2012,sq2112,sq2212;
        private JButton sq0020,sq0120,sq0220,sq1020,sq1120,sq1220,
        sq2020,sq2120,sq2220;
        private JButton sq0021,sq0121,sq0221,sq1021,sq1121,
        sq1221,sq2021,sq2121,sq2221;
        private JButton sq0022,sq0122,sq0222,sq1022,sq1122,sq1222,
        sq2022,sq2122,sq2222;

        public void Board(){

            setLayout(new BorderLayout());

            center.setLayout(new GridLayout(3,3));

            grid00 = new JPanel();
            grid01 = new JPanel();
            grid02 = new JPanel();
            grid10 = new JPanel();
            grid11 = new JPanel();
            grid12 = new JPanel();
            grid20 = new JPanel();
            grid21 = new JPanel();
            grid22 = new JPanel();

            grid00.setLayout(new GridLayout(3,3));
            grid01.setLayout(new GridLayout(3,3));
            grid02.setLayout(new GridLayout(3,3));
            grid10.setLayout(new GridLayout(3,3));
            grid11.setLayout(new GridLayout(3,3));
            grid12.setLayout(new GridLayout(3,3));
            grid20.setLayout(new GridLayout(3,3));
            grid21.setLayout(new GridLayout(3,3));
            grid22.setLayout(new GridLayout(3,3));

            sq0000 = new JButton();
            sq0001 = new JButton();
            sq0002 = new JButton();
            sq0010 = new JButton();
            sq0011 = new JButton();
            sq0012 = new JButton();
            sq0020 = new JButton();
            sq0021 = new JButton();
            sq0022 = new JButton();

            sq0100 = new JButton();
            sq0101 = new JButton();
            sq0102 = new JButton();
            sq0110 = new JButton();
            sq0111 = new JButton();
            sq0112 = new JButton();
            sq0120 = new JButton();
            sq0121 = new JButton();
            sq0122 = new JButton();

            sq0200 = new JButton();
            sq0201 = new JButton();
            sq0202 = new JButton();
            sq0210 = new JButton();
            sq0211 = new JButton();
            sq0212 = new JButton();
            sq0220 = new JButton();
            sq0221 = new JButton();
            sq0222 = new JButton();

            sq1000 = new JButton();
            sq1001 = new JButton();
            sq1002 = new JButton();
            sq1010 = new JButton();
            sq1011 = new JButton();
            sq1012 = new JButton();
            sq1020 = new JButton();
            sq1021 = new JButton();
            sq1022 = new JButton();

            sq1100 = new JButton();
            sq1101 = new JButton();
            sq1102 = new JButton();
            sq1110 = new JButton();
            sq1111 = new JButton();
            sq1112 = new JButton();
            sq1120 = new JButton();
            sq1121 = new JButton();
            sq1122 = new JButton();

            sq1200 = new JButton();
            sq1201 = new JButton();
            sq1202 = new JButton();
            sq1210 = new JButton();
            sq1211 = new JButton();
            sq1212 = new JButton();
            sq1220 = new JButton();
            sq1221 = new JButton();
            sq1222 = new JButton();

            sq2000 = new JButton();
            sq2001 = new JButton();
            sq2002 = new JButton();
            sq2010 = new JButton();
            sq2011 = new JButton();
            sq2012 = new JButton();
            sq2020 = new JButton();
            sq2021 = new JButton();
            sq2022 = new JButton();

            sq2100 = new JButton();
            sq2101 = new JButton();
            sq2102 = new JButton();
            sq2110 = new JButton();
            sq2111 = new JButton();
            sq2112 = new JButton();
            sq2120 = new JButton();
            sq2121 = new JButton();
            sq2122 = new JButton();

            sq2200 = new JButton();
            sq2201 = new JButton();
            sq2202 = new JButton();
            sq2210 = new JButton();
            sq2211 = new JButton();
            sq2212 = new JButton();
            sq2220 = new JButton();
            sq2221 = new JButton();
            sq2222 = new JButton();

            grid00.add(sq0000);
            grid00.add(sq0001);
            grid00.add(sq0002);
            grid00.add(sq0010);
            grid00.add(sq0011);
            grid00.add(sq0012);
            grid00.add(sq0020);
            grid00.add(sq0021);
            grid00.add(sq0022);

            grid01.add(sq0100);
            grid01.add(sq0101);
            grid01.add(sq0102);
            grid01.add(sq0110);
            grid01.add(sq0111);
            grid01.add(sq0112);
            grid01.add(sq0120);
            grid01.add(sq0121);
            grid01.add(sq0122);

            grid02.add(sq0200);
            grid02.add(sq0201);
            grid02.add(sq0202);
            grid02.add(sq0210);
            grid02.add(sq0211);
            grid02.add(sq0212);
            grid02.add(sq0220);
            grid02.add(sq0221);
            grid02.add(sq0222);

            grid10.add(sq1000);
            grid10.add(sq1001);
            grid10.add(sq1002);
            grid10.add(sq1010);
            grid10.add(sq1011);
            grid10.add(sq1012);
            grid10.add(sq1020);
            grid10.add(sq1021);
            grid10.add(sq1022);

            grid11.add(sq1100);
            grid11.add(sq1101);
            grid11.add(sq1102);
            grid11.add(sq1110);
            grid11.add(sq1111);
            grid11.add(sq1112);
            grid11.add(sq1120);
            grid11.add(sq1121);
            grid11.add(sq1122);

            grid12.add(sq1200);
            grid12.add(sq1201);
            grid12.add(sq1202);
            grid12.add(sq1210);
            grid12.add(sq1211);
            grid12.add(sq1212);
            grid12.add(sq1220);
            grid12.add(sq1221);
            grid12.add(sq1222);

            grid20.add(sq2000);
            grid20.add(sq2001);
            grid20.add(sq2002);
            grid20.add(sq2010);
            grid20.add(sq2011);
            grid20.add(sq2012);
            grid20.add(sq2020);
            grid20.add(sq2021);
            grid20.add(sq2022);

            grid21.add(sq2100);
            grid21.add(sq2101);
            grid21.add(sq2102);
            grid21.add(sq2110);
            grid21.add(sq2111);
            grid21.add(sq2112);
            grid21.add(sq2120);
            grid21.add(sq2121);
            grid21.add(sq2122);

            grid22.add(sq2200);
            grid22.add(sq2201);
            grid22.add(sq2202);
            grid22.add(sq2210);
            grid22.add(sq2211);
            grid22.add(sq2212);
            grid22.add(sq2220);
            grid22.add(sq2221);
            grid22.add(sq2222);

            center.add(grid00);
            center.add(grid01);
            center.add(grid02);
            center.add(grid10);
            center.add(grid11);
            center.add(grid12);
            center.add(grid20);
            center.add(grid21);
            center.add(grid22);

            add(center, BorderLayout.CENTER);

        }

        public static void main(String[] args) {
            BoardGui gui = new BoardGui();
            gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            gui.setSize(700, 700);
            gui.setResizable(true);
            gui.setVisible(true);
            gui.setTitle("Tic Tac Toe Extreme");
        }

    }

So far, by looking at your code, it's going to generate a NullPointerException because center is never initialized...as to if this is your actually issue, I can only guess 到目前为止,通过查看您的代码,它将生成NullPointerException因为中心从未被初始化...至于这是否是您的实际问题,我只能猜测

Having said that, you need to learn how to use arrays and break you code down into manageable chunks. 话虽如此,您需要学习如何使用数组并将代码分解为可管理的块。

There is so much repetition, you'd be crazy not to re-use it in some way, for example... 重复是如此之多,例如,您会疯狂地不以某种方式重复使用它。

You could create a custom JPanel whose sole responsibility it is, is to show a 3x3 grid of buttons... 您可以创建一个自定义的JPanel它的唯一职责是显示3x3的按钮网格...

public class ButtonPane extends JPanel {

    private JButton[][] btns;

    public ButtonPane() {
        setLayout(new GridLayout(3, 3));
        btns = new JButton[3][3];
        for (int row = 0; row < 3; row++) {
            for (int col = 0; col < 3; col++) {
                btns[row][col] = new JButton(row + "x" + col);
                add(btns[row][col]);
            }
        }
    }

    public JButton getButtonAt(int row, int col) {
        return btns[row][col];
    }

}

You could then create another custom panel, whose responsibility it is, is to show a 3x3 grid of these button panels... 然后,您可以创建另一个自定义面板,其职责是显示这些按钮面板的3x3网格...

public class ContentPane extends JPanel {

    private ButtonPane[][] buttonPanels;

    public ContentPane() {
        setLayout(new GridLayout(3, 3));
        buttonPanels = new ButtonPane[3][3];
        for (int row = 0; row < 3; row++) {
            for (int col = 0; col < 3; col++) {
                buttonPanels[row][col] = new ButtonPane();
                add(buttonPanels[row][col]);
            }
        }
    }

    public ButtonPane getButtonPaneAt(int row, int col) {
        return buttonPanels[row][col];
    }

}

And then put it all together on a frame or other container... 然后将它们放在一起放在框架或其他容器上...

纽扣

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test{

    public static void main(String[] args) {
        new JavaApplication834();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new ContentPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class ContentPane extends JPanel {

        private ButtonPane[][] buttonPanels;

        public ContentPane() {
            setLayout(new GridLayout(3, 3));
            buttonPanels = new ButtonPane[3][3];
            for (int row = 0; row < 3; row++) {
                for (int col = 0; col < 3; col++) {
                    buttonPanels[row][col] = new ButtonPane();
                    add(buttonPanels[row][col]);
                }
            }
        }

        public ButtonPane getButtonPaneAt(int row, int col) {
            return buttonPanels[row][col];
        }

    }

    public class ButtonPane extends JPanel {

        private JButton[][] btns;

        public ButtonPane() {
            setLayout(new GridLayout(3, 3));
            btns = new JButton[3][3];
            for (int row = 0; row < 3; row++) {
                for (int col = 0; col < 3; col++) {
                    btns[row][col] = new JButton(row + "x" + col);
                    add(btns[row][col]);
                }
            }
        }

        public JButton getButtonAt(int row, int col) {
            return btns[row][col];
        }

    }

}

Remember, Java is an object oriented language, one of it's goals to produce re-usable blocks... 记住,Java是一种面向对象的语言,它的目标之一就是产生可重用的块...

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

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