将组件添加到JPanel,屏幕上没有更改

问题描述:

我正在为我制作的私人游戏做一个小型动画/ xml文件创建编辑器。 我有一个JPanel,我添加了一些自定义组件,但我从来没有看到任何改变。将组件添加到JPanel,屏幕上没有更改

自定义组件:

public class BasicProprety extends JPanel { 
String key; 
String value; 

    public BasicProprety(String k, String v) { 
     key = k; 
     value = v; 
     JTextField keyField = new JTextField(k); 
     JTextField valueField = new JTextField(v); 
     valueField.setVisible(true); 
     keyField.setVisible(true); 
     this.add(keyField); 
     this.add(valueField); 
     this.setVisible(true); 
    } 
} 

我用一个静态方法来刷新当前帧的特性:

public static void refreshFrameProperties() { 
    Frame currentFrame = DataBank.getCurrentInstance().getCurrentFrameObj(); 
    BasicProprety millisecs = new BasicProprety("millisecondsTillNextFrame", String.valueOf(currentFrame.getMillisecondsToNextFrame())); 
    BasicProprety offsetX = new BasicProprety("offsetX", String.valueOf(currentFrame.getOffset().x)); 
    BasicProprety offsetY = new BasicProprety("offsetY", String.valueOf(currentFrame.getOffset().y)); 
    BasicProprety sizeX = new BasicProprety("sizeX", String.valueOf(currentFrame.getSize().x)); 
    BasicProprety sizeY = new BasicProprety("sizeY", String.valueOf(currentFrame.getSize().y)); 
    sizeX.revalidate(); 
    sizeY.revalidate(); 
    offsetY.revalidate(); 
    offsetX.revalidate(); 
    millisecs.revalidate(); 
    sizeX.repaint(); 
    sizeY.repaint(); 
    offsetY.repaint(); 
    offsetX.repaint(); 
    millisecs.repaint(); 
    ArrayList<BasicProprety> basicList = new ArrayList<BasicProprety>(); 
    for (int i = 0; i < mainInterface.getProperties().getComponentCount(); i++) { 
     if (mainInterface.getProperties().getComponent(i) instanceof BasicProprety) 
      basicList.add((BasicProprety)mainInterface.getProperties().getComponent(i)); 
    } 
    for (BasicProprety bp : basicList) { 
     mainInterface.getProperties().remove(bp); 
    } 
    mainInterface.getProperties().revalidate(); 
    mainInterface.getProperties().repaint(); 
    System.out.println("REMOVED: "+mainInterface.getProperties().getComponentCount()); 
    mainInterface.getProperties().getLayout().addLayoutComponent("TEST", sizeY); 
    mainInterface.getProperties().add(millisecs); 
    mainInterface.getProperties().add(offsetX); 
    mainInterface.getProperties().add(offsetY); 
    mainInterface.getProperties().add(sizeX); 
    mainInterface.getProperties().add(sizeY); 
    /*Random r = new Random(); 
    mainInterface.getProperties().setBackground(new Color(r.nextInt()));*/ 
    mainInterface.getProperties().revalidate(); 
    mainInterface.getProperties().repaint(); 
    mainInterface.getProperties().setVisible(true); 
    System.out.println("NO: "+mainInterface.getProperties().getComponentCount()); 

} 

我的组件与NetBeans界面编辑器创建,这是创建的代码:

properties = new javax.swing.JPanel(); 
    org.jdesktop.layout.GroupLayout propertiesLayout = new org.jdesktop.layout.GroupLayout(properties); 
    properties.setLayout(propertiesLayout); 
    propertiesLayout.setHorizontalGroup(
     propertiesLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 
     .add(0, 243, Short.MAX_VALUE) 
    ); 
    propertiesLayout.setVerticalGroup(
     propertiesLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 
     .add(0, 386, Short.MAX_VALUE) 
    ); 
+0

无关,但'BasicProprety'拼写错误。在未来可能会有所帮助,以纠正它。 – oldrinb 2012-08-08 18:40:30

+0

Yeppe,谢谢我发现该类的自动完成问题,谢谢你,我知道为什么! – 2012-08-08 18:41:45

+0

您可能还希望将'mainInterface.getProperties()'保存在局部变量中,以便让代码更易于遵循,例如'... properties.revalidate(); properties.repaint(); ......“vs你现在拥有的东西。 – oldrinb 2012-08-08 18:46:58

它们被合理添加,但不会添加到您的Component而是contentPaneJPanel.getContentPane()这是您的JPanel的唯一组件。

虽然不确定。

+0

我刚试过,ContentPane只有3?!?而不是5个子组件,但数字永远不会改变,虽然我删除所有之前添加新的。 – 2012-08-08 18:53:20

组件数错误是您正在检查mainInterface.getComponentCount()而不是mainInterface.getProperties().getComponentCount()。想必mainInterface只包含你的JPanel

您可以通过使用局部变量来避免类似的错误,例如,

final JPanel properties = mainInterface.getProperties(); 
... 
System.out.println("number of properties' components: " 
         + properties.getComponentCount()); 

顺便说一句,可以简化下面的循环......

ArrayList<BasicProprety> basicList = new ArrayList<BasicProprety>(); 
for (int i = 0; i < mainInterface.getProperties().getComponentCount(); i++) { 
    if (mainInterface.getProperties().getComponent(i) instanceof BasicProprety) 
     basicList.add((BasicProprety)mainInterface.getProperties().getComponent(i)); 
} 
for (BasicProprety bp : basicList) { 
    mainInterface.getProperties().remove(bp); 
} 

...这个:

for (Component component : properties.getComponents()) { 
    if (component instanceof BasicProperty) { 
    properties.remove(component); 
    } 
} 

确保所有的UI修改是在事件分派线程的上下文中完成的。

(PS没有任何人认为Container应该公开的Iterator<Component>不知何故?)

+0

我刚刚试过,我的组件被一直添加到正确的位置,但屏幕上没有出现任何组件 – 2012-08-08 18:54:46

+0

您并未迭代Container的内部列表,用于其子组件; 'properties.getComponents()'返回Component []中组件的副本......不用担心! – oldrinb 2012-08-08 18:57:01

+0

好的,感谢您的精确度 – 2012-08-08 18:58:58