1

I am new to Java and learning GUI now a days. I want to add space on top of may FirstPlayer name I am using JPanel with GridLayout but when I add invisible box as the first element to set my elements in the center but I got nothing as my desire. Please help me.

Here is my code:

JPanel    main   =   new  JPanel();

GridLayout  layout  =   new GridLayout(6,1);
layout.setVgap(10);
JPanel  parentPanel =   new JPanel(layout);
parentPanel.setOpaque(false);
parentPanel.add(Box.createRigidArea(new Dimension(80,0)));
parentPanel.add(main.getFirstName());
parentPanel.add(main.getFirstField());
parentPanel.add(Box.createRigidArea(new Dimension(20,0 )));

parentPanel.add(main.getSecondName());
parentPanel.add(main.getSecondField());

main.add(parentPanel,BorderLayout.CENTER);

JFrame frame    =   new JFrame("Player Menu");

frame.add(main,BorderLayout.CENTER);
frame.add(Box.createRigidArea(new Dimension(100,0 )),BorderLayout.NORTH);
frame.setVisible(true);
frame.pack();
frame.setSize(900,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Ahsan Rafiq
  • 111
  • 2
  • 8
  • 3
    Try `GridBagLayout` instead, [how to use](http://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html) –  Jan 07 '15 at 10:53
  • 2
    What exactly are you trying achieve? `GridLayout` makes all components equal size, including the rigid area. If you need to make something of different size, you need to use another layout manager. `GridBagLayout` was already suggested, but a vertical `BoxLayout` could be simpler and sufficient for your purposes. – kiheru Jan 07 '15 at 10:58
  • Take a look at http://stackoverflow.com/a/5328475/1015495 – Mike G Jan 07 '15 at 15:54

0 Answers0