Java 具有不可见内容的导致睡眠的帧

Java 具有不可见内容的导致睡眠的帧,java,swing,sleep,jdialog,jmenubar,Java,Swing,Sleep,Jdialog,Jmenubar,我有一个带有菜单栏菜单的框架,当我选择菜单项时,我希望程序显示一条消息,然后几秒钟后(通过睡眠功能)自动关闭该消息 但与此相反,我得到的是一条空消息(带有不可见内容的jdialog): 如果删除关闭消息,它的内容将在睡眠时间后出现 我需要改变什么才能得到正确的结果 我想得到这个: 完整工作代码: import java.awt.Dimension; import java.awt.GridLayout; import java.awt.event.ActionEvent; import ja

我有一个带有菜单栏菜单的框架,当我选择菜单项时,我希望程序显示一条消息,然后几秒钟后(通过睡眠功能)自动关闭该消息

但与此相反,我得到的是一条空消息(带有不可见内容的jdialog):

如果删除关闭消息,它的内容将在睡眠时间后出现

我需要改变什么才能得到正确的结果

我想得到这个:

完整工作代码:

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.util.concurrent.TimeUnit;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;

public class MessageSleepTest extends JDialog {

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable(){
            public void run(){
                new Frame("frame with menubar");
            }
        });
    }
}

class Message extends JDialog {

    public Message() {
        this.setLayout(new GridLayout(0, 1));
        this.add(new JLabel("Displaying this message for 3 seconds and then closing it...", JLabel.CENTER));

        this.setAlwaysOnTop(true);
        this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        this.pack();
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }
}

class Frame extends JFrame{

    public Frame(String title){

        super(title);
        setJMenuBar(new MenuBar());
        setPreferredSize(new Dimension(500, 300));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setLocationRelativeTo(null); // center the frame
        setVisible(true);

    }

}

class MenuBar extends JMenuBar implements ActionListener{
    public static JMenuItem itmOpen;

    public MenuBar() {
        JMenu menuFile = new JMenu("File");

        itmOpen = new JMenuItem("Open...");
        itmOpen.addActionListener(this);

        add(menuFile);
        menuFile.add(itmOpen);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        JMenuItem source = (JMenuItem)e.getSource();

        if(source == itmOpen){

            JDialog message = new Message();
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
            WindowEvent windowClosing = new WindowEvent(message, WindowEvent.WINDOW_CLOSING);
            message.dispatchEvent(windowClosing);

        }
    }
}

您有线程问题:
sleep
是在AWT的事件调度线程上执行的,它完成AWT和Swing中的所有事件处理工作。因此,当您单击菜单项时,它会为
消息
创建对象,但会被
setVisible()
方法卡住,该方法通常会向JDialog发送一个事件以进行布局和显示。此消息进入EDT队列,在事件处理程序(执行的
actionPerformed
方法)完成后,将被处理。然而,睡眠是介于两者之间的

因此,请尝试以下方法:

@Override
public void actionPerformed(ActionEvent e) {
   JMenuItem source = (JMenuItem) e.getSource();
   if (source == itmOpen) {
      final JDialog message = new Message();
      new Thread( new Runnable() {
         @Override
         public void run() {
            try {
               TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException ex) {
               // Do nothing with it
            }
            WindowEvent windowClosing = new WindowEvent(message, WindowEvent.WINDOW_CLOSING);
            message.dispatchEvent(windowClosing);
         }
      }).start();
   }
}

使用swing.Timer的另一个解决方案

@Override
public void actionPerformed(ActionEvent e) {
    JMenuItem source = (JMenuItem)e.getSource();

    if(source == itmOpen){

        JDialog message = new Message();
        Timer timer = new Timer(1000 * 3, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                WindowEvent windowClosing = new WindowEvent(message, WindowEvent.WINDOW_CLOSING);
                message.dispatchEvent(windowClosing);
            }
        });
        timer.setRepeats(false);
        timer.start();

    }
}

非常感谢您的解释!你的方法奏效了。