Java - GridBagLayout wrong ratio

34 Views Asked by At

I am new to creating a GUI with Java. I tried to use the GridBagLayout for a project but i am stuck here.

I want to make the Button and the two JLabels the same size but the button is bigger.

public class Menu extends JPanel {
    
    public Menu() {
        
        
        
        this.setBackground(Color.black);
        
        
        GridBagLayout gb = new GridBagLayout();
        setLayout(gb);
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.BOTH;
        
        
        JButton b1 = new JButton();
        b1.setText("Start");
        
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 1;
        gbc.weighty= 1;
        gb.setConstraints(b1, gbc);
        add(b1);
        
        
        JPanel Gap = new JPanel();
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.weightx = 1;
        gbc.weighty= 1;
        
        gb.setConstraints(Gap, gbc);
        add(Gap);
        
        
        RadioPanel radio = new RadioPanel();
        
        gbc.gridx = 2;
        gbc.gridy = 0;
        gbc.weightx = 1;
        gbc.weighty = 1;
        
        gb.setConstraints(radio, gbc);
        add(radio);
        
        
        
        
        
        
        
        
        
        
    }

}

Click here to see my GUI

This is my code and this is what it looks like.

1

There are 1 best solutions below

0
Basil Bourque On

GridLayout, not GridBagLayout

For three equally sized widgets, use GridLayout rather than GridBagLayout. See Oracle tutorial, How to Use GridLayout.

Here is an example of a button with two labels, all the same size.

package work.basil.example.swing;

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

// An abbreviated version of example code provided by Oracle from:
// https://docs.oracle.com/javase/tutorial/displayCode.html?code=https://docs.oracle.com/javase/tutorial/uiswing/examples/layout/GridBagLayoutDemoProject/src/layout/GridBagLayoutDemo.java
public class ThreeWidgets
{
    private static void createAndShowGUI ( )
    {
        JFrame.setDefaultLookAndFeelDecorated( true );

        // Establish the window, represented by a `JFrame` object.
        JFrame frame = new JFrame( "Three Widgets App" );
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

        makeContent( frame.getContentPane() );

        // Show window.
        frame.pack();
        frame.setVisible( true );
    }

    private static void makeContent ( Container container )
    {
        container.setLayout( new GridLayout() );

        JButton widget1 = new JButton( "Widget 1" );

        JLabel widget2 = new JLabel( "Widget 2" );
        widget2.setBorder( BorderFactory.createLineBorder( Color.GREEN ) );

        JLabel widget3 = new JLabel( "Widget 3 with long title" );
        widget3.setBorder( BorderFactory.createLineBorder( Color.MAGENTA ) );

        container.add( widget1 );
        container.add( widget2 );
        container.add( widget3 );
    }

    public static void main ( String[] args )
    {
        // Assign a task to event-dispatching thread: Create and show our GUI.
        SwingUtilities.invokeLater( ThreeWidgets :: createAndShowGUI );
    }
}

screenshot of window with GridLayout