[英]How to set up JProgressbar of running aplication
My application run's about 7 second's. 我的应用程序运行大约7秒钟。
I want to make a progres bar of that but I don't know how. 我想为此做一个进度条,但我不知道如何做。
Could you help me ? 你可以帮帮我吗 ?
you should use the progress bar as an indeterminated mode. 您应该将进度条用作不确定模式。
You can use something like this: 您可以使用如下形式:
private void dialogWaiting()
{
JProgressBar progressBar = new JProgressBar();
progressBar.setIndeterminate(true);
JTextArea msgLabel;
final JDialog dialogWaiting;
JPanel panel;
msgLabel = new JTextArea("Please wait...");
msgLabel.setEditable(false);
panel = new JPanel(new BorderLayout(5, 5));
panel.add(msgLabel, BorderLayout.PAGE_START);
panel.add(progressBar, BorderLayout.CENTER);
panel.setBorder(BorderFactory.createEmptyBorder(11, 11, 11, 11));
dialogWaiting = new JDialog(Frame.getFrames()[0], "Doing whatever", true);
dialogWaiting.getContentPane().add(panel);
dialogWaiting.setResizable(false);
dialogWaiting.pack();
dialogWaiting.setSize(300, dialogWaiting.getHeight());
dialogWaiting.setLocationRelativeTo(null);
dialogWaiting.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
dialogWaiting.setAlwaysOnTop(true);
msgLabel.setBackground(panel.getBackground());
SwingWorker worker = new SwingWorker() {
@Override
protected void done() {
// Close the dialog
dialogWaiting.dispose();
}
@Override
protected Void doInBackground() {
// Do the long running task here
// put all the code you want to run as the dialogWaiting is on
return null;
}
};
worker.execute();
dialogWaiting.setVisible(true);
}
Basicly it's as simple as, lets say the application has 5 methods that are executed after another. 基本上它很简单,可以说应用程序有5种方法,这些方法会陆续执行。 You could set the progress of the bar to +20% after each method has finished. 每种方法完成后,您可以将进度条的进度设置为+ 20%。
public class App {
public void method_1(){
// method code
progressbar.setValue(20);
}
public void method_2(){
// method code
progressbar.setValue(40);
}
public void method_3(){
// method code
progressbar.setValue(60);
}
// etc
It could be during a specific Thread
or maybe when a thread has finished, you could even reset it to 0 and start again when a new Thread has started. 可能是在特定Thread
期间,也可能是在线程完成时,您甚至可以将其重置为0,然后在新线程启动时重新启动。 That all upto you. 全部由您决定。
One way to do it (which is a little stupid) is to use the setValue()
method various times in your code. 一种实现方法(有点愚蠢)是在代码中多次使用setValue()
方法。 Example: 例:
void main(String[]args){
//assuming that you have imported the progressbar class and have instantiated it
//with the variable name "jp"
System.out.println();//simulating some work...
jp.setValue(10);
System.out.println()//simulating some work...
jp.setValue(20);
//you get the idea...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.