Java GUI repaint() problem?

痴心易碎 提交于 2019-12-17 09:48:21

问题


I have a JFrame. This JFrame contains a JButton. I click the JButton and 10 JTextFields are created.

the problem: I cannot see them until "I force a repaint()" by resizing the window. Only then do I see the JTextFields created.

CODE:

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();

THANK YOU - after the for loop, I just called points.validate() and it worked...


回答1:


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.

It's obscure and not very clever, but it's the rules. It may be better to call JComponent.revalidate



来源:https://stackoverflow.com/questions/369823/java-gui-repaint-problem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!