创建一个简单的消息框,在Java中几秒后消失 [英] Create a plain message box that disappears after a few seconds in Java

查看:910
本文介绍了创建一个简单的消息框,在Java中几秒后消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道在显示设定的秒数后,使JOptionPane样式普通消息框消失的最佳方法是什么。

I wonder what is the best approach to make a JOptionPane style plain message box disappear after being displayed for a set amount of seconds.

我正在考虑启动一个单独的线程(使用一个计时器)从主GUI线程执行此操作,以便主GUI可以继续处理其他事件等。但是我如何实际使消息框在这个单独的线程中消失并正确终止线程。谢谢。

I am thinking to fire up a separate thread (which uses a timer) from the main GUI thread to do this, so that the main GUI can carry on processing other events etc. But how do I actually make the message box in this separate thread disappear and terminate the thread properly. Thanks.

编辑:所以我按照下面发布的解决方案提出了这个问题

so this is what I come up with by following the solutions posted below

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();
}
}

现在的问题是,我想去在用户和主GUI之间的交互过程中创建这个类的多个实例,我想知道dispose()方法是否每次都能正确清理所有内容。否则,我最终可能会在内存中累积大量冗余对象。谢谢。

Now the question is that, as i cam going to create multiple instances of this class throughout the course of the interaction between the user and the main GUI, I wonder whether the dispose() method cleans up everything properly every time. Otherwise, I may end up with accumulating lots of redundant objects in memory. thanks.

推荐答案

我认为在你的情况下,你不能使用任何 JOptionPane 静态方法( showX ... )。你必须创建一个 JOptionPane 实例,然后从中创建一个 JDialog 并显示 JDialog 你自己。一旦你有 JDialog ,就可以强制它的可见性。

I think in your situation, you can't use any of JOptionPane static methods (showX...). You have to create a JOptionPane instance instead, then create a JDialog from it and show that JDialog yourself. Once you have JDialog, you can force its visibility.

// 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);

我没有尝试过,所以我不能保证它有效,但我认为应该这样做; - )

I haven't tried it so I can't guarantee that it works but I think it should ;-)

当然,这里计时器 javax.swing.Timer ,正如其他人已经提到的那样,因此你确定该动作将在EDT中运行,你将不会有任何创建或终止你自己的线程。

Of course, here Timer is javax.swing.Timer, as someone else already mentioned, thus you're sure the action will run in the EDT and you won't have any problem with creating or terminating your own Thread.

这篇关于创建一个简单的消息框,在Java中几秒后消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆