0

我正在为我正在制作的私人游戏制作一个小型动画/xml 文件创建编辑器。我有一个 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)
    );
4

2 回答 2

1

它们被适当地添加,但不是添加到您的,而是添加到ComponentcontentPaneJPanel.getContentPane()这是您的JPanel.

不过不确定。

于 2012-08-08T18:42:12.187 回答
0

组件计数中的错误是您正在检查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>?)

于 2012-08-08T18:48:38.490 回答