java ProgressMonitor 用法举例

时间:2021-12-27 22:17:11

近来工作不忙,写了一个小东西。用到了java进度条来显示当前的进度,如下是我写的一个测试的小例子,用于读取大文件。
读取文件的主类,带进度条
<code>
package cn.com.metalWork.util;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.TimeUnit;


import javax.swing.JFrame;
import javax.swing.ProgressMonitor;


public class TaskReadFile implements Runnable {


File file = new File("e:\\test.txt");

InputStream in=null;

ProgressMonitor monitor =null;

JFrame frame=null;

boolean flag = false;

StringBuilder sb = null;

byte[] bt=null;
public TaskReadFile( JFrame frame)
{
try {
in =new FileInputStream(file);
this.frame=frame;
monitor = new ProgressMonitor(frame, "读取文件...", "准备读取文件", 0,100);
sb=new StringBuilder();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}

@Override
public void run() {
long length=file.length();
monitor.setNote("文件大小:"+length);
int persize=(int) (length/100);
bt= new byte[persize];
for(int i =0;i<=100;i++)
{
try {
TimeUnit.MILLISECONDS.sleep(100);
in.read(bt, 0, persize);
sb.append(new String(bt,"GBK"));
System.out.println(sb.toString());
((TestMonitor) frame).area.append(sb.toString());
((TestMonitor) frame).area.setCaretPosition(((TestMonitor) frame).area.getDocument().getLength()-1); 
monitor.setNote("读取中: "+i+"%");
monitor.setProgress(i);
if(monitor.isCanceled())
{
in.close();
break;
}
if(bt.length==0)
{
monitor.setNote("文件读取完成");
monitor.close();
in.close();
break;
}
} catch (InterruptedException e) {
//e.printStackTrace();
} catch (IOException e) {
//e.printStackTrace();
}
}
}


}

</code>
测试类
<code>
package cn.com.metalWork.util;


import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class TestMonitor extends JFrame {


private static final long serialVersionUID = -731731615094423134L;
public Thread t =null;
public JPanel jPanel=null;
public JScrollPane jScrollPane=null;
public JButton button = null;
public JTextArea area= null;
public void initUI()
{

this.setLayout(new BorderLayout());
TaskReadFile r= new TaskReadFile(this);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
jPanel =new JPanel();
area= new JTextArea();
jScrollPane= new JScrollPane(area);
button = new JButton("开始读取");
button.addActionListener(new ReadFileListener());
//jScrollPane.setSize(1000, 890);
jPanel.add(button);
this.getContentPane().add(jScrollPane,BorderLayout.CENTER);
this.getContentPane().add(jPanel,BorderLayout.SOUTH);
t = new Thread(r);
}

class ReadFileListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
t.start();
}
}

public static void main(String[] args) {
TestMonitor tm= new TestMonitor();
tm.initUI();
tm.setVisible(true);
tm.setSize(1000,800);
}


}
</code>
这个测试功能主要用到了ProgressMonitor的用法,大家可以参考一下,具体各个方法的用法大家可以参考一下java se API。