Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Including more than two Panels in a JFrame?

We are working on a project where we encountered a problem with including more than two Panels on the same JFrame .What we want is one Panel above the other.

Can the community help give an example of ho to implement this or refer me to a good tutorial or guide related to our Java Swing needs?

like image 994
Neethu Avatar asked Mar 27 '10 10:03

Neethu


People also ask

Can a JFrame have multiple panels?

Creating a JFrame Window with Multiple panels added to it Now Let's do some improvisation by adding multiple panel into the JFrame with different colors into it. So you can see that I created 4 Panels and set their location and size according to your own will by using the setBounds() function.

How do I create multiple Jframes?

java write the actual logic code, in this class write the code which creates JFrame, add JButton for open New Frame. In this class write the code which creates JFrame, add JLabel into the New Frame. Simillarly, you can create multiple frames and navigate from one frame to another.

How do I add a panel to a frame in Java?

To add the panel to the frame, get the content pane frame from the frame object first, the call the add method passing in the panel. The pack method tells the frame to resize itself based on the preferred size of the components it contains.

What is the difference between JFrame and JPanel?

Basically, a JFrame represents a framed window and a JPanel represents some area in which controls (e.g., buttons, checkboxes, and textfields) and visuals (e.g., figures, pictures, and even text) can appear.


3 Answers

Assuming you want two panels added to a single frame:

Set a layout for your parent JFrame and add the two panels. Something like the following

JFrame frame = new JFrame();
//frame.setLayout(); - Set any layout here, default will be the form layout
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
frame.add(panel1);
frame.add(panel2);

Assuming you want to add one panel over the other

JFrame frame = new JFrame();
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
frame.add(panel1);
panel1.add(panel2);

There is no limit on the number of panels to be added on the JFrame. You should understand that they all are containers when seen on a higher level.

like image 121
bragboy Avatar answered Oct 12 '22 23:10

bragboy


if you want each of the frames/panels the same size, use the GridLayout, with a grid of 1(column) and 2(rows)

Frame myFrame;  
GridLayout myLayout = new GridLayout(2,1);  

myFrame.setLayout(myLayout);  

Panel p1;  
Panel p2;  

myFrame.add(p1);
myFrame.add(p2);

if the panels are different size use the BorderLayout.... set the upper frame to "North" and the lower one to "South" or "Center"

Frame myFrame;  

myFrame.setLayout(new BorderLayout() );  

Panel p1;  
Panel p2;  

myFrame.add(p1, BorderLayout.NORTH);  
myFrame.add(p2, BorderLayout.CENTER);  
like image 34
Wintermut3 Avatar answered Oct 12 '22 23:10

Wintermut3


//you can also use card Layout, that enables you to add multiple card-panels on Main panel.

CardLayout cl;
JPanel main,one,two,three;
JButton button1,button2;

cl = new CardLayout();
main.setLayout(cl);

main.add(one,"1");
main.add(two,"2");
main.add(three,"3");

cl.show(main,"1");

public void actionPerformed(ActionEvent e){
 if(e.getSource() == button1)
     cl.show(main,"2");
 else if(e.getSource() == button2)
     cl.show(main,"3");
}
like image 44
Ashish kudale Avatar answered Oct 12 '22 23:10

Ashish kudale