Java – display JComponent in JPanel on JFrame

I can't display JComponent. In JPanel on JFrame

The following does not work

JComponent component = ...
panel.add(component,BorderLayout.CENTER);
frame.add(panel,BorderLayout.CENTER);

But if I add JComponent to JFrame [such as frame. Add (component, borderlayout. Center);], It will display the contents

Any ideas

Solution

The default layout of JPanel is flowlayout, so you don't have to specify the center of the panel

Simply put:

panel.add(component);

Or, do:

panel.setLayout(new BorderLayout());
panel.add(component,BorderLayout.CENTER);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>