有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

用户界面Java GUI重绘()问题?

我有一个JFrame。此JFrame包含一个JButton。我单击JButton并创建10个JTextFields

问题是: 在通过调整窗口大小“我强制重新绘制()”之前,我无法看到它们。只有这样,我才能看到创建的JTextFields

代码:

JPanel points = new JPanel();

//Creating the JTextFields:
for (int i=0; i<10; i++) {
    JTextField textField = new JTextField();
    points.add(textField);
}

repaint();
this.repaint();
super.repaint();
points.repaint();

谢谢-在for循环之后,我刚刚调用了points。validate()成功了


共 (1) 个答案

  1. # 1 楼答案

    API文件说:

    Note: If a component has been added to a container that has been displayed, validate must be called on that container to display the new component. If multiple components are being added, you can improve efficiency by calling validate only once, after all the components have been added.

    这很晦涩,也不是很聪明,但这是规则。最好打电话给JComponent.revalidate