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

最佳答案

它们被适当地添加,但不添加到您的Component中,而是添加到contentPane中。 JPanel.getContentPane()JPanel的唯一组成部分。

虽然不确定。

10-07 19:16
查看更多