我想在我的java程序中做的是,當我按下按鈕時,它會以時間間隔在textfield
中顯示文本。 即我按下按鈕然後jFrame
彈出,有一個標誌,顯示了類似的文字: 第1條第二:「第一次」 隨後的時滯說1秒 那麼第二件事:「第二屆」Java睡眠不工作在循環中
我是一個新手,我試圖谷歌這個問題,但我找不到解決方案,即使在3-4小時後 我嘗試了很多東西:睡覺,嘗試ñ趕上...
請寫出答案非常簡單。
這是我的代碼: 在下面的代碼中,當按下按鈕時,jFrame出現但屏幕中出現白色,並且當總和結束時,其屏幕變爲灰色,並顯示答案....
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
See.setVisible(true);//See is the JFrame
t007.setVisible(true);//Label in See
l2.setVisible(true);//TextField in See
int ran, g, d, col, ran2;
double y = 1000 * (Double.parseDouble(t2.getText()));
int x = (int) y;
d = 0;
double c = Math.pow(10, Integer.parseInt(t1.getText()));
col = Integer.parseInt(t3.getText());
for (g = 0; g < col;) {
ran = (int) (Math.random() * (c)); // Random Number Creation Starts
if (ran > (c/10)) {
g = g + 1;
ran2 = ((int) (Math.random() * 10)) % 2;
if (ran2 == 1) {
ran = ran * (-1);
}
d = d + ran;
if (d < 0) {
ran = ran * (-1);
d = d + (2 * ran);
}
l2.setVisible(true);
t007.setText("" + ran);
System.out.println("" + ran);
jButton6.doClick();
//Pausing (Sleep)
try {
Thread.sleep(x);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
}
l2.setText("" + d);
}
使用'SwingWorker'提供後臺處理和同步備份到事件隊列。參見[Swing中的併發(http://docs.oracle.com/javase/tutorial/uiswing/concurrency/)瞭解更多詳情 – MadProgrammer
不要堵塞EDT(事件指派線程) - 的圖形用戶界面將「凍結」時發生。而不是調用'Thread.sleep(n)'實現一個Swing'Timer'來重複執行任務,或者一個'SwingWorker'執行長時間運行的任務。有關更多詳細信息,請參見[Swing中的併發](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/)。 –
嘿,我不能理解你,你能不能簡單地寫下代碼,然後告訴我什麼和如何。請 –