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

Java 如何设置运行应用程序的JProgressbar,java,swing,Java,Swing,我的应用程序运行大约7秒 我想做一个进度条,但我不知道怎么做 你能帮我吗?基本上,它很简单,假设应用程序有5个方法,它们一个接一个地执行。在每个方法完成后,您可以将条形图的进度设置为+20% public class App { public void method_1(){ // method code progressbar.setValue(20); } public void method_2(){ // meth

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

我想做一个进度条,但我不知道怎么做


你能帮我吗?

基本上,它很简单,假设应用程序有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

它可能是在一个特定的
线程期间
或者当一个线程完成时,您甚至可以将其重置为0,然后在新线程启动时重新启动。这一切取决于您。

基本上,它很简单,假设应用程序有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

它可能是在一个特定的
线程期间
或者当一个线程完成时,您甚至可以将其重置为0,然后在新线程启动时重新启动。这一切取决于您。

基本上,它很简单,假设应用程序有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

它可能是在一个特定的
线程期间
或者当一个线程完成时,您甚至可以将其重置为0,然后在新线程启动时重新启动。这一切取决于您。

基本上,它很简单,假设应用程序有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
它可能是在一个特定的
线程期间
或者当一个线程完成时,您甚至可以将其重置为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...
一种方法(有点愚蠢)是在代码中多次使用
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...
一种方法(有点愚蠢)是在代码中多次使用
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...
一种方法(有点愚蠢)是在代码中多次使用
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...

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

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

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

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

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

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

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

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

您是否有一些代码来显示您到目前为止所尝试的内容?是否需要3k行代码?我想做的一切就是在应用程序执行之前用进度条显示一个小窗口,我想如果你知道怎么做的话,你不需要看到任何代码:)不,不需要。(@3k行代码),但要让进度条显示准确的值,我想运行应用程序需要“大约7秒”以上的时间。您必须查看应用程序中正在进行的流程,并相应地更新progressbar。这不是一个问题,而是一个请求。这就是为什么@WonderWorld希望您发布一些代码。如果你不能消除这个问题,并提供几行代码(绝对不是几千行)让我们看看你的项目是如何设计的,这是你的问题,而不是我们的问题。可能的重复你有一些代码来显示你迄今为止的尝试吗?你想要3k行代码吗?我想做的一切就是在应用程序执行之前用进度条显示一个小窗口,我想如果你知道怎么做的话,你不需要看到任何代码:)不,不需要。(@3k行代码),但要让进度条显示准确的值,我想运行应用程序需要“大约7秒”以上的时间。您必须查看应用程序中正在进行的流程,并相应地更新progressbar。这不是一个问题,而是一个请求。这就是为什么@WonderWorld希望您发布一些代码。如果你不能消除这个问题,并提供几行代码(绝对不是几千行)让我们看看你的项目是如何设计的,这是你的问题,而不是我们的问题。可能的重复你有一些代码来显示你迄今为止的尝试吗?你想要3k行代码吗?我想做的一切就是在应用程序执行之前用进度条显示一个小窗口,我想如果你知道怎么做的话,你不需要看到任何代码:)不,不需要。(@3k行代码),但要让进度条显示准确的值,我想运行应用程序需要“大约7秒”以上的时间。您必须查看应用程序中正在进行的流程,并相应地更新progressbar。这不是一个问题,而是一个请求。这就是为什么@WonderWorld希望您发布一些代码。如果你不能消除这个问题,并提供几行代码(绝对不是几千行)让我们看看你的项目是如何设计的,这是你的问题,而不是我们的问题。可能的重复你有一些代码来显示你迄今为止的尝试吗?你想要3k行代码吗?我想做的一切就是在应用程序执行之前用进度条显示一个小窗口,我想如果你知道怎么做的话,你不需要看到任何代码:)不,不需要。(@3k行代码),但要让进度条显示准确的值,我想运行应用程序需要“大约7秒”以上的时间。您必须查看应用程序中正在进行的流程,并相应地更新progressbar。这不是一个问题,而是一个请求。这就是为什么@WonderWorld希望您发布一些代码。如果你不能消除这个问题并提供几行代码(绝对不是几千行)让我们看看你的项目是如何设计的,那是你的问题,不是我们的问题。可能是尼斯的重复,我不知道那个特性。尼斯,我不知道那个特性。尼斯,我不知道那个特性。尼斯,我不知道那个特性。尼斯,我不知道那个特性。