when ı add two panels into a frame last panel i added seen on the screeen how can i show both panels in one frame

Recommended Answers

All 5 Replies

There gone be something wrong with your code. Can you please post it?

PS: Use code tags, press hash "#" sign on post toolbar and paste your code between the tags which will appear

It's probobly a layout issue. Try to use something like this

class MyFrame extends JFrame
{
   
    public MyFrame()
    {
    
     setSize(800,600);
     setTitle("MyFrame");

     JPanel panel1 = new JPanel();
     JPanel panel2 = new JPanel();

     Container cp = getContentPane();

[B]     cp.setLayout(new FlowLayout());[/B]

     cp.add(panel1);
     cp.add(panel2);
 
    }
 

}

and what makes you think you get only one panel to show if you do that?

My Example will show two panels.
I just wanted to show him how to add a layout to the frame.
If he hasn't done that he will only see one panel.

Well, the contentPane of the the JFrams already has a layout. It simply needs to be used correctly. If you use add without any anchor parameter then according to the documentation
As a convenience, BorderLayout interprets the absence of a string specification the same as the constant CENTER

So, simply calling add(Component) twice, is the same as calling add(Component, CENTER) twice. She should be calling add(Component, CENTER) and add(Component, SOUTH), for example.

Of course, changing the layout (as your post showed) is always an option, but is not, strictly speaking, needed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.