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