JPanel Content Confusion

I am learning Java Swing and I have added a menuBar to the frame. By default, this should cause jframe.getContentPane().add(child). When I ran the script, menuBar did not appear. But the button was at the very top of "y = 0", if that makes sense.

Then I realized my mistake, which I really needed to enter into the menu in the menu bar. Then the menu screen appeared. So it made me wonder ... is this "menu" "contentpane" actually 2 panels? This is confusing for me. Because it is very similar to the panel. But it getContentPane()returns a container, not a JPanel object, so I'm confused.

If so, does this mean that the only thing that strikes directly into the frame is just Jpanel objects? Therefore, JButtons, JLabels are not directly in the frame ... Does this mean that jpanels are "nested"? Another thing that confuses me. If jpanel can control the location of things, what is the purpose of the LayoutManager ?: S Thank you, and please respond as if to a 2 year old asking why the sky is blue, ha;)

+5
source share
4 answers

Some random thoughts:

  • Yes, JPanels and other components are often "nested". A solid understanding of Swing / AWT layout managers is important here.
  • , JFrame getContentPane(), , JPanel ( Container).
  • , , Container contentPane, , .
+10

frame.setJMenuBar(menuBar);

Java Swing ContentPane, Java5 BorderLayout LayoutManager

frame.add(myPanel); // , frame.add(myPanel, BorderLayout.CENTER),

, LayourManagers

+3

getContentPane() Container. , JPanel Container, Swing. , , Container contentPane ( ), .

GUI JFrame, JButton, JLabel .. .

JPanel , LayoutManager, ; , (, FlowLayout), , ( GridBagLayout). JavaDoc LayoutManagers , .

, . A Container Container. , , . , LayoutManager , , , / , .

+1

API doc JComponent.

, , JComponent Component, JComponent .

In addition, in the Subcategory, under the direct name, you can see a list of all extension classes JComponent, including JMenuBarand JPanel.

So, JMenuBarthey JPanelare two more specialized versions JComponent(or Container).

0
source

All Articles