Mapping JComponent inside a JPanel on a JFrame

I cannot display a JComponent inside a JPanel on a JFrame.

The following does not work.

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

But if I add a JComponent to the JFrame [for example frame.add(component, BorderLayout.CENTER);], it will display the content.

Any ideas

+3
source share
2 answers

A JPaneldefault layout is FlowLayout, so you do not need to specify the center of the panel.

Just do:

panel.add(component);

Alternatively, follow these steps:

panel.setLayout(new BorderLayout());
panel.add(component, BorderLayout.CENTER);
+5
source

By default, JComponent is not the preferred size.

JPanel FlowLayout. , , 0, .

, JFrame, BorderLayout. , , , , ,

, , .

+2

Source: https://habr.com/ru/post/1761533/


All Articles