user3868427
user3868427

Reputation: 43

I can't figure out how to arrange these buttons as seen in the link

package GUI;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

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

public class GridBagLayoutEx2
{
     public GridBagLayoutEx2()
    {
    JFrame frame = new JFrame("GridBagLayoutEx2");
    frame.setVisible(true);
    frame.setSize(400, 400);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);

    JPanel panel = new JPanel();
    panel.setLayout(new GridBagLayout());

    GridBagConstraints gbc = new GridBagConstraints();

    JButton b1 = new JButton("1");
    JButton b2 = new JButton("2");
    JButton b3 = new JButton("3");
    JButton b4 = new JButton("4");
    JButton b5 = new JButton("5");

    gbc.insets = new Insets(2, 2, 2, 2);

    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.gridheight = 5;
    gbc.gridwidth = 3;
    gbc.fill = GridBagConstraints.VERTICAL;
    panel.add(b1, gbc);

    gbc.gridx = 1;
    gbc.gridy = 1;
    gbc.gridheight = 1;
    gbc.gridwidth = 1;
    panel.add(b2, gbc);

    gbc.gridx = 2;
    gbc.gridy = 2;
    gbc.gridheight = 1;
    panel.add(b3, gbc);

    gbc.gridx = 3;
    gbc.gridy = 3;
    gbc.gridheight = 1;
    panel.add(b4, gbc);

    gbc.gridx = 1;
    gbc.gridy = 4;
    gbc.gridwidth = 3;
    gbc.gridheight = 1;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    panel.add(b5, gbc);

    frame.add(panel);
    //frame.pack();
}

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

}

enter image description here

Hello to everyone. I have been trying to settle this GUI for a while. Eventhough I watched many GUI tutorials, after countless number of trial-and-errors I gave up. In image (above) you can see the layout I am trying make. Thanks in advance...

Upvotes: 0

Views: 130

Answers (1)

Jan Bodnar
Jan Bodnar

Reputation: 11647

Consider using MigLayout manager. For the unit type, I have chosen centimeters. MigLayout provides several unit types to choose from.

The following example fits your image well:

package com.zetcode;

import java.awt.EventQueue;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import net.miginfocom.swing.MigLayout;

public class ButtonsEx extends JFrame {

    public ButtonsEx() {

        initUI();

        setTitle("MigLayout example");
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        
    }

    private void initUI() {

        JPanel pnl = new JPanel(new MigLayout());
        pnl.add(new JButton("Button"), "spany 2, grow, w 4cm, h 4cm");
        pnl.add(new JButton("Button"), "spanx 2, w 4cm, h 2cm, grow");
        pnl.add(new JButton("Button"), "w 2cm, h 2cm, wrap");
        pnl.add(new JButton("Button"), "w 2cm, h 2cm");
        pnl.add(new JButton("Button"), "w 2cm, h 2cm");
        pnl.add(new JButton("Button"), "w 2cm, h 2cm, wrap");

        add(pnl);

        pack();
    }    

    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                ButtonsEx ex = new ButtonsEx();
                ex.setVisible(true);
            }
        });
    }
}

MigLayout example

Upvotes: 1

Related Questions