JSPCN主页 | JSP空间 | 网站制作 | JSP下载 | JSP论坛 | JSP教程 | 关于JSPCN | 联系我们
JSP虚拟主机,jsp空间,java空间,java虚拟空间,详细请点击进入
做最专业的JSP中文网站 当前位置首页--JAVA技术--JAVA类  
文章搜索:
关键字 标题
  
JSP中文网内容管理系统(JCMS)
JSP虚拟主机
网络笔记本
网摘,图片,笔记收藏
虚拟服务器

JSPCN文章目录分类
JSP配置[219]JSP基础[136]
中文问题[69]上传问题[27]
JAVABEAN[46]数据库[212]
文件操作[126]图片声音[17]
JSP其他[57]时间相关[16]
JAVAMAIL[72]STRUTS[144]
开发工具[28]教程系列[157]
JSP实例[89]
JAVA基础[421]APPLET[78]
JAVA网络[179]Applica[115]
Servlet[98]XML[163]
J2ME[257]J2EE[374]
考试相关[63]JAVA线程[90]
EJB[261]Swing[26]
Java API[141]声音图片[28]
异常处理[33]JAVA实例[290]
JAVA类[139]SUN[89]
Hibernate[6]JMX[8]
Spring[34]
本版推荐文章 
本版热点文章 
相关文章链接 
 
ProgressMonitorInputStream类的使用(笔记)
作者:未知     文章来源:www.jspcn.net
访问次数:407次     加入时间:2005年01月19日
在写GUI程序时,常常需要读取一些比较大的文件,此时可能需要几分钟的时间。我们希望界面尽量友好,使用户随时知道读取文件、处理的进度。在大多数Windows程序中,大家对进度条都已经非常熟悉,Java中也有现成的类可以非常方便地完成此项功能--ProgressMonitorInputStream。下面是一个小的程序,你可以编译运行。运行后点击窗口中的“Press me”按钮,将弹出一个窗口,其中包含一个进度条,显示读取当前目录中名为bigfile.dat的文件的进度。还包含一个“取消”按钮,可以随时中止读取线程。

import java.awt.FlowLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.FileInputStream;import java.io.InputStream;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.ProgressMonitorInputStream;public class Test { public static void main(String[] args) { // create a test frame with a "press me" button final JFrame f = new JFrame("Sample"); f.getContentPane().setLayout(new FlowLayout()); JButton b = new JButton("Press me"); f.getContentPane().add(b); f.pack(); // set up the file read action b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // when button is pressed, start a new thread // to read the file. A new thread is needed because we // need to free the GUI update thread to paint the // progress monitor new Thread() { public void run() { try { // open the file, wrapping it in a ProgressMonitorInputStream InputStream in = new FileInputStream("bigfile.dat"); ProgressMonitorInputStream pm = new ProgressMonitorInputStream(f,"Reading the big file",in); // read the file. If it´s taking too long, the progress // monitor will appear. The amount of time is roughly // 1/100th of the estimated read time (based on how long // it took to read the first 1/100th of the file.) // Note that by default, the dialog won´t appear unless // the overall estimate is over 2 seconds. int c; while((c=pm.read()) != -1) { // do something } pm.close(); // needs better error handling, of course... } catch(Exception ex) { ex.printStackTrace(); } } }.start(); }}); // display the frame f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); }} 
Copyright © 2002-2005 JSPCN.net. All rights reserved.
JSP中文网    备案序号:蜀ICP备05001583号
成都恒海科技发展有限公司    成都市一环路南二段6号新瑞楼三楼8号