Matthew Klein
Matthew Klein

Reputation: 5

How do I fill the whole Jframe?

I am fairly new to GUIs in Java. What I want to know is, is there a way to take two JPanels and divide it up in the JFrame, ie. 70% for panel 1 and 30% for panel 2?

Here is a snippet from where I create my GUI:

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import java.awt.Color;
import java.awt.GridLayout;

public class ButtonGrid extends Game
{
private static JFrame frame = new JFrame();

private static JPanel panel1 = new JPanel();
private static JPanel panel2 = new JPanel();
private static JPanel panel3 = new JPanel();
private static JPanel panel4 = new JPanel();

private static JLabel lblP1 = new JLabel("Player 1: ");
private static JLabel lblP2 = new JLabel("Player 2: ");
private static JLabel P1Score = new JLabel("0");
private static JLabel P2Score = new JLabel("0");

public static JButton grid[][];

public ButtonGrid(int width, int length)
{
    frame.setLayout(new GridLayout(width, length));


    panel1.setLayout(new GridLayout(width, length));

    panel2.setLayout(new BoxLayout(panel2, BoxLayout.Y_AXIS));
    panel3.setLayout(new BoxLayout(panel3, BoxLayout.X_AXIS));
    panel4.setLayout(new BoxLayout(panel4, BoxLayout.X_AXIS));

    lblP1.setForeground(Color.blue);
    lblP2.setForeground(Color.red);

    grid = new JButton[width][length];

    for(int x=0; x<length; x++)
    {
        for(int y=0; y<width; y++)
        {
            grid[x][y] = new JButton();
            grid[x][y].addActionListener(actionListener);
            grid[x][y].setName("[" + x + ',' +y + "]");
            //grid[x][y].setText(grid[x][y].getName());
            //frame.add(grid[x][y]);
            panel1.add(grid[x][y]);
        }
    }

    grid[0][0].setBackground(Color.blue);
    grid[width-1][length-1].setBackground(Color.red);

    panel3.add(lblP1);
    panel3.add(P1Score);
    panel4.add(lblP2);
    panel4.add(P2Score);

    panel2.add(panel3);
    panel2.add(panel4);


    frame.add(panel2);
    frame.add(panel1);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(800, 600);
    frame.setVisible(true);
    frame.setResizable(false);

}

What I would like is for the buttons to be even sized and filled to the bottom, now there is about 2/3 of empty space in the frame. How do I fill the rest of the 2/3 of space in the frame with the buttons?

Upvotes: 0

Views: 232

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347184

Take a look at GridBagLayout, it allows you to specify the weight that should be applied to each component.

For example...

JPanel top = new JPanel();
top.setBorder(new LineBorder(Color.RED));
JPanel bottom = new JPanel();
bottom .setBorder(new LineBorder(Color.BLUE));

JPanel parent = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weighty = 0.7;
gbc.fill = GridBagConstraints.BOTH;
parent.add(top, gbc);

gbc.gridy++;
gbc.weighty = 0.3;
parent.add(bottom, gbc);

Side Notes:

  • Don't over use static, there's no need for any of the static references present in your code, as they are all private...
  • Don't create a frame within a JPanel (or any other type of container). The component should not care about where it might be used, that's an external decision...
  • Using setResizable will change the size of the viewable area and should be done before setting the size of the window. You should rely on pack instead of setSize as this will size the window so that the content size is honoured

Upvotes: 2

Related Questions