简体   繁体   中英

Call JPanel into another JPanel from a different class

I'm trying to make a GUI application with Java.

I would like to call/include a JPanel-2 with some code into another JPanel-1 (as container) which is in a separate class (Mainclass).

Would this work?

Jpanel Japnel1 = new Jpanel.getcontentpane().add(Mainclass.panel-2);

JPanel does not have a method to return a Container via 'getcontentpane()'.

A JPanel is in itself a Container (see http://docs.oracle.com/javase/6/docs/api/javax/swing/JPanel.html ).

If you want to add a JPanel to another JPanel then as with any Container, use its .add( Component comp) method, as follows:

JPanel myJPanel1 = new JPanel();
JPanel myJPanel2 = new JPanel();

myJPanel1.add( myJPanel2 );

The question is too vague for further advice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM