Home > Software design >  Set 3x3 grid layout with swing
Set 3x3 grid layout with swing

Time:10-22

Im trying to build a tic tac toe game with swing. Using grid layout its coming out to weird numbers of rows and columns and cant seem to get it to 3x3. What am I doing wrong? Should I just use a float layout and set positions?

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

public class main {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Tic Tac Toe");
        JPanel panel = new JPanel();

        frame.add(panel);
    
        GridLayout grid = new GridLayout(3,3);
    
    
        frame.setVisible(true); 
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(600, 600);
        frame.setLayout(grid);
        frame.setResizable(false);
    
        JButton button1 = new JButton();
        JButton button2 = new JButton();
        JButton button3 = new JButton();
        JButton button4 = new JButton();
        JButton button5 = new JButton();
        JButton button6 = new JButton();
        JButton button7 = new JButton();
        JButton button8 = new JButton();
        JButton button9 = new JButton();
    
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);
        frame.add(button4);
        frame.add(button5);
        frame.add(button6);
        frame.add(button7);
        frame.add(button8);
        frame.add(button9);
    
    }
}

CodePudding user response:

Due to some reasons, a panel is preferred over frames for assigning layouts.

Assign the layout to your panel like this: panel.setLayout(grid);

Then, add the panel to your frame like this: frame.add(panel);

The, add all your widgets to the panel by replacing frame with panel where you're adding your Buttons.

CodePudding user response:

Although you have already accepted @JustinCoding screen capture of running Swing app

Explanation

Originally, in order to add components to a JFrame, you had to first call method getContentPane which returned a Container that you could add components to. JFrame is referred to as a top level container and its content pane is the container for all the components that you add to it. The default content pane is a JPanel whose layout manager is BorderLayout. Hence no need to add your components to a JPanel and add that JPanel to the JFrame. You can add components directly to the JFrame. Just be aware that when you are adding components directly to a JFrame, you are actually adding them to a JPanel with BorderLayout.

And since the content pane is a JPanel, you are free to also change its layout manager – as you have done in your code via this line:

frame.setLayout(grid);

Note that you only need to set one of the dimensions in the GridLayout constructor. Here is a quote from the javadoc of that constructor.

One, but not both, of rows and cols can be zero, which means that any number of objects can be placed in a row or in a column.

In other words, for your tic-tac-toe game, you need only set the columns to 3. That means that each row will contain no more than three columns so when you add nine buttons, GridLayout will ensure that they will be arranged in three rows of three columns each. Hence, for your tic-tac-toe board, you can use the following code.

GridLayout grid = new GridLayout(0, 3); // zero rows and three columns
  • Related