Michael Michael - 17 days ago 6
Java Question

Java GUI layout questions

Sorry if this has been posted before, but I cannot seem to find any good info that helps me, or I just don't understand other answers enough to help me as I just started programming GUIS.

I wrote a program that has various Items in their own (sub)Jpanel(jtextfields. combobox's, buttons etc.) and all the sub Jpanels in a main jpanel inside a jframe. My goal is to center and stack each sub JPanel on top of one another, so that when the user resizes the window each item stays centered and stacked on top of one another. (when I stacked I don't mean layered where one pane is in front of another, rather stacking the panes like a sandwich so to speak) My panes just move with the default flowlayout and I hope to stop that.

I have seen BoxLayout but like I said, I am new to GUIS and I am not sure if I can apply the BoxLayout to Jpanels.

Answer

First off, it is called a JPanel, not JPane :P

Secondly,

JPanel panel = new JPanel(new BorderLayout());

BorderLayout is probably your best bet. For more information on layouts, check out

http://docs.oracle.com/javase/tutorial/uiswing/layout/using.html#choosing

And by the way, NetBeans GUI Builder is probably your best friend as an introductory GUI programmer.

Comments