ProgressBar.java
上传用户:hang318
上传日期:2021-05-07
资源大小:41k
文件大小:2k
源码类别:

状态条

开发平台:

Java

  1. package com.ata.wx;
  2. import java.awt.Color;
  3. import java.awt.FlowLayout;
  4. import java.awt.GridLayout;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import javax.swing.JButton;
  8. import javax.swing.JFrame;
  9. import javax.swing.JLabel;
  10. import javax.swing.JPanel;
  11. import javax.swing.JProgressBar;
  12. public class ProgressBar extends Thread implements ActionListener{
  13.         JProgressBar jpb=new JProgressBar();
  14.         JLabel jl=new JLabel();
  15.       
  16.         public void actionPerformed(ActionEvent e) {
  17.                 start();
  18.         }
  19.         public void run(){
  20.                 for(int i=0;i<=100;i++){
  21.                 jpb.setValue(i);
  22.                 jpb.setStringPainted(true);
  23.                 String per=(int)(jpb.getPercentComplete()*100)+"%";
  24.                 jl.setText(per);
  25.                         try{
  26.                                 Thread.sleep(100);
  27.                         }catch(Exception ee){
  28.                                 ee.printStackTrace();
  29.                         }
  30.                 }
  31.         
  32.         }
  33.         public ProgressBar(){
  34.                 JFrame jf=new JFrame();
  35.                 jf.setLayout(new FlowLayout());
  36.                 JButton jb=new JButton("START");
  37.                 JPanel jp=new JPanel();
  38.                 jp.setLayout(new GridLayout(2,1));
  39.                 jp.add(jpb);
  40.                 jp.add(jl);
  41.                 jf.add(jp);
  42.                 
  43.                 jf.add(jb);
  44.                 
  45.                 jb.setBackground(Color.orange);
  46.                 jb.addActionListener(this);
  47.                 jf.pack();
  48.                 jf.setLocation(400,300);
  49.                 jf.setSize(200,140);
  50.                 jf.setVisible(true);
  51.                 jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  52.         }
  53.         public static void main(String[] args) {
  54.                 new ProgressBar();
  55.         }
  56. }