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.
2 Answers 2
First off, what you're referring to is a JPanel, not a JPane
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.
9 Comments
JPanel panel = new JPanel(); panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));There are easy ways to design your User Interface in Java Swing or the last One JAVAFX. In Swing, there is a eclipse plugin called Swing Windows Builder, there you can easily build your UI by dragging and dropping and As for the JAVAFX, there is Nice and free IDE called Intelli IDE CE, it has also built-in UI designer called Oracle JavaFx Scene Builder. Go for the JavaFx and speed up your development.
JPanel, right? And yes, you can set theLayoutManagerof anyContainer.