去除 JProgressBar 的不确定模式 [英] Removing the indeterminate mode of JProgressBar

查看:29
本文介绍了去除 JProgressBar 的不确定模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在按下开始按钮时启动我的进度条,当我的任务完成时,我调用了我试图停止进度条不确定模式的函数,但我仍然无法做到这一点(我是在我的应用程序中使用 SwingWorker)

I start my progress bar when the start button is pressed and when my task is completed, I call the function in which I am trying to stop the indeterminate mode of progress bar, but I am still unable to do that (I am using SwingWorker for my application)

这是我启动进度条的代码;这段代码写在开始按钮内:

Here is my code for starting a progress bar; this code is written inside start button:

private void StartButtonMouseClicked(java.awt.event.MouseEvent evt) {
    Main f22 = new Main();

    f2.getfile(FileName, 0);
    f2.execute();

    SwingUtilities.invokeLater(new Runnable()
    {
        @Override
        public void run()
        { 
            jProgressBar1.setIndeterminate(true);

        }
    });

这是函数内部的代码,一旦任务完成就会调用.

This is code inside the function, which is called once the task is completed.

jProgressBar1.setVisible(false);

推荐答案

请尝试通读这段代码,它是 JProgressBar 和 SwingWorker 的工作代码,一旦你理解了它的工作原理,就可以在你的方式.

Please try to go through this code, its a working code for JProgressBar with SwingWorker, then once you understand the working of it, feel free to implement it in your way.

 import java.awt.*;

 import java.awt.event.*;

 import java.util.List;

 import javax.swing.*;


 public class ProgressBarTest
 {
    public static void main(String[] args)
    {
       EventQueue.invokeLater(new Runnable()
          {
             public void run()
             {
                JFrame frame = new ProgressBarFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
             }
         });
    }
 }

 /**
 * A frame that contains a button to launch a simulated activity, a progress bar, and a
  * text area for the activity output.
  */
 class ProgressBarFrame extends JFrame
 {
    public ProgressBarFrame()
    {
       setTitle("ProgressBarTest");
       setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

       // this text area holds the activity output
       textArea = new JTextArea();

       // set up panel with button and progress bar
       final int MAX = 1000;
       JPanel panel = new JPanel();
       startButton = new JButton("Start");
       progressBar = new JProgressBar(0, MAX);
       progressBar.setStringPainted(true);
       panel.add(startButton);
       panel.add(progressBar);

       checkBox = new JCheckBox("indeterminate");
       checkBox.addActionListener(new ActionListener()
          {
             public void actionPerformed(ActionEvent event)
             {
                progressBar.setIndeterminate(checkBox.isSelected());
                progressBar.setStringPainted(!progressBar.isIndeterminate());
             }
          });
       panel.add(checkBox);
       add(new JScrollPane(textArea), BorderLayout.CENTER);
       add(panel, BorderLayout.SOUTH);

       // set up the button action

       startButton.addActionListener(new ActionListener()
         {
             public void actionPerformed(ActionEvent event)
             {
                startButton.setEnabled(false);
                activity = new SimulatedActivity(MAX);
                activity.execute();
             }
          });
    }

    private JButton startButton;
    private JProgressBar progressBar;
    private JCheckBox checkBox;
    private JTextArea textArea;
    private SimulatedActivity activity;

    public static final int DEFAULT_WIDTH = 400;
    public static final int DEFAULT_HEIGHT = 200;

    class SimulatedActivity extends SwingWorker<Void, Integer>
    {
       /**
        * Constructs the simulated activity that increments a counter from 0 to a
        * given target.
        * @param t the target value of the counter.
        */
       public SimulatedActivity(int t)
       {
          current = 0;
          target = t;
       }
       protected Void doInBackground() throws Exception
       {
         try
          {
             while (current < target)
             {
                Thread.sleep(100);
                current++;
                publish(current);
             }
          }
          catch (InterruptedException e)
          {
          }
          return null;
       }

       protected void process(List<Integer> chunks)
       {
          for (Integer chunk : chunks)
          {
             textArea.append(chunk + "\n");
             progressBar.setValue(chunk);
          }
       }

       protected void done()
       {
         startButton.setEnabled(true);
      }

      private int current;
       private int target;
    }
 }

这篇关于去除 JProgressBar 的不确定模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆