Java 如何从JInternalFrame访问jpanel(在JDesktopPane中)

Java 如何从JInternalFrame访问jpanel(在JDesktopPane中),java,swing,jpanel,jinternalframe,jdesktoppane,Java,Swing,Jpanel,Jinternalframe,Jdesktoppane,我想从JInternalFrame设置一个jpanel(在JDesktopPane中)的访问权限,我将它设置为public,但当我进行这样的测试时,它不会给出“null”: JDesktop frameP = new JDesktop(); frameP.jPanel1.setVisible(true); //This is what I want but it doesn't work System.out.println("Name " + frameP.jPanel1.getName()

我想从JInternalFrame设置一个jpanel(在JDesktopPane中)的访问权限,我将它设置为public,但当我进行这样的测试时,它不会给出“null”:

JDesktop frameP = new JDesktop();
frameP.jPanel1.setVisible(true); //This is what I want but it doesn't work

System.out.println("Name " + frameP.jPanel1.getName());
最后一行给了我空值,这意味着他没有得到JPanel

我还尝试了以下代码:

 for(int k=0; k<frameP.getContentPane().getComponentCount();k++ ){
             if(frameP.getContentPane().getComponent(k)instanceof  JPanel){
                 JPanel pnl = (JPanel) frameP.getContentPane().getComponent(k);
                 if(pnl.getName().equals("jPanel1")){
                     pnl.setVisible(true);
                 }
                 System.out.println("Nom pnl: " +pnl.getName()); // That gives null too
             }
         } 

for(int k=0;k我只是解决了这个问题,我这样做了:

JDesktop.jpnlGauche.setVisible(false);
JDesktop.jpnlHaut.setVisible(false);

什么是JDesktop?这可能是您的类,但您尚未提供它的代码,因此我们的任何回答都只是猜测。此外,如果您试图使用JDesktopPanes,您不会直接将jpanel添加到它们。您将jpanel添加到JInternalFrame,然后将此JIF添加到JDesktopPane,这反过来又添加到JFrame.JDesktop这只是JDesktopPane的名称。我使用调色板添加控件,这就是为什么面板在JDesktop上,我想从JInternalFrameNo访问它,JDesktop不仅仅是“JDesktopPane的名称”。显然,您已经对它进行了子类化(不是同一件事),并且还没有向我们展示它的代码。“我使用调色板添加控件,这就是为什么面板在JDesktop上,我想从JInternalFrame访问它”……我不想试图解读这条评论,我只想让你看看教程: