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]
見How to use root panes,我留下問自己,如果你明白'super' d oes - 從我能告訴'Panel2'永遠不會添加到任何東西(btw,在提供的上下文中'this.getParent()'和'super.getParent()'做同樣的事情) – MadProgrammer
考慮提供一個[如何創建一個最小的,完整的和可驗證的例子](http://stackoverflow.com/help/mcve) - 這種方式我們沒有四處奔波,試圖猜測你的問題 – MadProgrammer
嗯,有些事情我不明白,但我沒有命名任何東西null.layeredPane – Denizen