小编典典

如何在Java中延迟MessageDialogBox?

java

因此,在这段代码中:

//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的放置是否正确,或者是否可以将消息延迟到该方法完成处理之前?


阅读 209

收藏
2020-11-26

共1个答案

小编典典

您可以使用SwingWorker

在这里看看Java教程

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);
    }
};
2020-11-26