Convert JFrame to JPanel

I currently wrote Java Swing with help JFrame, and it works great.

But now I need to use several screens, and they are executed using cardlayouts.

So I need to convert my JFrameto JPanel. I currently have this line for myJFrame

mainFrame.getContentPane().add(c4Panel,BorderLayout.CENTER); 

But if I convert mainFrameto JPanel, I can’t use the getContentPane()IDE tells me to usegetRootPane()

but I get an error in this line

mainPanel.getRootPane().add(c4Panel,BorderLayout.CENTER); 

The error I get is

Exception in thread "main" java.lang.NullPointerException
+4
source share
1 answer
mainPanel.getRootPane().add(c4Panel,BorderLayout.CENTER); 

It should only be:

mainPanel.add(c4Panel,BorderLayout.CENTER); 

The code:

mainFrame.getContentPane()

.. just returns a container that itself has a method add(), and part is getContentPane()not needed for some time.

+3
source

All Articles