Skip to main content

how to connect Jframe and Jpanel

Please note these forums are being decommissioned and use the new and improved forums at
2 replies [Last post]
Joined: 2012-06-17

i noe this is a silly question.

i had a main class Jframe created with netbean gui builder and a Jpanel class with netbean gui builder also. i wan to show jpanel class after i click on a button. how can i link this 2 together?
assume tat jframe=frame1
i tried new frame2().setVisible(true);
but it doesn't work.

one more question.
is it possible to link multiple jpanel class to form a new gui class?
example. jpanel class 1 ,2,3,4
i wan to put jpanel 1,2,3 into 4 and make a new layout from it.
how to do i connect the together?

anyone pls help

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2012-04-08

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.

Joined: 2007-11-14

JFrame and JPanel are quite different classes so you cannot simply equate them as you seem to be trying to do.
In outline: you need to look at the getContentPane() method of class javax.swing.JFrame. That gets you a java.awt.Container which has a layout manager (you can set different types). Then you can use the add() method of Container to add components (JPanels, JButtons, etc) to the content pane of the frame. Then make the frame visible.