如何在Java中延迟MessageDialogBox?

所以在这段代码中:

//Actions performed when an event occurs.
    public void actionPerformed(ActionEvent event) 
    {
        String command = event.getActionCommand();

        //If btnConvertDocuments is clicked, the FileConverter method is called and the button is then disabled [so as to prevent duplicates].
        if (command.equals("w"))
        {
            new Thread(new Runnable() 
            {
                public void run() 
                {
                    FileConverter fc = new FileConverter();

                }
             }).start();
            btnConvertDocuments.setEnabled(false);
            //Validation message ensuring completion of the step.
            JOptionPane.showMessageDialog(this, "Step 1 Complete!", "Validation", JOptionPane.INFORMATION_MESSAGE);
        }

在FileConverter方法甚至没有被调用之前,似乎消息对话框窗口弹出方式太快了.我想知道JOptionPane的位置是否正确,或者是否有办法延迟消息直到方法完成处理?

解决方法:

你可以使用SwingWorker.

看看这里,java tutorial.

SwingWorker worker = new SwingWorker<Void, Void>() {
    @Override
    public Void doInBackground() {
        FileConverter fc = new FileConverter();
        return null;
    }

    @Override
    public void done() {
        JOptionPane.showMessageDialog(this, "Step 1 Complete!", "Validation", JOptionPane.INFORMATION_MESSAGE);
    }
};
上一篇:java – 带有JTextArea的JOptionPane而不是文本字段?


下一篇:java – JScrollPane中的JTextArea包装单词但缺少字母