0

I'm trying to create Connect 4 game which would look like on this picture: enter image description here I've been able to create a gridlayout with 42 buttons and now I need to add Reset button. I believe that I need to combine 2 layouts in one frame but I dont know how to do that and cant find any answer anywhere. Thank you for your help and time.

public class ApplicationRunner {

public static void main(String[] args) {
    new ConnectFour();
    }
} 
import javax.swing.*;
import java.awt.*;

public class ConnectFour extends JFrame {
    private String buttonLbl = "X";
    HashMap<String, JButton> buttons;
    public ConnectFour() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(600, 600);
        setTitle("Connect Four");
        setLocationRelativeTo(null);

        JButton resetButton = new JButton();
        resetButton.setName("reset button");
        resetButton.setText("Reset");

        for (int i = 6; i > 0; i--) {
            for (char c = 'A'; c <= 'G'; c++) {
                String cell = "" + c + i;
                JButton cellButton = new JButton(" ");

                cellButton.setBackground(Color.LIGHT_GRAY);
                cellButton.setName("Button" + cell);
                add(cellButton);
            }
        }

        GridLayout gl = new GridLayout(6, 7, 0, 0);
        setLayout(gl);
        setVisible(true);
    }
}
Michal
  • 229
  • 3
  • 11
  • 3
    You can have one layout per [panel](https://docs.oracle.com/en/java/javase/17/docs/api/java.desktop/javax/swing/JPanel.html) So you can keep the frame’s borderlayout, create one panel with a gridlayout and add it to the center of the frame and create a footer panel containing the button (and perhaps more elements in the future) and add it to the frame’s south. – Holger Jan 10 '23 at 08:50
  • 3
    Use multiple containers, each with their own layout manager, then combine them together in a parent container, for [example](https://stackoverflow.com/questions/26411775/how-i-can-do-swing-complex-layout-java/26412976#26412976); [example](https://stackoverflow.com/questions/24462297/layout-relative-to-screensize/24462643#24462643); [example](https://stackoverflow.com/questions/33946629/relative-and-absolute-positioning-issues-with-swing/33947433#33947433); [example](https://stackoverflow.com/questions/33576358/how-to-use-java-swing-layout-manager-to-make-this-gui/33577874#33577874) – MadProgrammer Jan 10 '23 at 08:58
  • Oracle has a helpful tutorial, [Creating a GUI With Swing](https://docs.oracle.com/javase/tutorial/uiswing/index.html). Skip the Learning Swing with the NetBeans IDE section. Pay particular attention to the [Laying Out Components Within a Container](https://docs.oracle.com/javase/tutorial/uiswing/layout/index.html) section. – Gilbert Le Blanc Jan 10 '23 at 11:06

1 Answers1

2

One solution is to use two JPanel instances (with each having its own LayoutManager).

Then you add these two JPanel instances to your JFrame.

Example:

public class MyApplication extends JFrame {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                MyApplication app = new MyApplication();
                app.setVisible(true);
            }
        });
    }

    private MyApplication() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(600, 600);
        setTitle("Connect Four");
        setLocationRelativeTo(null);

        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.Y_AXIS));
        JButton resetButton = new JButton();
        resetButton.setName("reset button");
        resetButton.setText("Reset");
        resetButton.setAlignmentX(Component.RIGHT_ALIGNMENT);
        buttonPanel.add(resetButton);

        // add buttonPanel to JFrame
        add(buttonPanel, BorderLayout.SOUTH);

        JPanel mainPanel = new JPanel(new GridLayout(6, 7, 0, 0));

        for (int i = 6; i > 0; i--) {
            for (char c = 'A'; c <= 'G'; c++) {
                String cell = "" + c + i;
                JButton cellButton = new JButton(" ");

                cellButton.setBackground(Color.LIGHT_GRAY);
                cellButton.setName("Button" + cell);
                mainPanel.add(cellButton);
            }
        }

        // add mainPanel to JFrame
        add(mainPanel, BorderLayout.CENTER);

        setVisible(true);
    }

}
Michael
  • 537
  • 1
  • 5
  • 13