954,228 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

problem with jpanels

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

enes
Newbie Poster
9 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

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

peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 901
 

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();

<strong>     cp.setLayout(new FlowLayout());</strong>

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

}
Artmann
Newbie Poster
19 posts since Nov 2007
Reputation Points: 10
Solved Threads: 1
 

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

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

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.

Artmann
Newbie Poster
19 posts since Nov 2007
Reputation Points: 10
Solved Threads: 1
 

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.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You