如何在某个进程结束后以编程方式关闭消息对话 - Java

时间:2018-03-18 11:05:43

标签: java swing

如何在某个功能的开头显示JOptionPane,例如,"工作...",然后在所述功能结束时以编程方式解除它/一段代码?

2 个答案:

答案 0 :(得分:0)

要回答你的问题,你可以运行这段代码:

//Declare the JOptionPane object
JOptionPane pane = new JOptionPane();

function yourFunction(){
     //Beginning
     pane.showInternalMessageDialog(frame, "Working...", "information", JOptionPane.INFORMATION_MESSAGE);

     //End
     pane.setVisible(false); //or pane.dispose();
}

可以通过在隐藏窗格时调用pane.dispose()方法来增强代码,以释放窗格使用的系统资源。

答案 1 :(得分:0)

每当弹出JOptionPane时,它就会在另一个帖子中弹出,所以你可以按照下面的说法进行操作:

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.util.Scanner;

public class Main
{
    private JOptionPane pane;
    private JDialog dialog;
    private Timer t;

    public Main()
    {
        t = new Timer(2000, closeJDialog);
        t.start();

        pane = new JOptionPane("My Dialog Message to You!");
        dialog = pane.createDialog("My Dialog Title");
        dialog.setVisible(true);
    }

    private ActionListener closeJDialog = new ActionListener() {
        public void actionPerformed(ActionEvent e)
        {
            if (dialog.isShowing()) {
                int i = new Scanner(System.in).nextInt();

                // Call your function here

                dialog.dispose();
                System.out.println("dialog disposed");
            }
        }
    };

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable() {
            public void run()
            {
                new Main();
            }
        });
    }
}