Java GUI 重绘()问题? [英] Java GUI repaint() problem?
问题描述
我有一个 JFrame.这个 JFrame 包含一个 JButton.我单击 JButton 并创建了 10 个 JTextField.
I have a JFrame. This JFrame contains a JButton. I click the JButton and 10 JTextFields are created.
问题:在通过调整窗口大小强制重绘()"之前,我无法看到它们.只有这样我才能看到创建的 JTextFields.
the problem: I cannot see them until "I force a repaint()" by resizing the window. Only then do I see the JTextFields created.
代码:
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() 并且它起作用了......
THANK YOU - after the for loop, I just called points.validate() and it worked...
推荐答案
Container.add
API 文档说:
Container.add
API docs sayeth:
注意:如果一个组件已经添加到已显示的容器,必须调用验证容器来显示新的零件.如果多个组件正在添加,您可以改进仅通过调用验证来提高效率一次,在所有组件都具有已添加.
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
It's obscure and not very clever, but it's the rules. It may be better to call JComponent.revalidate
这篇关于Java GUI 重绘()问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!