Home > Software engineering >  GUI Layout with Java
GUI Layout with Java

Time:05-10

I'm trying to create a GUI with 3 components arranged vertically centered in the middle. Now I've created the components but I cannot find a way to add them vertically on top of each other vertically. I've tried making different panels and different layouts and I get something different every time. Any help would greatly be appreciated. Here's my code.

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

public class EquationSolverGUI extends JFrame {

    public static final int WIDTH = 500;
    public static final int HEIGHT = 300;;

    public EquationSolverGUI() {
        
        super("Equation Solver");
        setSize(WIDTH, HEIGHT);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new GridLayout(3, 0));

        JPanel panel = new JPanel();
        JPanel inputPanel = new JPanel();
        JPanel outputPanel = new JPanel();
        JPanel buttonPanel = new JPanel();


        JTextField input = new JTextField(20);
        inputPanel.add(input);

        JTextField output = new JTextField(20);
        outputPanel.add(output);

        JButton solve = new JButton("Solve");
        buttonPanel.add(solve);

        add(input);
        add(output);
        add(solve);
        
        panel.add(inputPanel);
        panel.add(outputPanel);
        panel.add(buttonPanel);

        add(panel, BorderLayout.CENTER);
    }

     public static void main(String[] args) {
        EquationSolverGUI equationSolver = new EquationSolverGUI();
        equationSolver.setVisible(true);
    }
    
    
}

CodePudding user response:

Take a look at enter image description here

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JFrame;
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 JFrame();
                frame.add(new MainPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class MainPane extends JPanel {
        public MainPane() {
            setLayout(new GridBagLayout());

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.gridwidth = GridBagConstraints.REMAINDER;

            JTextField input = new JTextField(20);
            JTextField output = new JTextField(20);
            JButton solve = new JButton("Solve");

            add(input, gbc);
            add(output, gbc);
            add(solve, gbc);
        }
    }
}

CodePudding user response:

You almost had it.

Change this line...

setLayout(new GridLayout(3, 0));

...to be this instead...

setLayout(new GridLayout(0, 1));

...and that will fix everything.

The reason why this fixes everything is because the constructor accepts 2 parameters -- rows and columns. If you set rows to 10, you will get 10 rows. If you set columns to 20, you will get 20, and so on.

However, if you set either rows or columns (NOT BOTH) to equal 0, then you will be permitted to add an infinite number of elements.

For example, if I want 20 (or so) elements stacked on top of each other, I would do new GridLayout(0, 1), which is another of saying I want some number of rows, but only 1 column, which is another way of saying, just make one column of an indefinite amount of elements. It works vice versa for horizontal, just make it new GridLayout(1, 0).

  • Related