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

时间:2012-08-08 18:37:25

标签: java animation jpanel

我正在为我正在制作的私人游戏做一个小动画/ 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)
    );

2 个答案:

答案 0 :(得分:1)

它们是合适的,但不是Component,而是 contentPane JPanel.getContentPane()这是您JPanel的唯一组成部分。

不确定。

答案 1 :(得分: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>?)