嗨,是否可以創建Java Swing JDialog
框(或替代Swing對象類型),以便我可以用來警告用戶某個事件,然後在延遲後自動關閉對話框; 沒有用戶不得不關閉對話框?我可以在Java Swing JDialog框上設置一個計時器,以在幾毫秒後關閉
10
A
回答
7
是的 - 當然可以。你有沒有試圖安排結束?
JFrame f = new JFrame();
final JDialog dialog = new JDialog(f, "Test", true);
//Must schedule the close before the dialog becomes visible
ScheduledExecutorService s = Executors.newSingleThreadScheduledExecutor();
s.schedule(new Runnable() {
public void run() {
dialog.setVisible(false); //should be invoked on the EDT
dialog.dispose();
}
}, 20, TimeUnit.SECONDS);
dialog.setVisible(true); // if modal, application will pause here
System.out.println("Dialog closed");
上述程序會在20秒後關閉對話框,你會看到文本「對話框關閉」打印到控制檯
3
我會用一個Swing計時器。定時器觸發時,代碼將自動在事件調度線程中執行,GUI的所有更新都應在EDT中完成。
閱讀Swing教程How to Use Timers中的部分。
14
該解決方案基於oxbow_lakes',但它使用了一個javax.swing.Timer,它用於這種類型的東西。它總是在事件分派線程上執行它的代碼。這對避免細微但討厭的錯誤很重要
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Test {
public static void main(String[] args) {
JFrame f = new JFrame();
final JDialog dialog = new JDialog(f, "Test", true);
Timer timer = new Timer(2000, new ActionListener() {
public void actionPerformed(ActionEvent e) {
dialog.setVisible(false);
dialog.dispose();
}
});
timer.setRepeats(false);
timer.start();
dialog.setVisible(true); // if modal, application will pause here
System.out.println("Dialog closed");
}
}
相關問題
- 1. 以編程方式關閉當前顯示的Java Swing JDialog框
- 2. 毫秒計時器在cocos2d
- 3. swing:在JDialog上設置光標
- 4. Swing:如何從控制器以編程方式關閉JDialog?
- 5. 我可以在幾臺計算機上設置一個git存儲庫嗎?
- 6. 內置EXE幾秒鐘後關閉
- 7. 時間以秒或毫秒
- 8. 爲什麼我的計時器在每毫秒和幾秒鐘內消失?
- 9. npm.cmd在幾秒後立即關閉
- 10. ACM1602B-FL-YBW在幾秒鐘後關閉
- 11. 跨平臺毫秒計時器持續49天以上?
- 12. 我可以爲.vbs文件設置一個計時器嗎?
- 13. TimePicker以毫秒爲單位倒計時
- 14. 時間以毫秒爲單位計算
- 15. NSTimer倒計時跳過上一秒(不使用最後毫秒)
- 16. 從幾毫秒到幾分鐘秒的JavaScript倒計時腳本
- 17. jQuery毫秒計時器?
- 18. VB - 可以在消息框中設置運行計時器嗎?
- 19. 毫秒計時器MATLAB
- 20. 我可以使用moment.js以毫秒超過24小時
- 21. Java用JDialog關閉另一個窗口
- 22. 計時器在C + + 11計時:從納秒到毫秒
- 23. Android:毫秒計時器?
- 24. 在計時器中顯示毫秒html
- 25. 在npgsql中設置超時毫秒
- 26. 倒計時器跳過一些毫秒
- 27. 以毫秒
- 28. 。用java啓動的.msi文件在幾秒鐘後關閉。
- 29. 爲什麼我的計時器以毫秒而不是秒爲單位運行?
- 30. 我可以在關閉設備時保持我的服務嗎?
您應該在事件分派線程上調用dialog.setVisisble(false)。否則,代碼行爲是不可預知的。 – 2009-08-20 15:55:12
這是非常真實的 - 我爲了混淆的理由而忽略了這一點 – 2009-08-20 16:26:40