2011-05-11 33 views
3

幾秒鐘後消失一個普通的消息框,我不知道什麼是使所顯示的秒一組量後的JOptionPane風格樸素消息框消失的最好方法。Java創建

我想從主GUI線程啓動一個單獨的線程(它使用一個定時器)來做到這一點,以便主GUI可以繼續處理其他事件等。但是,我怎麼實際上使消息框在這個單獨的線程中消失並正確地終止線程。謝謝。

編輯:所以這是我想出用以下解決方案下面貼

package util; 

import java.awt.BorderLayout; 
import java.awt.Dimension; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.SwingConstants; 
import javax.swing.Timer; 

public class DisappearingMessage implements ActionListener 
{ 
    private final int ONE_SECOND = 1000; 

    private Timer timer; 
    private JFrame frame; 
    private JLabel msgLabel; 

public DisappearingMessage (String str, int seconds) 
{ 
frame = new JFrame ("Test Message"); 
msgLabel = new JLabel (str, SwingConstants.CENTER); 
msgLabel.setPreferredSize(new Dimension(600, 400)); 

timer = new Timer (this.ONE_SECOND * seconds, this); 
// only need to fire up once to make the message box disappear 
timer.setRepeats(false); 
} 

/** 
* Start the timer 
*/ 
public void start() 
{ 
// make the message box appear and start the timer 
frame.getContentPane().add(msgLabel, BorderLayout.CENTER); 
frame.pack(); 
frame.setLocationRelativeTo(null); 
frame.setVisible(true); 

timer.start(); 
} 

/** 
* Handling the event fired by the timer 
*/ 
public void actionPerformed (ActionEvent event) 
{ 
// stop the timer and kill the message box 
timer.stop(); 
frame.dispose(); 
} 

public static void main (String[] args) 
{ 
DisappearingMessage dm = new DisappearingMessage("Test", 5); 
dm.start(); 
} 
} 

現在的問題是,正如我CAM要在整個交互過程中創建此類的多個實例在用戶和主GUI之間,我想知道dispose()方法是否每次都能正確地清理所有內容。否則,我可能會在內存中累積大量冗餘對象。謝謝。

+0

http://download.oracle.com/javase/6/docs/api/java/awt/Window.html#dispose \(\),我應該認爲消息框中涉及的所有實例都將被清理? – skyork 2011-05-11 16:53:31

+0

你的配置文件說什麼? – trashgod 2011-05-11 17:55:00

+0

...方法最終確定 – mKorbel 2011-05-11 21:39:04

回答

3

我想在你的情況下,你不能使用任何的JOptionPane靜態方法(showX...)。您必須創建一個JOptionPane實例,然後創建一個JDialog並自己顯示JDialog。一旦你有JDialog,你可以強制其知名度。

// Replace JOptionPane.showXxxx(args) with new JOptionPane(args) 
JOptionPane pane = new JOptionPane(...); 
final JDialog dialog = pane.createDialog("title"); 
Timer timer = new Timer(DELAY, new ActionListener() { 
    public void actionPerformed(ActionEvent e) { 
     dialog.setVisible(false); 
     // or maybe you'll need dialog.dispose() instead? 
    } 
}); 
timer.setRepeats(false); 
timer.start(); 
dialog.setVisible(true); 

我還沒有嘗試過,所以我不能保證它的作品,但我認爲它應該;-)

當然,這裏Timerjavax.swing.Timer,因爲別人已經提到的,因此你'確定這個行動將在美國東部時間運行,你不會有任何創建或終止你自己Thread的問題。

+0

所以,爲了確認,在屏幕上顯示JOptionPane實例的DELAY時間,主GUI在這種情況下仍然是響應式的?另外,當對話消失(被處置)時,主GUI線程不受任何影響?謝謝。 – skyork 2011-05-11 16:12:10

+0

+1同樣的解決方案,我想出了。 – mre 2011-05-11 16:15:18

+0

+1不錯的解決方案。 – Boro 2011-05-11 16:30:53

1

Timers有自己的線程。我認爲你應該做的是創建一個新的Timer(或者,最好是讓你重複使用,直到你不再需要它),安排一個任務,要求消息框消失,然後完成這個任務add another task to the event queue,它將刪除消息框。

有可能是雖然更好的方法。另外: 是的,使用javax.swing.timer可能會更好。我在上面談到使用兩個任務的原因是我假設你將不得不在AWT線程中執行隱藏方法,以避免可能出現的某些細微的種族問題。如果你使用javax.swing.Timer,你已經在AWT線程中執行了,所以這一點變得沒有意義。

+0

我相信你的意思是'javax.swing.Timer',對嗎? – mre 2011-05-11 14:49:45

+0

爲什麼這裏需要兩項任務?通過另一項任務「刪除消息框」是​​什麼意思? – skyork 2011-05-11 14:52:26