我的扩展JPanel的父级为null

时间:2017-04-24 01:40:15

标签: java swing null jpanel parent

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

public String printMaze(char[][] maze) {
    String s= "";
    for(int i=0;i<maze.length;i++){
        for(int j=0; i<maze[i].length;j++){
            s= maze.toString();;
        }
    }
    return s;
}
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);
        }
    });
}
}

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

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);
    }

}

1 个答案:

答案 0 :(得分:1)

所以,基本上,你似乎对API有误解。 JFrameJRootPane组成,其中包含JLayeredPane,其中包含contentPaneJMenuBarglassPane,如下所示。 ..

RootPane

因此,在使用setContentPane(mainPanel);时,您需要将mainPanel添加到JLayeredPane

有关详细信息,请参阅How to use root panes