睡眠造成具有隐形内容的框架

时间:2017-08-24 04:15:23

标签: java swing sleep jdialog jmenubar

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

但与此不同的是,我收到了空信息(jdialog包含隐藏内容):

enter image description here

如果删除关闭消息,其内容将在休眠时间后显示。

我需要改变什么来获得正确的结果?

我想得到这个:

enter image description here

完整的工作代码:

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

        }
    }
}

2 个答案:

答案 0 :(得分:1)

你有线程问题:
sleep是在AWT的Event Dispatch Thread上执行的,它执行AWT和Swing中的所有事件处理。因此,当您单击菜单项时,它会为Message创建对象,但会遇到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();
   }
}

答案 1 :(得分:0)

使用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();

    }
}