[英]How show my application delay with JProgressBar?
我寫了一個簡單的應用程序,我想用JProgressBar Plese顯示它的延遲;
我想用Joptionpane顯示JProgressBar,並帶有一個取消按鈕,它應該是模態的
這是我的源代碼:
class CustomFrame extends JFrame {
private JProgressBar progressBar;
public CustomFrame() {
long start = System.currentTimeMillis();
myMethod();
this.getContentPane().setLayout(null);
this.setSize(200, 200);
//JOptionPane. ?????
this.setTitle("JFrame");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
long end = System.currentTimeMillis();
System.out.print("\nTime: " + (end - start));
}
public void myMethod(){
try {
java.io.File file = new java.io.File("i://m.txt");
BufferedReader input =
new BufferedReader(new FileReader(file));
String line;
while ((line = input.readLine()) != null) {
if (line.indexOf("CREATE KGCGI=") != -1 ){
System.out.println(line);
}
}
input.close();
}
catch(Exception e){
e.printStackTrace();
}
}
謝謝 ...
要使它正常工作,您需要做幾件事:
file.length()
),以確定如何調整進度條的范圍( myProgressBar.setMaximum(length)
) myProgressBar.setValue(myProgressBar.getValue()+lineLength)
,將需要觸發對進度條的更新( myProgressBar.setValue(myProgressBar.getValue()+lineLength)
)。 批評的幾點:
init()
方法的調用中進行。 JFrame
作為超類正在做什么。 JOptionPane
是一個類,它將彈出一個非常基本的模態對話框,其中包含一些文本,可能是圖標或輸入字段。 它不是嵌入在對話框中的面板。 JDialog
(也可以使其成為模式)可能會更容易。 JDialog
允許您JDialog
添加按鈕,而作為獨立JOptionPane
則要求您使用“是/否”或“是/否/取消”或“確定/取消”等。 JOptionPane
,並且僅顯示取消按鈕,則可以實例化JOptionPane
(與使用實用程序show*
方法相對),並使用進度條作為message
,並將JOptionPane.CANCEL_OPTION
用作optionType
參數。 您仍然需要將其放入JDialog
以使其可見。 有關更多詳細信息,請參見本教程 : JOptionPane(構造函數)
使用指定的按鈕,圖標,消息,標題等創建JOptionPane。 然后,您必須將選項窗格添加到JDialog,在選項窗格上注冊屬性更改偵聽器,然后顯示對話框。 有關詳細信息,請參見停止自動關閉對話框。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.