Panel is not showing on frame in Java Swing

Viewed 35

I've made a HallOfFame class which is subclass of JPanel and i want to add a label writing "Hall OF Fame" on this panel.In the MainWindow class (frame) i have added the HallOfFame (Panel) to the content pane but nothing shows up. The same happens with every component i am trying to add on the frame (MainWindow) except the top (placed North with the BorderLayout) panel with 3 buttons.

public class HallOfFame extends JPanel{
JLabel hofLabel;

public HallOfFame() {
    
    
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
this.setBorder(new LineBorder(Color.GRAY, 1));
this.setAlignmentX(CENTER_ALIGNMENT);

hofLabel = new JLabel("Hall OF Fame");

add(hofLabel);

}

}

public class MainWindow extends JFrame{

/*Size of main window*/
public static final Dimension winSize = new Dimension(1200, 800);
public static final int TOP_HEIGHT = 80;
public static final int PLAYER_WIDTH = 300;


private GameBoard gameBoard;
private HallOfFame hallOfFame;
private BannerPanel bannerPanel;
private PlayerPanel playerPanel;

public MainWindow() {
    
    //Initializing the frame.
    Container c = this.getContentPane();
    c.setPreferredSize(winSize);
    this.setTitle("TucTacToe");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    
    /*Hall of fame*/
    hallOfFame = new HallOfFame();  
    
    /*GameBoard*/
    gameBoard = new GameBoard();
    
    /*Banner Panel*/
    bannerPanel = new BannerPanel();
    
    /*PlayerPanel*/
    playerPanel = new PlayerPanel();
    
    /*Adding the components to the window*/
    c.add(bannerPanel, BorderLayout.NORTH);
    c.add(hallOfFame, BorderLayout.CENTER);
    //add(gameBoard, BorderLayout.CENTER);
    c.add(playerPanel);
    
    this.pack();
    this.setVisible(true);//setting visible the frame.
}

}

Here is the window of the project

https://i.stack.imgur.com/EJDmm.png

1 Answers
c.add(hallOfFame, BorderLayout.CENTER);
c.add(playerPanel)

You are adding the "playerPanel" to the CENTER of the frame. When you don't specify the constraint it defaults to the CENTER when using BorderLayout.

Only the last component added will be visible. So it appears your playerPanel doesn't have any components added to it.

For a simple test just use:

c.add(hallOfFame, BorderLayout.CENTER);
//c.add(playerPanel)

Now you should see the hallOfFame panel.

Related