I am trying to add button in my GUI using Gridbagconstraints
in LayoutManager
in Java. The location of the button is always in the center, irrespective of the coordinates.
package StudentInfo;
import javax.swing.*;
import java.awt.*;
public class Gui {
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
frame.add(panel);
panel.setLayout(new GridBagLayout());
frame.setSize(600,600);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
GridBagConstraints gbc = new GridBagConstraints();
JButton b = new JButton("Hello");
gbc.gridx=1;
gbc.gridy=1;
panel.add(b,gbc);
JButton v = new JButton("exit");
gbc.gridx=1;
gbc.gridx=0;
panel.add(v,gbc);
}
}
Output
Question
How to define co-ordinates of the buttons and place them at desired location?