有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

swing将消息框添加到Java程序中,该程序在某一点打开和关闭

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

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

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

        }

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


共 (5) 个答案

  1. # 1 楼答案

    正如我在问题中提到的,您应该查看swing教程。这真的很好,而且很容易理解

    JOptionPane是JavaSwing的一部分,不过,正如其他人所说,它可能不是显示繁忙状态的最佳方法。正如其他人所说,进度条可能是一个不错的选择,这取决于您的需要,但当您阅读本教程时,您将看到其他选项

    祝你好运

  2. # 3 楼答案

    我可能不会使用JOptionPane。因为在显示忙消息时,程序将不会执行。您最好使用文本字段和按钮创建一个更复杂的swing应用程序。当用户单击该按钮时,系统会在窗口底部显示忙碌消息