Home > Blockchain >  Managing panels (GridLayout) size in a BorderLayout JFrame
Managing panels (GridLayout) size in a BorderLayout JFrame

Time:06-28

So, i'm making a java battleship game with a GUI from Java Swing. I actually designed the borders (a very initial way) and now i'm trying to make a well-sized board. Consider that the board are the blue/red panels in the photo below

A pack() results in this

fullscreen

But i want larger boards. What should i do? setPrefferedSize() seems to not solve the problem, i think because the JFrame is BorderLayout...


package testeborderlayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;

public class TesteBorderLayout extends JFrame {
    private JButton[][] tabuleiro1 = new JButton[10][10];
    private JButton[][] tabuleiro2 = new JButton[10][10];
    private JPanel jPanel1;
    private JPanel jPanel2;
    private JTextField jTextField1; 
    
    public TesteBorderLayout() {
        initComponents();
    }
                       
    private void initComponents() {

        jPanel1 = new JPanel();
        jPanel2 = new JPanel();
        jTextField1 = new JTextField();
        
        for(int i=0;i<10;i  ){
            for(int j=0; j<10;j  ){
                tabuleiro1[i][j]=new JButton();
                tabuleiro2[i][j]=new JButton();
            }
        }
        
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBackground(new Color(255, 102, 102));
        jPanel1.setLayout(new GridLayout(10,10));
        getContentPane().add(jPanel1, BorderLayout.PAGE_START);

        jPanel2.setBackground(new Color(51, 102, 255));
        jPanel2.setLayout(new GridLayout(10,10));
        getContentPane().add(jPanel2, BorderLayout.PAGE_END);
        
        for(int i=0;i<10;i  ){
            for(int j=0;j<10;j  ){
                jPanel1.add(tabuleiro1[i][j]);
                jPanel2.add(tabuleiro2[i][j]);
            }
        }
        
        jTextField1.setBackground(new Color(255,255,255));
        jTextField1.setText("Comandos para o usuário");
        getContentPane().add(jTextField1, BorderLayout.CENTER);
        setTitle("Batalha Naval");
        pack();
    }                

    public static void main(String args[]) {
        
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TesteBorderLayout().setVisible(true);
            }
        });
    }
                
}

CodePudding user response:

BorderLayout.PAGE_START/END will use the components preferredSize. Instead, I'd use a GridBagLayout, for example...

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Main {

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

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new TesteBorderLayout();
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TesteBorderLayout extends JFrame {
        private JButton[][] tabuleiro1 = new JButton[10][10];
        private JButton[][] tabuleiro2 = new JButton[10][10];
        private JPanel jPanel1;
        private JPanel jPanel2;
        private JTextField jTextField1;

        public TesteBorderLayout() {
            initComponents();
        }

        private void initComponents() {

            jPanel1 = new JPanel();
            jPanel2 = new JPanel();
            jTextField1 = new JTextField();

            for (int i = 0; i < 10; i  ) {
                for (int j = 0; j < 10; j  ) {
                    tabuleiro1[i][j] = new JButton();
                    tabuleiro2[i][j] = new JButton();
                }
            }

            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

            JPanel contentPane = new JPanel(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.weightx = 1;
            gbc.weighty = 1;
            gbc.fill = GridBagConstraints.BOTH;

            jPanel1.setBackground(new Color(255, 102, 102));
            jPanel1.setLayout(new GridLayout(10, 10));
            contentPane.add(jPanel1, gbc);

            jPanel2.setBackground(new Color(51, 102, 255));
            jPanel2.setLayout(new GridLayout(10, 10));
            gbc.gridy = 2;
            contentPane.add(jPanel2, gbc);

            for (int i = 0; i < 10; i  ) {
                for (int j = 0; j < 10; j  ) {
                    jPanel1.add(tabuleiro1[i][j]);
                    jPanel2.add(tabuleiro2[i][j]);
                }
            }

            gbc.gridy = 1;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.weighty = 0;

            jTextField1.setBackground(new Color(255, 255, 255));
            jTextField1.setText("Comandos para o usuário");
            contentPane.add(jTextField1, gbc);

            add(contentPane);
            setTitle("Batalha Naval");
            pack();
        }
    }    
}

See How to Use GridBagLayout for more details

CodePudding user response:

You are using the contentPange you need to make it visible. Also pack() always makes the minimum possible dimensions. You should replace the pack with the following two lines.

    this.setSize(800, 800);
    getContentPane().setVisible(true);

Then you can see how you want to resize the individual components. (panels, buttons etc)

  • Related