您的位置:程序门 -> java -> gui 设计



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


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


怎么在现有的程序中加入进度条功能及进度条如何实现[已结贴,结贴人:tofu1983]
发表于:2007-03-15 15:30:11 楼主
现在的项目基本上做完了,但有些功能需要很长时间的计算,得等上一会儿,所以想做个进度条功能,向大虾们请教一下,如何实现进度条及如何将进度条加到我现在的程序中.我不太清楚怎么做,但大概是起一个新的线程,同时启动进度条.但不知道怎么实现,请大虾帮帮忙!
发表于:2007-03-15 20:09:281楼 得分:50
其实用进度条并不是最好的方法,可以选择用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;
}


发表于:2007-03-16 15:40:012楼 得分:50
/**
*   @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();
                }
        }
}

这个程序可以帮你完成你想要的功能,呵呵,
发表于:2007-03-16 15:41:103楼 得分:0
挺适合你的需求的


快速检索

最新资讯
热门点击