I am learning Java Swing and I appended a menuBar to the frame. By default this should call jframe.getContentPane().add(child)
. When I ran the script the menuBar didn't show up. But the button was at the very top "y=0" if that makes sense.
Then I realized my mistake I actually had to put in a menu in the menubar. Then the menuBar showed up. So that got me thinking...is the "menubar" "contentpane" actually 2 panels? It is confusing the hell out of me. Because that acted a lot like a panel. But getContentPane()
returns a Container, not a JPanel object so I'm confused.
If so, does that mean that the only thing that is dumped directly into a frame are just Jpanel objects? Hence JButtons, JLabels are not directly in a frame... Does that mean, jpanels are "nested"? One more thing that is confusing me. If a jpanel can control how things are positioned, what is a LayoutManager for? :S Thanks, and please answer as if to a 2yr old asking why the sky is blue,ha ;)
getContentPane()
always returns aContainer
instance. However, you should note thatJPanel
objects areContainer
instances, as well as other classes in the Swing framework. The actual class of the instance returned is irrelevant, as you do not have control over which implementation ofContainer
is used as a contentPane (unless you have forced a specific contentPane), and most of the time this should not be a problem.You can add many GUI widgets in a
JFrame
, such asJButton
,JLabel
, etc. However, they will be automatically added to the associated contentPane.JPanel
does not handle the objects positioning, theLayoutManager
associated with your panel does; either automatically based on its own set of rules (e.g.FlowLayout
), or by using the constraints you have specified when adding the object to the container (theGridBagLayout
layout manager is a good example). The JavaDoc on the LayoutManagers usually contain enough information to get you started on using them.You can have nested panels, yes. A
Container
can contain otherContainer
instances. While it seems to be a complicated solution, it does enable you to control exactly how your GUI is displayed. Depending on whichLayoutManager
you are using, on the needs you have to fulfill with your user interface, and on your own preferences/habits as a developper, you might need less or more nested panels.Some random thoughts:
getContentPane()
is technically a Container, it's also a JPanel (which inherits eventually from Container).You need to see this API doc for
JComponent
.If you look at the inheritance hierarchy, you will see that the
JComponent
extendsComponent
so a JComponent is a Component.Also under Direct Known Subclasses, you can see the list of all the classes that extend the
JComponent
includingJMenuBar
andJPanel
.So,
JMenuBar
andJPanel
are two more specialized versions ofJComponent
(orContainer
).for that there is method
for todays Java Swing GUI, is not neccesary declare
ContentPane
, from Java5, and with BorderLayout as defaultLayoutManager
then
frame.add(myPanel);
//is same asframe.add(myPanel, BorderLayout.CENTER)
and occupated whole Containerbasic stuff about how to use LayourManagers