Search code examples
javaswingjframejpanelboxlayout

Adding a JLabel to a JFrame that is using BoxLayout


I am creating a JFrame that contains a JPanel with a grid of buttons. Everything works fine but I then want to add a JLabel above the panel of buttons but the label never appears. It does appear if I don't use BoxLayout, though. Any help is appreciated.

The first code part below is my JFrame class:

import java.awt.Dimension;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Frame extends JFrame {
    private static final long serialVersionUID = 1L;

    Panel panel = new Panel();
    Dimension frameDim = new Dimension(1000, 1000);
    Dimension labelDim = new Dimension(100, 20);
    Box box = new Box(BoxLayout.Y_AXIS);
    JLabel label = new JLabel("Tic Tac Toe");
    JPanel pane = new JPanel();

    public Frame() {

        pane.add(label);
        pane.setPreferredSize(labelDim);
        pane.setMinimumSize(labelDim);
        add(pane);
        box.add(Box.createVerticalGlue());
        box.add(panel);     
        box.add(Box.createVerticalGlue());
        add(box);
        setTitle("Tic Tac Toe");
        setSize(frameDim);
        setMinimumSize(frameDim);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);
    }
}

The code below is my JPanel class with buttons:

import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JPanel;

public class Panel extends JPanel {
    private static final long serialVersionUID = 2L;

    private int i;

    JButton[] button = new JButton[9];
    GridLayout layout = new GridLayout(3, 3);
    Dimension dim = new Dimension(500, 500);

    public Panel() {

        for (i = 0; i<9; i++) {
            button[i] = new JButton();
            add(button[i]);
        }
        setPreferredSize(dim);
        setMinimumSize(dim);
        setMaximumSize(dim);
        setLayout(layout);
    }   
}

The last code part below is the Main class:

public class RunGame {

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

Solution

  • you can use the BorderLayout when adding your label into Pane into the Frame and remove that box thing as below

    public Frame()
      {
        //create label and add it to the frame
        JLabel label = new JLabel("Tic Tac Toe");
        label.setHorizontalAlignment( JLabel.CENTER );
        add(label, BorderLayout.NORTH);
    
        //create buttonsPanel and add it to the frame 
        JPanel buttons = new JPanel();
        buttons.setLayout( new GridLayout(3, 3));
        for (int i = 0; i < 9; i++)
        {
          buttons.add(new JButton(""+i));
        }
        add(buttons, BorderLayout.CENTER);
    
        //setup the title, other properties for the frame etc.. 
        setTitle("Tic Tac Toe");
        setSize(1000, 1000);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);
      }
    

    hope this helps better :)