i was coding Address Book GUI using GridBagLayout . problem is that when i set gridx and gridy that does not work it looks as there is no layout used and components fall in center of the Frame . i am not pasting whole code just a small piece of code for 1 JButton
to let you see what is happening .. please see the code and gudide me how to fix it , right now button is falling in center of the JFrame
I am just beginner so please dont mind if foolish style code or any problem like that
public class rect
{
JFrame frame ;
JPanel panel1 ;
JButton b1;
GridBagConstraints gbc;
public rect()
{
panel1 = new JPanel();
panel1.setSize(300,300);
panel1.setLayout(new GridBagLayout());
gbc = new GridBagConstraints();
frame= new JFrame("Address ?ooK ");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
b1= new JButton () ;
b1.setText("Hello ");
gbc.gridx=1;
gbc.gridy=0;
panel1.add(b1, gbc) ;
frame.add(panel1);
panel1.setVisible(true);
frame.setVisible(true);
}
weightx/weight
to1.0
and changeanchor
toGridBagLayout.NORTH
... and see what happens ;-) I recommend to carefully read GBL documentation and tutorial. – Guillaume Polet