I'm trying to increase the distance of my JButtons from the top of my Panel to make it more visually appealing, i've tried using an invisible button but have had no luck.
public class SimpleBorder {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(500,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Border etched = (Border) BorderFactory.createEtchedBorder();
String[] items = {"A", "B", "C", "D"};
JList list = new JList(items);
JTextArea text = new JTextArea(10, 40);
JScrollPane scrol = new JScrollPane(text);
JScrollPane scrol2 = new JScrollPane(list);
JPanel panel= new JPanel();
panel.add(scrol2,BorderLayout.WEST);
panel.add(scrol, BorderLayout.EAST);
panel.setBorder(etched);
frame.add(panel);
frame.setVisible(true);
}
}
Any ideas ?
The key basically lies, in the fact, that the component doesn't knows it's actual size, till
frame.pack()
won't be called. Hence after this I am performing this calculation, to determine how much empty space to put forBorder
and again callingframe.pack()
torepack()
everything after putting theBorder
.Please do have a look at this example, and see if this suite your needs :
Here is the output :
EDIT 1 :
Moreover, if you will use
GridLayout
instead of usingGridBagLayout
for theMainMenu
, then I guess the results will be more promising. Please have a look at this example for that change :EDIT 2 :
Another variant is looking much better, though, this time, the base
JPanel
is usingGridLayout
and theMenuPanel
is usingGridBagLayout
. Please have a look at this example :