zoukankan      html  css  js  c++  java
  • [转载]java 进度条

    原文地址:java 进度条作者:一度_何成飞

    import java.awt.Canvas;
    import java.awt.Color;
    import java.awt.Graphics;

    public class MyProgressBar extends Canvas {
     private float scaleSize;
     private float currentValue;
     
     public MyProgressBar(){
      this(100, 50);
     }
     
     public MyProgressBar(float scaleSize, float currentValue){
      this.scaleSize = scaleSize;
      this.currentValue = currentValue;
      
      this.setBackground(Color.lightGray);
      this.setForeground(Color.magenta);
      setSize(150, 25);
     }
     
     public float getCurrentValue() {
      return currentValue;
     }
     public void setCurrentValue(float currentValue) {
      this.currentValue = Math.max(0, currentValue);
      if (this.scaleSize < this.currentValue) {
       this.currentValue = this.scaleSize;
      }
     }
     public float getScaleSize() {
      return scaleSize;
     }
     public void setScaleSize(float scaleSize) {
      this.scaleSize = Math.max(1.0f, scaleSize);
      if (this.scaleSize < this.currentValue) {
       this.scaleSize = this.currentValue;
      }
     }

     public synchronized void paint(Graphics g){
      int w = getSize().width;
      int h = getSize().height;
      
      g.setColor(getBackground());
      g.fillRect(1, 1, w-2, h-2);
      g.fill3DRect(0, 0, w-1, h-1, true);
      
      g.setColor(getForeground());
      g.fillRect(3, 3, (int)(currentValue*(w-6)/scaleSize), h-6);
     }
    }

     

    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    import javax.swing.JButton;
    import javax.swing.JFrame;

    public class TestMyProgressBar extends JFrame implements Runnable,
      ActionListener {

     private MyProgressBar bar;
     private JButton btnStart;
     static TestMyProgressBar tmpb;

     public TestMyProgressBar() {
      setSize(400, 300);
      setLocation(400, 400);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setTitle("演示自定义进度条");
      setLayout(new FlowLayout());
      btnStart = new JButton("开始");
      this.add(btnStart);
      btnStart.addActionListener(this);
      bar = new MyProgressBar();
      setVisible(true);
     }

     public static void main(String[] args) {
      tmpb = new TestMyProgressBar();
     }

     @Override
     public void run() {
      for (int i = 1; i <= 20; i++) {
       int x = i * 5;
       bar.setCurrentValue(x);
       if (x > 0 && x < 100) {
        btnStart.setEnabled(false);
       }
       if (x == 100) {
        btnStart.setEnabled(true);
       }
       try {
        Thread.sleep(200);
        add(bar);
        
       } catch (InterruptedException e) {
        e.printStackTrace();
       }
      }
     }

     @Override
     public void actionPerformed(ActionEvent e) {
      if (e.getActionCommand().equals("开始")) {
       Thread t = new Thread(tmpb);
       t.start();
      }
     }
    }

  • 相关阅读:
    怎么用JQUERY设置div背景图片?
    为什么导入本地jquery.js老是无效?(已解决)
    问题:AJAX的send参数里,空格以及它后面的数据在传递时消失(已解决)
    问题:win7下配置好服务器就是不能查询数据库。(已解决)
    问题:怎么把mysql的系统时间调整为电脑的时间?(已解决)
    jira安装说明
    HTTP 响应时发生错误。这可能是由于服务终结点绑定未使用 HTTP 协议造成的。这还可能是由于服务器中止了 HTTP 请求上下文(可能由于服务关闭)所致。
    C# 清空控制台屏幕内容
    NPOI导出Excel,添加图片和设置格式,添加条形码
    JS简写
  • 原文地址:https://www.cnblogs.com/liuzhuqing/p/7481078.html
Copyright © 2011-2022 走看看