java - 向 Java 程序添加一个在特定点打开和关闭的消息框

标签 java swing dialog runtime

我能够自己弄清楚从用户那里获取输入并显示最终产品,但我想添加一条“忙碌”消息,该消息在代码中的某个点显示,然后在代码中的某个点关闭。

希望通过在我的代码中显示它会更容易解释。这是一个排序算法,排序时间显然会有所不同。

public static void main(String[] args) throws Exception{

        //Code that doesn't matter for this question...

        String userInput = JOptionPane.showInputDialog("Please enter the amount of input you would like to test (integer): ");

    // Code that doesn't matter for this question....

    //
    // Open dialogue box here that says, "Busy sorting" and stays open 
    // 
        long startTime = System.currentTimeMillis();
            sort(shorts);
        long endTime = System.currentTimeMillis();

    //
    // The dialogue box closes here without the user having to press anything
    //

    // This displays the final output.
            JOptionPane.showMessageDialog(null, error + "The runtime for " + input + " short integers is " + (endTime-startTime) + " milliseconds.", "Final Runtime", JOptionPane.PLAIN_MESSAGE);

        }

希望这个问题是可以理解的。

最佳答案

您需要使用 SwingWorker 类在 GUI 之外的单独线程上运行长任务。当它运行时,显示 JProgressBar 或类似的东西将是理想的。

http://java.sun.com/products/jfc/tsc/articles/threads/threads2.html

关于java - 向 Java 程序添加一个在特定点打开和关闭的消息框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6050751/

相关文章:

java - TextView 不会更改为新设置的文本

java - FlowLayout对象中的组件setSize方法

java - 更改未在 paint 方法中绘制的图形的颜色

c# - jQuery 对话框在 Response.TransmitFile 之后不会从代码隐藏中关闭

C# Windows 窗体 - 调整窗体大小时移动元素

java - 如何从Function接口(interface)返回Consumer

java - 如何拥有正确的关系基数?

Android 将数据从对话框传递到弹出对话框的屏幕

java - 在 Java 中播放游戏音乐 - 如何停止数据流?

java - 将文本排列到 JScrollPane 内的列中