java進度
『壹』 java實現進度條
先要明確你怎樣控制進度條,然後分步控制。就是如果讓你把進度條加到50%,你如何加。
『貳』 用 java 怎麼做進度條
用servlet吧,網上有很多!
『叄』 JAVA 進度條
import java.awt.BorderLayout;
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循環裡面加了個延時函數。
『肆』 java多文件上傳顯示進度條
使用 apache fileupload ,spring MVC jquery1.6x , bootstrap 實現一個帶進度條的多文件上傳,由於fileupload 的局限,暫不能實現每個上傳文件都顯示進度條,只能實現一個總的進度條,效果如圖:
packagecom.controller;
importjava.util.List;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importjavax.servlet.http.HttpSession;
importorg.apache.commons.fileupload.FileItemFactory;
importorg.apache.commons.fileupload.ProgressListener;
importorg.apache.commons.fileupload.disk.DiskFileItemFactory;
importorg.apache.commons.fileupload.servlet.ServletFileUpload;
importorg.apache.log4j.Logger;
importorg.springframework.stereotype.Controller;
importorg.springframework.web.bind.annotation.RequestMapping;
importorg.springframework.web.bind.annotation.RequestMethod;
importorg.springframework.web.bind.annotation.ResponseBody;
importorg.springframework.web.servlet.ModelAndView;
@Controller
{
Loggerlog=Logger.getLogger(FileUploadController.class);
/**
*upload上傳文件
*@paramrequest
*@paramresponse
*@return
*@throwsException
*/
@RequestMapping(value="/upload.html",method=RequestMethod.POST)
publicModelAndViewupload(HttpServletRequestrequest,
HttpServletResponseresponse)throwsException{
finalHttpSessionhs=request.getSession();
ModelAndViewmv=newModelAndView();
booleanisMultipart=ServletFileUpload.isMultipartContent(request);
if(!isMultipart){
returnmv;
}
//Createafactoryfordisk-basedfileitems
FileItemFactoryfactory=newDiskFileItemFactory();
//Createanewfileuploadhandler
ServletFileUploapload=newServletFileUpload(factory);
upload.setProgressListener(newProgressListener(){
publicvoipdate(longpBytesRead,longpContentLength,intpItems){
ProcessInfopri=newProcessInfo();
pri.itemNum=pItems;
pri.readSize=pBytesRead;
pri.totalSize=pContentLength;
pri.show=pBytesRead+"/"+pContentLength+"byte";
pri.rate=Math.round(newFloat(pBytesRead)/newFloat(pContentLength)*100);
hs.setAttribute("proInfo",pri);
}
});
Listitems=upload.parseRequest(request);
//Parsetherequest
//Processtheuploadeditems
//Iteratoriter=items.iterator();
//while(iter.hasNext()){
//FileItemitem=(FileItem)iter.next();
//if(item.isFormField()){
//Stringname=item.getFieldName();
//Stringvalue=item.getString();
//System.out.println("thisiscommonfeild!"+name+"="+value);
//}else{
//System.out.println("thisisfilefeild!");
//StringfieldName=item.getFieldName();
//StringfileName=item.getName();
//StringcontentType=item.getContentType();
//booleanisInMemory=item.isInMemory();
//longsizeInBytes=item.getSize();
//FileuploadedFile=newFile("c://"+fileName);
//item.write(uploadedFile);
//}
//}
returnmv;
}
/**
*process獲取進度
*@paramrequest
*@paramresponse
*@return
*@throwsException
*/
@RequestMapping(value="/process.json",method=RequestMethod.GET)
@ResponseBody
publicObjectprocess(HttpServletRequestrequest,
HttpServletResponseresponse)throwsException{
return(ProcessInfo)request.getSession().getAttribute("proInfo");
}
classProcessInfo{
publiclongtotalSize=1;
publiclongreadSize=0;
publicStringshow="";
publicintitemNum=0;
publicintrate=0;
}
}
『伍』 關於Java中進度條的問題
importjava.awt.BorderLayout;
importjava.awt.Cursor;
importjava.awt.Toolkit;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.beans.PropertyChangeEvent;
importjava.beans.PropertyChangeListener;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileOutputStream;
importjava.io.InputStream;
importjava.io.OutputStream;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JProgressBar;
importjavax.swing.SwingWorker;
{
=1L;
;
privateJButton;
privateTasktask;
publicBar(Stringtitle)
{
setTitle(title);
setLayout(newBorderLayout());
progressBar=newJProgressBar(0,100);
progressBar.setValue(0);
progressBar.setStringPainted(true);
add(progressBar,BorderLayout.CENTER);
=newJButton("拷貝");
.addActionListener(newActionListener()
{
@Override
publicvoidactionPerformed(ActionEvente)
{
start();
.setEnabled(false);
}
});
add(,BorderLayout.EAST);
}
classTaskextendsSwingWorker<Void,Void>
{
@Override
publicVoiddoInBackground()
{
setProgress(0);
Filefile=newFile("aa.mp3");
longsize=file.length();
byte[]bts=newbyte[1024*10];
intlength=0;
longcLength=0;
try
{
InputStreamstream=newFileInputStream(file);
OutputStreamos=newFileOutputStream(newFile("a.mp3"));
while((length=stream.read(bts))>0)
{
os.write(bts,0,length);
cLength+=length;
setProgress((int)(cLength*100/size));
Thread.sleep(100);
}
os.close();
stream.close();
}
catch(Exceptione1)
{
e1.printStackTrace();
}
returnnull;
}
@Override
publicvoiddone()
{
setTitle("拷貝完畢");
Toolkit.getDefaultToolkit().beep();
.setEnabled(true);
setCursor(null);
}
}
publicvoidpropertyChange(PropertyChangeEventevt)
{
if("progress"==evt.getPropertyName())
{
intprogress=(Integer)evt.getNewValue();
progressBar.setValue(progress);
}
}
privatevoidstart()
{
task=newTask();
task.addPropertyChangeListener(this);
task.execute();
setTitle("正在拷貝...");
setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
}
()
{
Barframe=newBar("Bar");
frame.setAlwaysOnTop(true);
frame.setSize(280,60);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
publicstaticvoidmain(String[]args)
{
javax.swing.SwingUtilities.invokeLater(newRunnable()
{
publicvoidrun()
{
createAndShowGUI();
}
});
}
}
『陸』 java中怎樣實現漸變色進度條
代碼如下:
package test;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.plaf.basic.BasicProgressBarUI;
public class test extends JFrame {
int current = 0;
public static void main(String[] args) {
new test().setVisible(true);
}
public test() {
JPanel contentPane = new JPanel();
setContentPane(contentPane);
contentPane.setBounds(0, 0, 400, 400);
final JProgressBar pbMusic = new JProgressBar();
pbMusic.setBounds(16, 38, 332, 4);
pbMusic.setValue(current);
pbMusic.setMaximum(100);
pbMusic.setUI(new GradientProgressBarUI());
pbMusic.setBorderPainted(false);
new Timer().schele(new TimerTask() {
@Override
public void run() {
pbMusic.setValue(current++);
}
}, 0, 1000);
contentPane.add(pbMusic);
setLocation(400, 200);
setSize(400, 400);
setVisible(true);
}
public class GradientProgressBarUI extends BasicProgressBarUI {
@Override
protected void paintDeterminate(Graphics g, JComponent c) {
Graphics2D graphics2d = (Graphics2D) g;
Insets b = progressBar.getInsets();
// JProgressBar的邊界區域
int width = progressBar.getWidth();
int height = progressBar.getHeight();
int barRectWidth = width - (b.right + b.left);
int barRectHeight = height - (b.top + b.bottom);
int arcSize = height / 2 - 1;
int amountFull = getAmountFull(b, barRectWidth, barRectHeight);
//已完成的進度
graphics2d.setColor(Color.WHITE);
graphics2d.fillRoundRect(0, 0, width - 1, height, arcSize,
arcSize);
//繪制JProgressBar的背景
//用GradientPaint類來實現漸變色顯示進度
//設置了開始點和終止點,並設置好這兩個點的顏色,系統會自動在這兩個點中用漸變色來填充
Point2D start = new Point2D.Float(0, 0);
Point2D end = new Point2D.Float(amountFull - 1, barRectHeight - 1);
//這里設置的終止點是當前已經完成的進度的那個點
GradientPaint gradientPaint = new GradientPaint(
start, new Color(114,243,233), end, new Color(233,243,123));
graphics2d.setPaint(gradientPaint);
graphics2d.fillRoundRect(b.left, b.top, amountFull - 1,
barRectHeight, arcSize, arcSize);//這里實現的是圓角的效果將arcSize調成0即可實現矩形效果
}
}
}
『柒』 java學習進度跟不上怎麼辦
要多去熟悉相關的原理性知識
多動手去練習,不要怕懶,一遍不會多練習幾遍
還是多練,熟能生巧。
ps:如果涉及項目,必須要把業務邏輯理清再去寫代碼,最好是偽代碼出來再去寫代碼。
『捌』 java 做進度條 進度數據獲取
publicstaticvoidmain(String[]args){
intnum=20;
for(inti=0;i<=num;i++){
System.out.println("當前進度:"+i*100/num+"%"+i+"/"+num);
}
}
『玖』 怎麼做java語言進度條
在js中做一個遮罩層,當你點擊的時候顯示進度條,進度條讀完之後再隱藏,以下有兩種方式,試試,望採納
方法一:
//開啟遮罩
$.messager.progress({});
或
$.messager.progress({
title: 'Please waiting',
msg: 'Loading data...',
text: 'PROCESSING.......'
});
//關閉遮罩
$.messager.progress('close');
方法二:
//顯示遮罩
$("#標簽ID").標簽("loading", "數據載入中……")
$("#dg").datagrid("loading", "數據載入中……");
//隱藏遮罩
$("#dg").datagrid("loaded");