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:
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 --
As you can see in this image, using the 0-style protects you from accidentally miscounting the number of elements. Now, the result remains as expected without any surprises.
As you can see though, you can still add an infinite number of elements without using 0-style, but I strongly recommend that you do not do this. The behaviour is difficult to follow, and unnecessarily complicates your domain. Instead, it's much easier to either use 0-style, or find some way to assert the number of elements being added.