java进度条代码大全 进度条代码怎么写

发布时间:2025-12-09 19:48:51 浏览次数:4

Java中如何实现进度条效果

代码如下:import java.awt.Color; import java.awt.Toolkit; import javax.swing.ImageIcon; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JProgressBar; import javax.swing.JWindow; @SuppressWarnings("serial") public class Demo extends JWindow implements Runnable { // 定义加载窗口大小 public static final int LOAD_WIDTH = 455; public static final int LOAD_HEIGHT = 295; // 获取屏幕窗口大小 public static final int WIDTH = Toolkit.getDefaultToolkit().getScreenSize().width; public static final int HEIGHT = Toolkit.getDefaultToolkit().getScreenSize().height; // 定义进度条组件 public JProgressBar progressbar; // 定义标签组件 public JLabel label; // 构造函数 public Demo() { // 创建标签,并在标签上放置一张图片 label = new JLabel(new ImageIcon("images/background.jpg")); label.setBounds(0, 0, LOAD_WIDTH, LOAD_HEIGHT - 15); // 创建进度条 progressbar = new JProgressBar(); // 显示当前进度值信息 progressbar.setStringPainted(true); // 设置进度条边框不显示 progressbar.setBorderPainted(false); // 设置进度条的前景色 progressbar.setForeground(new Color(0, 210, 40)); // 设置进度条的背景色 progressbar.setBackground(new Color(188, 190, 194)); progressbar.setBounds(0, LOAD_HEIGHT - 15, LOAD_WIDTH, 15); // 添加组件 this.add(label); this.add(progressbar); // 设置布局为空 this.setLayout(null); // 设置窗口初始位置 this.setLocation((WIDTH - LOAD_WIDTH) / 2, (HEIGHT - LOAD_HEIGHT) / 2); // 设置窗口大小 this.setSize(LOAD_WIDTH, LOAD_HEIGHT); // 设置窗口显示 this.setVisible(true); } public static void main(String[] args) { Demo t = new Demo(); new Thread(t).start(); } @Override public void run() { for (int i = 0; i 100; i++) { try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } progressbar.setValue(i); } JOptionPane.showMessageDialog(this, "加载完成"); this.dispose(); } } 效果图:

java 做进度条 进度数据获取

实现这个功能比较简单,用到的类有两个:ProgressMonitorInputStream(主要是整个类) 和 ProgressMonitor ,在javax.swing中整个文件的大小,和当前已经读取文件的大小,获得整个文件大小的方法

代码如下:

ProgressMonitorInputStream monitor;

/**

* @param 表示此进度条要依附在哪个组件上

* @param 显示在此进度条上的消息

* @param 需要监控的输入流

*/

monitor = new ProgressMonitorInputStream(null, "Loading ",new FileInputStream("filename path"));

int all = monitor.available();//整个文件的大小

int in = monitor.read(data);//每次读取文件的大小

例如:你每次读一行str=in.readLine();则data=str.instr.getBytes()+1;这里+1,主要是为了获得换行符的字节数,否则,最后获得的进步无法达到100%

代码如下:

int readed=0;//表示已经读取的文件

reader+=in;//累加读取文件大小

计算进度:

代码如下:

float process = (float) readed / all * 100;// 算出百分比

窗口显示:

代码如下:

progressMonitor.setNote("archived " + process + " %");// 显示在进度条上

java多文件上传显示进度条

使用   apache fileupload   ,spring MVC   jquery1.6x , bootstrap  实现一个带进度条的多文件上传,由于fileupload 的局限,暂不能实现每个上传文件都显示进度条,只能实现一个总的进度条,效果如图:

1、jsp 页面

!DOCTYPE html  

[email protected]="text/html;charset=UTF-8"%    

[email protected]="c" uri="" %    

html xmlns=""  

head  

meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /  

script src="../js/jquery-1.6.4.js" type="text/javascript"/script  

link rel="stylesheet" type="text/css" href="../css/bootstrap.css"/  

/head  

body  

        form   action="../upload.html"   

              encType="multipart/form-data" target="uploadf" method="post"  

                 p   

                    label 上传文件:/label  

                    p   

                        input type="file"  name="file"   

                              

                    /p  

                    p   

                        input type="file"  name="file"   

                    /p  

                    p   

                        input type="file"  name="file"   

                    /p  

                    label 上传进度:/label  

                    p   

                        p     

                            p  id = 'proBar'  /p  

                        /p  

                    /p  

                /p  

                  

                 p   

                    p   

                    button type="button"  submit/button  

                    /p  

                /p  

        /form  

        iframe name="uploadf" /iframe  

/body  

/html  

script   

$(document).ready(function(){  

    $('#subbut').bind('click',  

            function(){  

                $('#fForm').submit();  

                var eventFun = function(){  

                    $.ajax({  

                        type: 'GET',  

                        url: '../process.json',  

                        data: {},  

                        dataType: 'json',  

                        success : function(data){  

                                $('#proBar').css('width',data.rate+''+'%');  

                                $('#proBar').empty();  

                                $('#proBar').append(data.show);   

                                if(data.rate == 100){  

                                    window.clearInterval(intId);  

                                }     

                }});};  

                var intId = window.setInterval(eventFun,500);  

    });  

});  

/script

2、java 代码

package com.controller;  

  

import java.util.List;  

  

import javax.servlet.http.HttpServletRequest;  

import javax.servlet.http.HttpServletResponse;  

import javax.servlet.http.HttpSession;  

  

import org.apache.commons.fileupload.FileItemFactory;  

import org.apache.commons.fileupload.ProgressListener;  

import org.apache.commons.fileupload.disk.DiskFileItemFactory;  

import org.apache.commons.fileupload.servlet.ServletFileUpload;  

import org.apache.log4j.Logger;  

import org.springframework.stereotype.Controller;  

import org.springframework.web.bind.annotation.RequestMapping;  

import org.springframework.web.bind.annotation.RequestMethod;  

import org.springframework.web.bind.annotation.ResponseBody;  

import org.springframework.web.servlet.ModelAndView;  

  

@Controller  

public class FileUploadController {  

    Logger log = Logger.getLogger(FileUploadController.class);  

      

    /** 

     * upload  上传文件 

[email protected]

[email protected]

[email protected]

[email protected]

     */  

[email protected](value = "/upload.html", method = RequestMethod.POST)  

    public ModelAndView upload(HttpServletRequest request,  

            HttpServletResponse response) throws Exception {  

        final HttpSession hs = request.getSession();  

        ModelAndView mv = new ModelAndView();  

        boolean isMultipart = ServletFileUpload.isMultipartContent(request);  

        if(!isMultipart){  

            return mv;  

        }  

        // Create a factory for disk-based file items  

        FileItemFactory factory = new DiskFileItemFactory();  

  

        // Create a new file upload handler  

        ServletFileUpload upload = new ServletFileUpload(factory);  

        upload.setProgressListener(new ProgressListener(){  

               public void update(long pBytesRead, long pContentLength, int pItems) {  

                   ProcessInfo pri = new ProcessInfo();  

                   pri.itemNum = pItems;  

                   pri.readSize = pBytesRead;  

                   pri.totalSize = pContentLength;  

                   pri.show = pBytesRead+"/"+pContentLength+" byte";  

                   pri.rate = Math.round(new Float(pBytesRead) / new Float(pContentLength)*100);  

                   hs.setAttribute("proInfo", pri);  

               }  

            });  

        List items = upload.parseRequest(request);  

        // Parse the request  

        // Process the uploaded items  

//      Iterator iter = items.iterator();  

//      while (iter.hasNext()) {  

//          FileItem item = (FileItem) iter.next();  

//          if (item.isFormField()) {  

//              String name = item.getFieldName();  

//              String value = item.getString();  

//              System.out.println("this is common feild!"+name+"="+value);  

//          } else {  

//              System.out.println("this is file feild!");  

//               String fieldName = item.getFieldName();  

//                  String fileName = item.getName();  

//                  String contentType = item.getContentType();  

//                  boolean isInMemory = item.isInMemory();  

//                  long sizeInBytes = item.getSize();  

//                  File uploadedFile = new File("c://"+fileName);  

//                  item.write(uploadedFile);  

//          }  

//      }  

        return mv;  

    }  

      

      

    /** 

     * process 获取进度 

[email protected]

[email protected]

[email protected]

[email protected]

     */  

[email protected](value = "/process.json", method = RequestMethod.GET)  

[email protected]

    public Object process(HttpServletRequest request,  

            HttpServletResponse response) throws Exception {  

        return ( ProcessInfo)request.getSession().getAttribute("proInfo");  

    }  

      

    class ProcessInfo{  

        public long totalSize = 1;  

        public long readSize = 0;  

        public String show = "";  

        public int itemNum = 0;  

        public int rate = 0;  

    }  

      

}

java导入进度条

很简单,因为数据读到集合所用的时间远远不如数据库的存储,因此只要计算插入数据库的进度即可。做法是:在读入数据库的时候 根据集合的大小生成一个最大进度为集合长度的进度条,每成功写入数据库一条,进度条 +1。

需要做网站?需要网络推广?欢迎咨询客户经理 13272073477