0
JPanel을 생성하고이를 부모에 추가하는 확장 JPanel 클래스가 있습니다. 부모로부터 호출 getComponents 올바르게 패널을 보여 주지만, 확장 된 인스턴스에서 호출의 getParent는 출력이되지 않습니다 "null.layeredPane"로 부모 이름을 표시 null.layeredPane확장 된 JPanel 부모가 null입니다.
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);
}
}
을 반환 예상했던 것.
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
[How to create (http://stackoverflow.com/help/mcve) - 우리는 당신의 문제를 짐작하려고 노력하고 있지 않다. – MadProgrammer
글쎄, 내가 이해하지 못하는 일이있다. 하지만 아무것도 null.layeredPane 이름을 지정하지 않았습니다 – Denizen