繁体   English   中英

如何设置运行应用程序的JProgressbar

[英]How to set up JProgressbar of running aplication

我的应用程序运行大约7秒钟。

我想为此做一个进度条,但我不知道如何做。

你可以帮帮我吗 ?

您应该将进度条用作不确定模式。
您可以使用如下形式:

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

基本上它很简单,可以说应用程序有5种方法,这些方法会陆续执行。 每种方法完成后,您可以将进度条的进度设置为+ 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

可能是在特定Thread期间,也可能是在线程完成时,您甚至可以将其重置为0,然后在新线程启动时重新启动。 全部由您决定。

一种实现方法(有点愚蠢)是在代码中多次使用setValue()方法。 例:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM