Search code examples
javaswing

Java Swing multiple layouts in one frame


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);
    }
}

Solution

  • 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);
        }
    
    }