zoukankan      html  css  js  c++  java
  • 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);
    }

     

  • 相关阅读:
    Document
    Echarts 图例交互事件,及使用
    Echarts 入门之基础使用(部份 API)
    对比 continue、break 在循环中的作用
    Markdown 简要语法速成
    CSS 实现必填项前/后添加红色*、√、X、▲
    9.React Context 上下文
    [leetcode sort]179. Largest Number
    [leetcode tree]102. Binary Tree Level Order Traversal
    [leetcode tree]101. Symmetric Tree
  • 原文地址:https://www.cnblogs.com/liuyuanyuanGOGO/p/JProgress_Timer.html
Copyright © 2011-2022 走看看