代碼如下: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; // 定義標(biāo)簽組件 public JLabel label; // 構(gòu)造函數(shù) public Demo() { // 創(chuàng)建標(biāo)簽,并在標(biāo)簽上放置一張圖片 label = new JLabel(new ImageIcon("images/background.jpg")); label.setBounds(0, 0, LOAD_WIDTH, LOAD_HEIGHT - 15); // 創(chuàng)建進度條 progressbar = new JProgressBar(); // 顯示當(dāng)前進度值信息 progressbar.setStringPainted(true); // 設(shè)置進度條邊框不顯示 progressbar.setBorderPainted(false); // 設(shè)置進度條的前景色 progressbar.setForeground(new Color(0, 210, 40)); // 設(shè)置進度條的背景色 progressbar.setBackground(new Color(188, 190, 194)); progressbar.setBounds(0, LOAD_HEIGHT - 15, LOAD_WIDTH, 15); // 添加組件 this.add(label); this.add(progressbar); // 設(shè)置布局為空 this.setLayout(null); // 設(shè)置窗口初始位置 this.setLocation((WIDTH - LOAD_WIDTH) / 2, (HEIGHT - LOAD_HEIGHT) / 2); // 設(shè)置窗口大小 this.setSize(LOAD_WIDTH, LOAD_HEIGHT); // 設(shè)置窗口顯示 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(); } } 效果圖:

成都創(chuàng)新互聯(lián)公司服務(wù)項目包括米林網(wǎng)站建設(shè)、米林網(wǎng)站制作、米林網(wǎng)頁制作以及米林網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,米林網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到米林省份的部分城市,未來相信會繼續(xù)擴大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
實現(xiàn)這個功能比較簡單,用到的類有兩個:ProgressMonitorInputStream(主要是整個類) 和 ProgressMonitor ,在javax.swing中整個文件的大小,和當(dāng)前已經(jīng)讀取文件的大小,獲得整個文件大小的方法
代碼如下:
ProgressMonitorInputStream monitor;
/**
* @param 表示此進度條要依附在哪個組件上
* @param 顯示在此進度條上的消息
* @param 需要監(jiān)控的輸入流
*/
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,主要是為了獲得換行符的字節(jié)數(shù),否則,最后獲得的進步無法達(dá)到100%
代碼如下:
int readed=0;//表示已經(jīng)讀取的文件
reader+=in;//累加讀取文件大小
計算進度:
代碼如下:
float process = (float) readed / all * 100;// 算出百分比
窗口顯示:
代碼如下:
progressMonitor.setNote("archived " + process + " %");// 顯示在進度條上
使用?? apache fileupload?? ,spring MVC?? jquery1.6x , bootstrap? 實現(xiàn)一個帶進度條的多文件上傳,由于fileupload 的局限,暫不能實現(xiàn)每個上傳文件都顯示進度條,只能實現(xiàn)一個總的進度條,效果如圖:
1、jsp 頁面
!DOCTYPE?html??
%@?page?contentType="text/html;charset=UTF-8"%????
%@?taglib?prefix="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?id='fForm'?class="form-actions?form-horizontal"?action="../upload.html"???
encType="multipart/form-data"?target="uploadf"?method="post"??
div?class="control-group"??
label?class="control-label"上傳文件:/label??
div?class="controls"??
input?type="file"??name="file"?style="width:550"??
/div??
div?class="controls"??
input?type="file"??name="file"?style="width:550"??
/div??
div?class="controls"??
input?type="file"??name="file"?style="width:550"??
/div??
label?class="control-label"上傳進度:/label??
div?class="controls"??
div??class="progress?progress-success?progress-striped"?style="width:50%"??
div??id?=?'proBar'?class="bar"?style="width:?0%"/div??
/div??
/div??
/div??
div?class="control-group"??
div?class="controls"??
button?type="button"?id="subbut"?class="btn"submit/button??
/div??
/div??
/form??
iframe?name="uploadf"?style="display:none"/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??上傳文件?
*?@param?request?
*?@param?response?
*?@return?
*?@throws?Exception?
*/??
@RequestMapping(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?獲取進度?
*?@param?request?
*?@param?response?
*?@return?
*?@throws?Exception?
*/??
@RequestMapping(value?=?"/process.json",?method?=?RequestMethod.GET)??
@ResponseBody??
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;??
}??
}
很簡單,因為數(shù)據(jù)讀到集合所用的時間遠(yuǎn)遠(yuǎn)不如數(shù)據(jù)庫的存儲,因此只要計算插入數(shù)據(jù)庫的進度即可。做法是:在讀入數(shù)據(jù)庫的時候 根據(jù)集合的大小生成一個最大進度為集合長度的進度條,每成功寫入數(shù)據(jù)庫一條,進度條 +1。
當(dāng)前標(biāo)題:java進度條代碼大全 進度條代碼怎么寫
網(wǎng)址分享:http://chinadenli.net/article12/hjocdc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作、網(wǎng)站排名、全網(wǎng)營銷推廣、外貿(mào)網(wǎng)站建設(shè)、服務(wù)器托管、小程序開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)