[英]Jframe dispose on condition
這是我的代碼:
public Loading(Evento e){
initComponents();
setLocationRelativeTo(null);
this.evento = e;
new Thread(new thread1()).start();
}
public class thread1 implements Runnable{
@Override
public void run(){
for (int i=0; i<=100; i++){ //Progressively increment variable i
jpb.setValue(i); //Set value
jpb.repaint(); //Refresh graphics
try{
Thread.sleep(30);
if(i==100){
GestaoEvento a = new GestaoEvento(evento);
a.setVisible(true);
--->>>>this.dispose();<<<<---
}
} //Sleep 50 milliseconds
catch (InterruptedException err){}
}
}
}
this.dispose()
在那里不起作用因為this
引用了線程,我怎么能在那里關閉這個框架?
與您的問題相關 : this
是指內部類的實例。 你必須使用Loading.this.dispose()
。
最重要的
調用jpb.repaint();
在另一個不是EDT的線程中可能會導致意外行為,並調用Thread.sleep(30);
會凍結你的gui。 而是使用SwingTimer
閱讀更多: 如何使用SwingTimers | Swing中的並發性
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.