幾秒鐘後消失一個普通的消息框,我不知道什麼是使所顯示的秒一組量後的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()方法是否每次都能正確地清理所有內容。否則,我可能會在內存中累積大量冗餘對象。謝謝。
http://download.oracle.com/javase/6/docs/api/java/awt/Window.html#dispose \(\),我應該認爲消息框中涉及的所有實例都將被清理? – skyork 2011-05-11 16:53:31
你的配置文件說什麼? – trashgod 2011-05-11 17:55:00
...方法最終確定 – mKorbel 2011-05-11 21:39:04