java - 为什么 validateTree 不起作用?

标签 java swing validation jframe

所以,我一直在寻找解决我的问题的方法,但它就是行不通。这是给我带来问题的代码:

else if(xy==true || xz==true)
    {
        mm1.setVisible(true);
        mm2.setVisible(true);
        mm1.repaint();
        mm2.repaint();
        SwingUtilities.updateComponentTreeUI(this);
        this.validateTree();
        sound = java.applet.Applet.newAudioClip(getClass().getResource("/sonido/monster.wav"));
        sound.play();
        sound1 = java.applet.Applet.newAudioClip(getClass().getResource("/sonido/grito.wav"));
        sound1.play();
        try {
            Thread.sleep(4000);
        } catch (InterruptedException ex) {
            Logger.getLogger(formulario2.class.getName()).log(Level.SEVERE, null, ex);
        }
        formulariogame over2=new formulariogame();
        over2.setVisible(true);
        this.dispose();
    }

我尝试使用“同步”方法,使用 repaint(),更改顺序,但我不断收到此消息:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: This function should be called while holding treeLock
at java.awt.Component.checkTreeLock(Component.java:1196)
at java.awt.Container.validateTree(Container.java:1682)
at nivel_2.formulario2.AceptarActionPerformed(formulario2.java:148)
at nivel_2.formulario2.access$100(formulario2.java:20)
at nivel_2.formulario2$2.actionPerformed(formulario2.java:93)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3311)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

我在源代码编辑器中没有收到任何错误,并且使用任何其他方法,JFrame 都不会刷新。我该怎么做才能让它发挥作用?

Pd.:当该代码块执行时就会出现错误。

最佳答案

不要不要使用 Thread.sleep(),因为它会卡住您的 Swing 应用程序。

您应该使用 javax.swing.Timer

请参阅 Java 教程 How to Use Swing TimersLesson: Concurrency in Swing了解更多信息和示例。

关于java - 为什么 validateTree 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25253027/

相关文章:

java - 如何在Java中处理JSONParser中不存在的数据

java - 删除除带有视频扩展名的文件之外的所有文件

java - 使用 GridBagLayout JComponent 未在 JPanel 上显示

java - 从 JFrame 程序创建 JApplet

java - MongoDB 使用 Spring Data Mongo 在集合中自动递增 Integer Id

java - 如何将 JLabel 和 JTextField 放在图像之上?

html - 密码必须与 Angular 中的响应式(Reactive)表单匹配

javascript - JQuery 验证失败后页面仍然回发

c# - 正则表达式只允许一个特殊字符?

java - 嵌套的 while 循环与条件不匹配