2017-04-24 43 views
0

我有一个扩展的JPanel类,它创建一个JPanel并将其添加到其父项。 getComponents从父调用正确地示出了该面板,但是从的getParent扩展实例调用返回null.layeredPane我的扩展JPanel的父项为空

package testing; 

import javax.swing.JButton; 

public class ParentNullExample extends javax.swing.JFrame { 

private static Panel1 mainPanel; 
private static JButton AddButton; 

private void createAndShowGUI() { 
    //Set the look and feel. 
    initLookAndFeel(); 

    setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE); 

    AddButton = new javax.swing.JButton(); 
    AddButton.setName("AddButton"); 
    AddButton.setText("Add a Row"); 
    AddButton.addActionListener(new java.awt.event.ActionListener() { 
     @Override 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      addRowActionPerformed(evt); 
     } 
    }); 

    Panel1 componentPanel = new Panel1(true); 
    componentPanel.setName("componentPanel"); 

    mainPanel = new testing.Panel1(false); 
    mainPanel.setName("mainPanel"); 

    mainPanel.add(componentPanel); 
    mainPanel.add(AddButton); 

    mainPanel.setOpaque(true); 
    setContentPane(mainPanel); 

    pack(); 
    DebugUtils.analyseFrame(this); 
} 

private void addRowActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addRowActionPerformed 
    mainPanel.revalidate(); 
}//GEN-LAST:event_addRowActionPerformed 

public static void main(String[] args) { 
    javax.swing.SwingUtilities.invokeLater(new Runnable() { 
     @Override 
     public void run() { 
      new ParentNullExample().setVisible(true); 
     } 
    }); 
} 
} 

package testing; 

import java.awt.Component; 
import java.awt.Container; 
import java.awt.Dimension; 
import java.awt.GridLayout; 
import javax.swing.BoxLayout; 
import static javax.swing.BoxLayout.Y_AXIS; 
import javax.swing.JPanel; 

public class Panel1 extends JPanel { 

    private JPanel mainPanel; 
    private Dimension preferredSize; 
    private GridLayout tableGridLayout = new GridLayout(0,1); 
    private Boolean isGridLayout; 

    public Panel1(Boolean gridLayout) { 
     isGridLayout = gridLayout; 
     mainPanel = new JPanel(); 
     if (gridLayout) { 
      super.setLayout(tableGridLayout); 
     } 
     else { 
      BoxLayout boxLayout = new BoxLayout(this,Y_AXIS); 
      super.setLayout(boxLayout); 
     } 
     preferredSize = new Dimension(); 
    } 

    @Override 
    public void revalidate() { 
     if (preferredSize == null) preferredSize = new Dimension(); 
     Component superParent = super.getParent(); 
     String superParentName = (superParent == null ? "null" : superParent.getName()) ; 
     Component thisParent = this.getParent(); 
     String thisParentName = (thisParent == null ? "null" : thisParent.getName()) ; 

     System.out.print("revalidate: "+ 
      "Name: "+this.getName()+", "+ 
      "super.Parent: "+superParentName+", "+ 
      "this.Parent: "+thisParentName+", "+ 
      "preferredSize: ["+preferredSize.getHeight() +", "+ 
      preferredSize.getWidth()+"]\n"); 

     if (superParent != null) superParent.revalidate(); 
     if (thisParent != null) thisParent.revalidate(); 
    }; 

    @Override 
    public Dimension getPreferredSize() { 
     return preferredSize; 
    }; 

    @Override 
    public Dimension getMaximumSize() { 
     return new Dimension((int) super.getMaximumSize().getWidth(), (int) preferredSize.getHeight()); 
    }; 

    @Override 
    public Component add(Component c) { 
     super.add(c); 
     preferredSize.setSize(addComponentSize(c, preferredSize)); 
     tableGridLayout.layoutContainer(mainPanel); 
     return null; 
    } 

    public static Dimension addComponentSize(Component c, Dimension sizeSoFar) { 
     int componentWidth = (int) c.getPreferredSize().getWidth(); 
     int panelWidth = (int) sizeSoFar.getWidth(); 
     int width = (panelWidth > componentWidth) ? panelWidth : componentWidth; 
     int height = (int) (c.getPreferredSize().getHeight() + sizeSoFar.getHeight()); 
     return new Dimension(width, height); 
    } 

} 

输出显示父名称为“null.layeredPane”,这是不预期的一个。

revalidate: Name: null, super.Parent: null, this.Parent: null, preferredSize: [0.0, 0.0] 
revalidate: Name: null, super.Parent: null, this.Parent: null, preferredSize: [0.0, 0.0] 
revalidate: Name: null, super.Parent: null, this.Parent: null, preferredSize: [0.0, 0.0] 
revalidate: Name: null, super.Parent: null, this.Parent: null, preferredSize: [0.0, 0.0] 
1- Frame: Class: testing.ParentNullExample, Name: frame0, H: 61, W: 132 
2|- Container: Name: mainPanel, Class: testing.Panel1, Layout: javax.swing.BoxLayout, H: 23, W: 116 
3|-- Component: Class: testing.Panel1, Name: componentPanel, H: 0, W: 116 
3|-- Component: Class: javax.swing.JButton, Name: AddButton, H: 23, W: 85 


revalidate: Name: mainPanel, super.Parent: null.layeredPane, this.Parent: null.layeredPane, preferredSize: [23.0, 85.0] 
revalidate: Name: mainPanel, super.Parent: null.layeredPane, this.Parent: null.layeredPane, preferredSize: [23.0, 85.0] 
+0

How to use root panes,我留下问自己,如果你明白'super' d oes - 从我能告诉'Panel2'永远不会添加到任何东西(btw,在提供的上下文中'this.getParent()'和'super.getParent()'做同样的事情) – MadProgrammer

+1

考虑提供一个[如何创建一个最小的,完整的和可验证的例子](http://stackoverflow.com/help/mcve) - 这种方式我们没有四处奔波,试图猜测你的问题 – MadProgrammer

+0

嗯,有些事情我不明白,但我没有命名任何东西null.layeredPane – Denizen

回答

1

所以,基本上,你似乎有一个API的误解。一个JFrameJRootPane,其中包含了JLayeredPane,其中包含了contentPaneJMenuBarglassPane,如下...

RootPane

所以,当使用setContentPane(mainPanel);,您要添加mainPanelJLayeredPane

根据您所提供的代码的详细信息