为什么没有验证Tree有效?

时间:2014-08-11 21:52:11

标签: 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();
    }

我尝试过使用" synchronized"方法,使用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。:这段代码执行时会出现错误。

2 个答案:

答案 0 :(得分:1)

使用Thread.sleep(),因为它会冻结您的Swing应用程序。

相反,您应该使用javax.swing.Timer

有关更多信息和示例,请参阅Java教程How to Use Swing TimersLesson: Concurrency in Swing

答案 1 :(得分:0)

我觉得我没有给出足够的信息,或者我也是诺贝(我不知道),所以我要把所有给我提问的课程都放在这里(它是Jbutton),随着你给我的推荐而改变了(仍然没有工作):

private void AceptarActionPerformed(java.awt.event.ActionEvent evt) {                                        

    if(xy==true && Respuesta.getText().equals("verdadero"))
    {
        sound = java.applet.Applet.newAudioClip(getClass().getResource("/sonido/enter.wav"));
        sound.play();
        r1.setIcon(new ImageIcon(getClass().getResource("/imagenes2/escalera.png")));
        r2.setVisible(true);
        dialog.setText("<html>\n" +
                       "<font color='blue'><center><h4>¡BIEN HECHO!<br>ahora resuelve la siguiente<br>pista.</h4></center> </font>\n" +
                       "</html>");
        x=100;
        xy=false;
        xz=true;
    }
    else if(xz==true && Respuesta.getText().equals("falso"))
    {
        r2.setIcon(new ImageIcon(getClass().getResource("/imagenes2/escalera.png")));
        dialog.setText("<html>\n" +
                       "<font color='blue'><center><h4>¡GENIAL!<br>¡VAMOS A LA SIGUIENTE CÁMARA!</h4><h5>Haz click aquí</h5></center> </font>\n" +
                       "</html>");
        x=2;
    }
    else if(xy==true || xz==true)
    {
        mm1.setVisible(true);
        mm2.setVisible(true);
        mm1.repaint();
        mm2.repaint();
        sound = java.applet.Applet.newAudioClip(getClass().getResource("/sonido/monster.wav"));
        sound.play();
        sound1 = java.applet.Applet.newAudioClip(getClass().getResource("/sonido/grito.wav"));
        sound1.play();
        Timer timer1= new Timer(4000, new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent ae) {
                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
            }   
        });
        timer1.start();
        formulariogame over2=new formulariogame();
        over2.setVisible(true);
        this.dispose();
    }

}                                       

我需要的是,当timer1.starts()时,执行停止4秒,给程序刷新图像的时间。如果Timer方法不能满足我的需求,请给我另一个建议。如果你能把纠正的代码寄给我,我会非常感谢你。