简体   繁体   中英

JPanel not displaying in JFrame

I know that the same question has been asked many times, but I really don't seem to find the error in my code that hinders the object of type JPanel to be displayed in the JFrame. Here is the constructor of the class that extends JFrame:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;


public class Game extends JFrame implements ActionListener {

  private static final long serialVersionUID = 1L;

  private Board board;

  public Game() {
    super("Game");

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBackground(Color.WHITE);

    board = new Board();
    add(board, BorderLayout.CENTER);

    JButton button = new JButton("Start new game");
    button.setFocusPainted(false);
    button.addActionListener(this);

    JPanel control = new JPanel();
    control.setBackground(Color.WHITE);
    control.add(button);
    add(control, BorderLayout.SOUTH);

    pack();
    setResizable(false);
    setVisible(true);   
  }

And this one is the constructor of the class that extends JPanel:

public class Board extends JPanel implements ActionListener {

  public Board() {
    setBackground(Color.WHITE);
    setLayout(new GridLayout(NUMBER_OF_ROWS, NUMBER_OF_COLUMNS));
    setBorder(BorderFactory.createEmptyBorder(20, 20, 0, 20));
    board = new Cell[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS];

    for (int row = 0; row < NUMBER_OF_ROWS; row++) {
      for (int column = 0; column < NUMBER_OF_COLUMNS; column++) {
        board[row][column] = new Cell(this, row, column);
        add(board[row][column]);
      }
    }
  }

When I run the main method (which I didn't show here), it shows only the frame and the button. If someone could give a hint on this, I would be very thankful.

It seems to show up just fine in this close variant of the code that is runnable (a MCTaRE ).

在此处输入图片说明

Note that I put some space in the GridLayout and changed the colors to make panel boundaries more clear.

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

public class Game extends JFrame {

    private static final long serialVersionUID = 1L;
    private Board board;

    public Game() {
        super("Game");

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBackground(Color.WHITE);

        board = new Board();
        add(board, BorderLayout.CENTER);

        JButton button = new JButton("Start new game");
        button.setFocusPainted(false);

        JPanel control = new JPanel();
        control.setBackground(Color.GREEN);
        control.add(button);
        add(control, BorderLayout.SOUTH);

        pack();
        setResizable(false);
        setVisible(true);
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                new Game();
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency
        SwingUtilities.invokeLater(r);
    }
}

/** A pseudo Cell */
class Cell extends JButton {
    Cell(JComponent parent, int row, int column) {
        super(row + " " + column);
    }
}

/** And this one is the constructor of the class that extends JPanel */
class Board extends JPanel {

    int NUMBER_OF_ROWS=3;
    int NUMBER_OF_COLUMNS=4;
    Cell[][] board;

    public Board() {
        setBackground(Color.RED);
        setLayout(new GridLayout(NUMBER_OF_ROWS, NUMBER_OF_COLUMNS, 5, 5));
        setBorder(BorderFactory.createEmptyBorder(20, 20, 0, 20));
        board = new Cell[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS];

        for (int row = 0; row < NUMBER_OF_ROWS; row++) {
            for (int column = 0; column < NUMBER_OF_COLUMNS; column++) {
                board[row][column] = new Cell(this, row, column);
                add(board[row][column]);
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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