import java.awt.BorderLayout;

福鼎網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)公司!從網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、成都響應(yīng)式網(wǎng)站建設(shè)公司等網(wǎng)站項(xiàng)目制作,到程序開發(fā),運(yùn)營(yíng)維護(hù)。創(chuàng)新互聯(lián)公司成立與2013年到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來(lái)保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)公司。
import javax.swing.JFrame;
import javax.swing.JProgressBar;
public class Test extends JFrame {
public Test(){
super();
setSize(100,100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
JProgressBar progressBar = new JProgressBar();
getContentPane().add(progressBar,BorderLayout.NORTH);
progressBar.setStringPainted(true);
for (int i = 0;i 50;i++){
progressBar.setValue(i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
new Test();
}
}
for循環(huán)里面加了個(gè)延時(shí)函數(shù)。
代碼如下: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; // 定義進(jìn)度條組件 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)建進(jìn)度條 progressbar = new JProgressBar(); // 顯示當(dāng)前進(jìn)度值信息 progressbar.setStringPainted(true); // 設(shè)置進(jìn)度條邊框不顯示 progressbar.setBorderPainted(false); // 設(shè)置進(jìn)度條的前景色 progressbar.setForeground(new Color(0, 210, 40)); // 設(shè)置進(jìn)度條的背景色 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(); } } 效果圖:
思路如下:
public class MyClass extends Thread {
public JProgressBar bar = new JProgressBar();//進(jìn)度條
public OutputStream os;// 通向服務(wù)器的流
public InputStream is;// 你的文件流
public long len;// 你文件的總長(zhǎng)度
public void run() {
byte[] buff = new byte[1024];
long passedlen = 0;
try {
?int mark = 0;
?while (true) {
??? ?mark = is.read(buff, 0, 1024);
??? ?os.write(buff, 0, mark);
??? ?// 更新進(jìn)度
??? ?passedlen += mark;
??? ?int prog = (int) ((double) passedlen / (len == 0 ? 1 : len) * 100);
??? ?// 改變進(jìn)度條的value值
??? ?bar.setValue(prog);
??? ?// 結(jié)束判定
??? ?if (passedlen == len) {
??? ??? ?break;
??? ?}
?}
?os.close();
} catch (IOException ex) {
?ex.printStackTrace();
}
}
}
這個(gè)好像做不到,現(xiàn)在一般有的效果就是上傳文件時(shí)的進(jìn)度條。
像你說(shuō)得讀取數(shù)據(jù)之類還沒(méi)遇見(jiàn)過(guò),一般也只是加個(gè)循環(huán)轉(zhuǎn)動(dòng)的圖片等待而已,不能做到實(shí)時(shí)。
使用?? apache fileupload?? ,spring MVC?? jquery1.6x , bootstrap? 實(shí)現(xiàn)一個(gè)帶進(jìn)度條的多文件上傳,由于fileupload 的局限,暫不能實(shí)現(xiàn)每個(gè)上傳文件都顯示進(jìn)度條,只能實(shí)現(xiàn)一個(gè)總的進(jìn)度條,效果如圖:
1、jsp 頁(yè)面
!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"上傳進(jìn)度:/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?獲取進(jìn)度?
*?@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è)置JScrollPane js=new JScrollPane(ja); js在界面中的大小,試試。其實(shí)swing界面很簡(jiǎn)單,不顯示要么沒(méi)有添加,要么被遮蓋了,要沒(méi)有設(shè)置大小,要么沒(méi)有設(shè)置布局位置。你細(xì)看看、找一個(gè)精度條的代碼研究研究
網(wǎng)站標(biāo)題:javajs進(jìn)度條代碼 js 進(jìn)度條的實(shí)現(xiàn)
網(wǎng)頁(yè)網(wǎng)址:http://chinadenli.net/article26/hipijg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機(jī)、網(wǎng)站導(dǎo)航、ChatGPT、網(wǎng)站營(yíng)銷、服務(wù)器托管、響應(yīng)式網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)