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);
}
希望这个问题是可以理解的
# 1 楼答案
正如我在问题中提到的,您应该查看swing教程。这真的很好,而且很容易理解
JOptionPane是JavaSwing的一部分,不过,正如其他人所说,它可能不是显示繁忙状态的最佳方法。正如其他人所说,进度条可能是一个不错的选择,这取决于您的需要,但当您阅读本教程时,您将看到其他选项
祝你好运
# 2 楼答案
阅读Swing tutorrial中关于How to Use Progress Bars的部分,了解一些示例
或者您可能想尝试使用Disabled Glass Pane
# 3 楼答案
我可能不会使用JOptionPane。因为在显示忙消息时,程序将不会执行。您最好使用文本字段和按钮创建一个更复杂的swing应用程序。当用户单击该按钮时,系统会在窗口底部显示忙碌消息
# 4 楼答案
{a1}可能就是您正在寻找的
啊,是的,记住jzd关于Swing线程的重要评论You'll want to read up on that too
# 5 楼答案
您需要的是使用
SwingWorker
类在GUI的单独线程上运行长任务。在运行时,最好显示JProgressBar
或类似的内容http://java.sun.com/products/jfc/tsc/articles/threads/threads2.html