网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > Java
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
本月文章推荐
.Web框架wingS发布 2.0 版本.
.Java设计模式之迭代器模式.
.JAVA程序员必读:基础篇(4.a)-类和.
.Java模式设计之数据访问对象模式.
.回顾Java Beans.
.使用JBoss Eclipse IDE开发J2EE应.
.J2EE基础:Java EJB容器存取和实.
.JPanel加载图像的过程.
.类的初始化问题.
.记录log日志文件的工具类.
.Java 开发环境的过去、现在和将来.
.使用BigInteger处理大二进制数.
.JAR文件揭密.
.J2EE编程起步.
.NaN 属性 (Global).
.前车之覆,后车之鉴 --开源项目经.
.APTFLOW流程设计软件介绍.
.关于扰乱器与反编译.
.论J2EE程序员的武功修为.
.JAVA里字符编码的探索与理解.

ProgressMonitorInputStream类的使用(笔记)

发表日期:2008-1-5



  在写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); }}
上一篇:Effective Java读书笔记之五 人气:610
下一篇:Programming a Spider in Java 人气:823
浏览全部Java的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐