简体   繁体   English

JButton的2D数组未出现

[英]2D Array of JButtons Not Appearing

So I'm working an an assignment and I seemed to be stuck on a bug I can't seem to overcome. 所以我正在做一个作业,我似乎陷入了一个我似乎无法克服的错误。 Essentially when trying to create a 10x10 grid, it isn't appearing while everything else (eg. title and menu) is when I'm running the application. 本质上,当尝试创建10x10网格时,当我运行应用程序时,其他所有内容(例如标题和菜单)都不会出现。 Here's the essential part of what I've done so far 这是我到目前为止所做的重要部分

public class minesweeperGUI extends JFrame{

    private JFrame gameGUI;
    private JPanel board;
    private JButton[][] boardButtons = new JButton [10][10];

    public minesweeperGUI(){

        gameGUI = new JFrame("Enhanced Minesweeper"); //Title of the Window
        gameGUI.setVisible(true); //We want (true) to set it visible
        gameGUI.setSize(400,550); //400 550
        gameGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //This will close the frame when you press X. IMPORTANT
        gameGUI.getContentPane().setBackground(Color.CYAN);
        gameGUI.setLayout(null);

        //Panel for minesweeper board
        board = new JPanel();
        GridLayout experimentLayout = new GridLayout(10, 10);
        board.setLayout(experimentLayout);
        board.setSize(400,550);

        for(int x=0;x<boardButtons.length;x++){
            for(int y=0;y<boardButtons[x].length;y++){

                boardButtons[x][y] = new JButton();
                board.add(boardButtons[x][y]);
                }
        }

        gameGUI.add(board);



    }

    public static void main(String[] args) {

        minesweeperGUI mainInterface = new minesweeperGUI();

    }
}

Any type of help would be greatly appreciated :) 任何类型的帮助将不胜感激:)

gameGUI.setVisible(true); gameGUI.setVisible(true); //We want (true) to set it visible //我们想要(true)将其设置为可见

  • yes but its should be last code line in minesweeperGUI() , otherwise you added JComponent s to the already visible Swing Gui (missing notifier for automatical refresh, repaint) 是的,但是它应该是minesweeperGUI()最后一个代码行,否则,您将JComponent添加到了已经可见的Swing Gui中(缺少用于自动刷新和重新绘制的通知程序)

minesweeperGUI mainInterface = new minesweeperGUI(); minesweeperGUI mainInterface =新的minesweeperGUI();

  • should be wrapped into invokeLater , more in Oracle trail Initial Thread 应该包装在invokeLater ,更多内容应放在Oracle Trail Initial Thread

  • class name should be MinesweeperGUI , search for Java Namings Convention 类名应为MinesweeperGUI ,搜索Java Namings Convention


gameGUI.setLayout(null); gameGUI.setLayout(null); and board.setSize(400,550); 和board.setSize(400,550);

  • missing there setBounds for board.setSize(400,550); board.setSize(400,550);缺少setBounds board.setSize(400,550); (placing JPanel to JFrame , because JPanel has zero Dimension ) (将JPanel放置到JFrame ,因为JPanel Dimension数为零)

  • there no reason to use null layout , remove gameGUI.setLayout(null); 没有理由使用null layout ,删除gameGUI.setLayout(null);

    1. override getPreferredSize for private JPanel board; 覆盖private JPanel board; getPreferredSize private JPanel board;

    2. remove code lines gameGUI.setLayout(null); 删除代码行gameGUI.setLayout(null); and board.setSize(400,550); board.setSize(400,550);

    3. add code line pack() before setVisible(true) , for proper sizing for JFrame (based on getPreferredSize for private JPanel board; ) setVisible(true) pack()之前添加代码行pack() ,以正确调整JFrame大小(基于private JPanel board; getPreferredSize private JPanel board;

Remove the gameGUI.setLayout(null); 删除gameGUI.setLayout(null); call and move the next line to be after buttons adding 调用并将下一行移动到添加按钮之后

    gameGUI.setSize(400,550); //400 550
    gameGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //This will close the frame when you press X. IMPORTANT
    gameGUI.getContentPane().setBackground(Color.CYAN);

    gameGUI.setVisible(true); //We want (true) to set it visible

I think one Problem is that you mix up your instances... 我认为一个问题是您混合了实例...

public static void main(String[] args) {
    minesweeperGUI mainInterface = new minesweeperGUI();
}

makes a new Instance of your minesweeperGUI wich extends JFrame - so this simply makes a new JFrame... 为您的minesweeperGUI创建新实例,并扩展JFrame-因此,这仅使新JFrame ...

but in your construktor you create another JFrame instance private JFrame gameGUI; 但是在构造函数中,您将创建另一个JFrame实例private JFrame gameGUI; and gameGUI = new JFrame("Enhanced Minesweeper"); gameGUI = new JFrame("Enhanced Minesweeper"); ... ...

i suggest you remove the private variable gameGUI ( private JFrame gameGUI; ) and use simply this for your layout... 我建议你删除私有变量gameGUI( private JFrame gameGUI;并使用简单this为您的布局?

//private JFrame gameGUI; //remove this one!
private JPanel board;
private JButton[][] boardButtons = new JButton [10][10];
public minesweeperGUI(){

    super("Enhanced Minesweeper"); //construktor from superclass
    this.setVisible(true); //use of this
    this.setSize(400,550); //use of this
    ...        

    //Panel for minesweeper board
    board = new JPanel();
    GridLayout experimentLayout = new GridLayout(10, 10);
    board.setLayout(experimentLayout);
    board.setSize(400,550);

    for(int x=0;x<boardButtons.length;x++){
        for(int y=0;y<boardButtons[x].length;y++){

            boardButtons[x][y] = new JButton();
            board.add(boardButtons[x][y]);
            }
    }
    this.add(board); //use of this

}

you don't HAVE to write this.setXXX() you can also simply write setVisible(true) for instance... 不必this.setXXX()你也可以简单的写setVisible(true)例如...

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

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