我能够自己弄清楚从用户那里获取输入并显示最终产品,但我想添加一条“忙碌”消息,该消息在代码中的某个点显示,然后在代码中的某个点关闭。
希望通过在我的代码中显示它会更容易解释。这是一个排序算法,排序时间显然会有所不同。
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/