• JProgressBar与Timer的配套使用


        JProgressBar  的关键在于 setMaxium(int maxValue) 和 setValue(int progressValue);

        当ProgressBar的当前值需要Controller来提供时,遵照MVC结构的编码原则,我们不能向Controller传ProgressBar,

    所以不能在Controller中的逻辑过程中直接setValue,所以这时就需要Timer来为ProgressBar来setValue。

        Timer就像一个定时作业代理,每隔固定delay就向Controller询问一次进度值progressValue并为ProgressBar

    setValue(progressValue),这样就可以实现进度条正常运转。

        注意:setValue后proressBar若无法正常刷新进度条,则尝试一下方法。   

               MigrationController mc = MigrationController.getInstance();
               progressBar.setValue(mc.getProgressValue());        

    Dimension d = progressBar.getSize();
    Rectangle rect = new Rectangle(0, 0, d.width, d.height);

    progressBar.paintImmediately(rect);//立即刷新进度条

     

    JProgressBar与Timer配套使用的主要代码:

    1.初始化JProgressBar(视情况而定)

    progressBar.setMinimum(0);
    progressBar.setMaximum(100);

    progressBar.setValue(0);
    progressBar.setStringPainted(true);
    progressBar.setBorderPainted(true);
    progressBar.setBackground(Color.WHITE);

    2.初始化Timer:

    ActionListener taskPerformer = new ActionListener()
    {
        @Override
        public void actionPerformed(ActionEvent evt)
       {
             timerActionPerformed(evt);
        }
    };
    timer = new Timer(100, taskPerformer);

    3.为JProgressBar设置最大值(在合理的逻辑位置设置)

     progressBar.setMaximum(totalObject);

    4.Timer的监听事件处理

    private void timerActionPerformed(ActionEvent evt)
    {
         logger.info("CurrentProgressValue = " + progressBar.getValue());

         Dimension d = progressBar.getSize();
         Rectangle rect = new Rectangle(0, 0, d.width, d.height);

         MigrationController mc = MigrationController.getInstance();
         progressBar.setValue(mc.getProgressValue());
         progressBar.paintImmediately(rect);
    }

     

  • 相关阅读:
    JVM系列(五)并发相关
    String的hashCode 和equals 区别
    JVM系列(四)生命周期和classloader
    jvm面试题解答
    memcached(十三)注意事项
    memcached(十二)监控
    memcached(十)动态扩容
    memcached(九)--LRU
    memcached(八)-- set指令内部实现
    用通俗易懂的大白话讲解Map/Reduce原理
  • 原文地址:https://www.cnblogs.com/liuyuanyuanGOGO/p/JProgress_Timer.html
Copyright © 2020-2023  润新知