Search code examples
swingjframejpanellayout-managergridbaglayout

My components in two different panels are too small and don't fit to the frames size, how can I fix this?


I made a JFrame (size: 500 , 600) that uses GridBagLayout to place two JPanels (one using GridLayout and the other using GridBagLayout) on top of each other; however, when I run my JFrame the components are really small.

I wanted the grid panel to be 500, 500 and the GridBagLayout panel to be 500, 100. I tried using weighty and weightx but no matter what value I entered I got the same result. I'm sure I'm missing something as I only just learned GridBagLayout.


Screen

Java frame with small components:

https://i.sstatic.net/mLqpevVD.png

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

public class Board extends JFrame{
//Instance Variables -----------------------------------------------------------


//Board Constructor ------------------------------------------------------------
public Board() {
    this.setSize(500, 600);
    this.setTitle("VolticBoard");
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.weightx = 0.5;
    gbc.weighty = 0.5;
    gbc.anchor = GridBagConstraints.CENTER;

    add(new gridPane(), gbc);
    gbc.gridy++;
    add(new controlPane(), gbc);
    gbc.fill = GridBagConstraints.VERTICAL;
}

//Main Method ----------------------------------------------------------------
public static void main(String[] args) {
    Board b = new Board();
    b.setVisible(true);
    }
}

Code for GridPane:

import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class gridPane extends JPanel{
//Instance Variables ----------------------------------------------------------    
private JButton b1, b2, b3, b4, b5, b6, b7, b8, b9 , b10, b11, b12, b13, b14, b15, b16, b17, b18, b19, b20, b21, b22, b23, b24, b25;
private JButton[] buttons = {b1, b2, b3, b4, b5, b6, b7, b8, b9 , b10, b11, b12, b13, b14, b15, b16, b17, b18, b19, b20, b21, b22, b23, b24, b25};

//grid Constructor ----------------------------------------------------------------------------------------------------------------------------------------------
public gridPane() {
    setLayout(new GridLayout(5, 5, 2 , 2));
    setPreferredSize(new Dimension(500, 500));

    for(int i = 0; i < 25; i++) {
        buttons[i] = new JButton(i + 1 + "");
        buttons[i].setBackground(new Color(20,110,109));
        buttons[i].setForeground(new Color(233, 138, 50));
    }

    for(int u = 0; u < 25; u++) {
        add(buttons[u]);
    }

    setUpGridListeners();
    this.setVisible(true);
}

//Listeners for Buttons ------------------------------------------------------------------------------------------------------------------------------------------
public void setUpGridListeners() {
    for(JButton curBut : buttons) {
        curBut.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int buttonNum = Integer.valueOf(curBut.getText());
                Sound.playSound(buttonNum);
            }
        });
    }
}
}

Java code showing controlPane with GridBagLayout:

import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class controlPane extends JPanel{
//Instance Variables ---------------------------------------------------------
private JButton bns, bcs;

//control Constructor --------------------------------------------------------
public controlPane() {
    setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.weightx = 0.5;
    gbc.weighty = 0.5;
    gbc.anchor = GridBagConstraints.PAGE_END;
    gbc.insets = new Insets(5, 0, 0, 0);

    bns = new JButton("Set Sound");
    bns.setBackground(new Color(20,110,109));
    bns.setForeground(new Color(233, 138, 50));

    bcs = new JButton("Clear Sound");
    bcs.setBackground(new Color(20,110,109));
    bcs.setForeground(new Color(233, 138, 50));

    add(bns, gbc);
    gbc.gridx++;
    add(bcs, gbc);
    gbc.fill = GridBagConstraints.HORIZONTAL;

    this.setVisible(true);
}

//Listeners for Buttons -----------------------------------------------------
public void setUpControlListeners() {
    bns.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //int buttonNum = Integer.valueOf(bns.getText());
                //Sound.setSound(soundFile, buttonNum);
            }
    });
}
}

Sound Class for storing and playing sounds:

public class Sound {
@SuppressWarnings("unused")
private String soundFile;
@SuppressWarnings("unused")
private int soundNumber;


public static void playSound(int soundNum) {
    System.out.println("Sound Played " + soundNum);
}

public void setSound(String newSound, int buttonNum) {
    soundFile = newSound;
    soundNumber = buttonNum;
}
}

End Goal

java frame with the components correctly sized:

(https://i.sstatic.net/Me7E2dpB.png)


Solution

    1. To follow the java standard, use initial uppercase letters for class names.
    2. For the GridPane set the minimum size as well.
    3. For the buttons set the minimum size and the font size.
    import javax.swing.*;
    import java.awt.*;
    
    public class Board extends JFrame{
    //Instance Variables -----------------------------------------------------------
    
    
    //Board Constructor ------------------------------------------------------------
      public Board() {
        this.setSize(500, 600);
        this.setTitle("VolticBoard");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
        setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 0.5;
        gbc.weighty = 0.5;
        gbc.anchor = GridBagConstraints.CENTER;
    
        add(new GridPane(), gbc);
        gbc.gridy++;
        add(new ControlPane(), gbc);
        gbc.fill = GridBagConstraints.VERTICAL;
      }
    
    //Main Method ----------------------------------------------------------------
      public static void main(String[] args) {
        Board b = new Board();
        b.setVisible(true);
      }
    }
    
    =============================================
    
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    public class ControlPane extends JPanel{
    //Instance Variables ---------------------------------------------------------
      private JButton bns, bcs;
    
    //control Constructor --------------------------------------------------------
      public ControlPane() {
        setLayout(new GridBagLayout());
        setPreferredSize(new Dimension(450, 100));
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.weightx = 0.5;
        gbc.weighty = 0.5;
        gbc.anchor = GridBagConstraints.PAGE_END;
        gbc.insets = new Insets(4, 0, 2, 5);
    
        bns = new JButton("Set Sound");
        Dimension dim= new Dimension(200, 75);
        Font font= bns.getFont().deriveFont(Font.PLAIN).deriveFont(20.f);
        bns.setMinimumSize(dim);
        bns.setFont(font);
        bns.setBackground(new Color(20,110,109));
        bns.setForeground(new Color(233, 138, 50));
    
        bcs = new JButton("Clear Sound");
        bcs.setMinimumSize(dim);
        bcs.setFont(font);
        bcs.setBackground(new Color(20,110,109));
        bcs.setForeground(new Color(233, 138, 50));
    
        add(bns, gbc);
        gbc.gridx++;
        add(bcs, gbc);
    //    gbc.fill = GridBagConstraints.HORIZONTAL;
    
        this.setVisible(true);
      }
    
    //Listeners for Buttons -----------------------------------------------------
      public void setUpControlListeners() {
        bns.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    //int buttonNum = Integer.valueOf(bns.getText());
                    //Sound.setSound(soundFile, buttonNum);
                }
        });
      }
    }
    
    ==================================================
    
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    public class GridPane extends JPanel{
    //Instance Variables ----------------------------------------------------------    
    private JButton b1, b2, b3, b4, b5, b6, b7, b8, b9 , b10, b11, b12, b13, b14, b15, b16, b17, b18, b19, b20, b21, b22, b23, b24, b25;
    
    private JButton[] buttons = {b1, b2, b3, b4, b5, b6, b7, b8, b9 , b10, b11, b12, b13, b14, b15, b16, b17, b18, b19, b20, b21, b22, b23, b24, b25};
    
    //grid Constructor ----------------------------------------------------------------------------------------------------------------------------------------------
      public GridPane() {
        setLayout(new GridLayout(5, 5, 4, 4));
        setPreferredSize(new Dimension(500, 500));
        setMinimumSize(new Dimension(500, 500));
    
        Font font= new Font(Font.DIALOG, Font.PLAIN, 20);
        for(int i = 0; i < 25; i++) {
            buttons[i] = new JButton(i + 1 + "");
            buttons[i].setBackground(new Color(20,110,109));
            buttons[i].setForeground(new Color(233, 138, 50));
            buttons[i].setFont(font);
        }
    
        for(int u = 0; u < 25; u++) {
            add(buttons[u]);
        }
    
        setUpGridListeners();
        this.setVisible(true);
      }
    
    //Listeners for Buttons ------------------------------------------------------------------------------------------------------------------------------------------
      public void setUpGridListeners() {
        for(JButton curBut : buttons) {
            curBut.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    int buttonNum = Integer.valueOf(curBut.getText());
    //                Sound.playSound(buttonNum);
                }
            });
        }
      }
    }