Re: [J2SE] Jdialog的的dispose

看板java作者 (理想絕非空想)時間17年前 (2008/03/12 17:17), 編輯推噓1(102)
留言3則, 2人參與, 最新討論串5/6 (看更多)
依照 Swing 的規格,所有變更 Component 顯示狀態的動作, 皆需要在 Event Dispatch Thread 中執行。 這是為了達到 Thread safe 。 所以正確的作法應當是: JProgressBar bar = new JProgressBar(); bar.setIndeterminate(true); final JDialog dialog = new JDialog(); dialog.setModal(true); dialog.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); dialog.add(bar, BorderLayout.CENTER); dialog.add(new JLabel("請耐心等候..."), BorderLayout.NORTH); dialog.pack(); dialog.setLocationRelativeTo(null); SwingUtilities.invokeLater(new Runnable() { public void run() { dialog.setVisible(true); } }); try { // 在此做資料探勘 Thread.sleep(9999999); } catch (InterruptedException e) { } SwingUtilities.invokeLater(new Runnable() { public void run() { dialog.setVisible(false); } }); dialog.dispose(); 我想這應該能解決你遇到的問題。 Pao -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 163.18.62.71 ※ 編輯: pao0111 來自: 163.18.62.71 (03/12 17:19) ※ 編輯: pao0111 來自: 163.18.62.71 (03/12 17:21)

03/13 00:40, , 1F
不過 不是用 JProgressBar呀 還是感謝~
03/13 00:40, 1F

03/13 08:06, , 2F
這例子的JProgressBar只有作效果,拿掉也不影響的
03/13 08:06, 2F

03/13 08:08, , 3F
重點是在於SwingUtilities.invokeLater。
03/13 08:08, 3F
文章代碼(AID): #17rv-up0 (java)
文章代碼(AID): #17rv-up0 (java)