您好,欢迎来到[编程问答]网站首页   源码下载   电子书籍   软件下载   专题
当前位置:首页 >> 编程问答 >> Java >> 怎么在现有的程序中加入进度条功能及进度条如何实现

怎么在现有的程序中加入进度条功能及进度条如何实现

来源:网络整理     时间:2016/6/21 4:14:55     关键词:

关于网友提出的“ 怎么在现有的程序中加入进度条功能及进度条如何实现”问题疑问,本网通过在网上对“ 怎么在现有的程序中加入进度条功能及进度条如何实现”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:

问题: 怎么在现有的程序中加入进度条功能及进度条如何实现
描述:

现在的项目基本上做完了,但有些功能需要很长时间的计算,得等上一会儿,所以想做个进度条功能,向大虾们请教一下,如何实现进度条及如何将进度条加到我现在的程序中.我不太清楚怎么做,但大概是起一个新的线程,同时启动进度条.但不知道怎么实现,请大虾帮帮忙!


解决方案1:

/**
* @author xuwanhong_sun@hotmail.com 
* @version 1.0.0
*/
package org.mysoft.JSQGUI;
import java.awt.*;
import javax.swing.*;
import java.util.*;
public class WaitThread
{
    public static JDialog dialog;
    private Dimension dimensions = Toolkit.getDefaultToolkit().getScreenSize();
    private int width = dimensions.width * 3 / 10, height = 75;
    private Window parent;
    private String message;
    private ActionThread actionThread;
    private Map threads;
    
    public WaitThread(Window parent,String message)
    {
        this.parent = parent;
        this.message = message;
        initGUI();
    }
    public WaitThread(Window parent,String message,Map threads)
    {
        this.parent = parent;
        this.message = message;
        this.threads = threads;
        initGUI();
    }
    protected void initGUI()
    {
        if(parent instanceof Frame)
        {
            dialog = new JDialog((Frame)parent, "请稍等" + 
                                "...", true);
        }
        else if(parent instanceof Dialog)
        {
            dialog = new JDialog((Dialog)parent, "请稍等" + 
                                "...", true);
        }
        else
        {
            dialog = new JDialog();
            dialog.setTitle("请稍等" + "...");
            dialog.setModal(true);
        }
        dialog.setCursor(new Cursor(Cursor.WAIT_CURSOR));
        JLabel infoLabel = new JLabel(message, JLabel.CENTER);
        JPanel infoPanel = new JPanel(new BorderLayout());
        infoPanel.add(infoLabel, BorderLayout.CENTER);
        dialog.getContentPane().add(infoPanel);
        int left = (dimensions.width - width)/2;
        int top = (dimensions.height - height)/2;
        dialog.setSize(new Dimension(width,height));
        dialog.setLocation(left, top);
    }
    
    public void start()
    {
        SwingUtilities.invokeLater(new startThread());
    }
    
    public void startMore()
    {
        SwingUtilities.invokeLater(new startMoreThread());
    }
    public void actionPerformed()
    {
    }
    class startThread extends Thread
    {
        public void run()
        {
            new ShowDialog().start();
            actionThread = new ActionThread();
            actionThread.start();
            new CloseDialog().start();
        }
    }
    class startMoreThread extends Thread
    {
        public void run()
        {
            new ShowDialog().start();
            if (threads != null) 
            {
                for (Iterator iter = threads.values().iterator();
                     iter.hasNext();)
                {
                    ((Thread)iter.next()).start();
                }
            }
            new CloseDialog().start();
        }
    }
    class ActionThread extends Thread
    {
        public void run()
        {
            actionPerformed();
        }
    }
    class ShowDialog extends Thread
    {
        public void run()
        {
            dialog.show();
        }
    }
    
    class CloseDialog extends Thread
    {
        public void run()
        {
            if (actionThread != null)
            {
                try 
                {
                    actionThread.join();
                }
                catch(InterruptedException e)
                {
                    e.printStackTrace(System.out);
                } 
            }
            else if(threads != null)
            {
                Thread tmpThread = null;
                while (true)
                {
                    try
                    {
                        Thread.currentThread().sleep(250);
                    }
                    catch(InterruptedException e)
                    {
                        e.printStackTrace(System.out);
                    }
                    for( Iterator iter = threads.values().iterator();
                        iter.hasNext();)
                    {
                        tmpThread = (Thread)iter.next();
                        if(!tmpThread.isAlive())
                        {
                            iter.remove();
                        }
                    }
                    if (threads.isEmpty())
                    {
                        break;
                    }
                }
            }
            dialog.dispose();
        }
    }
}
这个程序可以帮你完成你想要的功能,呵呵,

解决方案2:

其实用进度条并不是最好的方法,可以选择用SPLASH,即开机画面
下面是进度条的例子,你参考下吧
/**
   @version 1.03 2004-08-22
   @author Cay Horstmann
*/
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.Timer;
/**
   This program demonstrates the use of a progress bar
   to monitor the progress of a thread.
*/
public class ProgressBarTest
{  
   public static void main(String[] args)
   {  
      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
      JPanel panel = new JPanel();
      startButton = new JButton("Start");
      progressBar = new JProgressBar();
      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());
            }
         });
      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)
            {  
               progressBar.setMaximum(1000);
               activity = new SimulatedActivity(1000);
               new Thread(activity).start();
               activityMonitor.start();
               startButton.setEnabled(false);
            }
         });
      // set up the timer action
      activityMonitor = new Timer(500, new 
         ActionListener()
         {  
            public void actionPerformed(ActionEvent event)
            {  
               int current = activity.getCurrent();
               
               // show progress
               textArea.append(current + "\n");
               progressBar.setStringPainted(!progressBar.isIndeterminate());
               progressBar.setValue(current);
               
               // check if task is completed
               if (current == activity.getTarget())
               {  
                  activityMonitor.stop();
                  startButton.setEnabled(true);
               }
            }
         });
   }
   private Timer activityMonitor;
   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;
}
/**
   A similated activity runnable.
*/
class SimulatedActivity implements Runnable

   /**
      Constructs the simulated activity thread object. The
      thread 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;
   }
   public int getTarget()
   {  
      return target;
   }
   public int getCurrent()
   {  
      return current;
   }
   public void run()
   {  
      try
      {
         while (current < target)
         {    
            Thread.sleep(100);
            current++;
         }
      }
      catch(InterruptedException e)
      {  
      }
   }
   private volatile int current;
   private int target;
}


以上介绍了“ 怎么在现有的程序中加入进度条功能及进度条如何实现”的问题解答,希望对有需要的网友有所帮助。
本文网址链接:http://www.codes51.com/itwd/1858581.html

相关图片

相关文章