python消息框显示几秒后自动关闭_创建一个简单的消息框,在Java中几秒后消失...

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.

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.

Edit: 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();

}

}

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.

解决方案

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

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值