2014-05-20 2 views
1
public void actionPerformed(ActionEvent evt) 
{ 
    if(combo1==true && !evt.getActionCommand().equals("correct")) 
    { 
    combo2 = true; 
    combo1 = false; 
    combo3 = false; 
    outText.setText("One more try!"); 
    } 
    else if(combo2==true && !evt.getActionCommand().equals("correct")) 
    { 
    combo1 = false; 
    combo2 = false; 
    combo3 = true; 

    **outText.setText("Wrong!!");** 

     //sleep for 1 second 
    try { 
      Thread.currentThread(); 
      Thread.sleep(1000); 
     } 
    catch (Exception ex) 
    { 
    System.exit(0); 
    } 

    System.exit(0); 
    } 
    else if(!evt.getActionCommand().equals("correct")) 
    { 
    combo1=true; 
    combo2=false; 
    combo3=false; 
    outText.setText("Two more tries!"); 
    } 
    else 
    outText.setText("Correct"); 
} 

이것은 올바른 대답을 얻으려는 내 코드의 일부입니다. 그러나 **로 표시된 줄은 거기에있는 수면 방법으로 작동하지 않습니다. 프로그램이 잠자기 후 닫히지 만 "틀린 !!" 잠들기 전에. 누군가가 나를 도울 수 있다면 그것은 대단히 감사하게 될 것입니다! 미리 감사드립니다.gui에서 수면 방법을 사용하면 작동하지 않습니다.

+0

에서 자세한 내용을 참조하십시오? 오류가 있습니까? 'outText.setText ("한번 더 해보십시오!");'전혀 작동합니까? –

+0

"Wrong !!"이 인쇄되지 않았습니다. 다른 모든 것들이 잘 작동했다. 하지만 이제는 해결되었습니다. 어쨌든 고마워요! – hotkoreanchick

답변

1

Thread.sleep()으로 실행 완료 방법을 차단하고 있습니다. 대신 javax.swing.Timer`를 사용하십시오.

outText.setText("Wrong!!"); 
new javax.swing.Timer(1000, new ActionListener(){ 
    public void actionPerformed(ActionEvent e) { 
     System.exit(0); 
    } 
}).start(); 

// get rid of Thread.sleep() 

그래서 * *이 인쇄 무엇 How to Use Swing Timers

+0

그게 완벽 해. 정말 고맙습니다!! – hotkoreanchick

관련 문제