import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.JProgressBar; public class ProgressTest extends JDialog{ public JProgressBar jpb; public int Max=100; public int Min=0; public int Count=1; public JButton bStart; public ProgressTest() {
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 ProgressMonitorTest {
public static void main(String[] args) {
// 创建一个包含“Click me”的窗口
final JFrame f = new JFrame("ProgressMonitor Sample");
f.getContentPane().setLayout(new FlowLayout());
JButton b = new JButton("Click me");
f.getContentPane().add(b);
f.pack(); // 设置按钮的动作事件
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 这儿使用了新的线程处理按钮的动作事件,因为我们需要
//主窗口的线程响应用户。这样你可以多次点击该按钮,
//会启动多个读取文件的线程。主窗口也保持响应。
new Thread() {
public void run() {
try {
// 打开文件输出流,把InputStream包装在ProgressMonitorInputStream中。
//在当前目录中需要放置一个大文件,建议超过50M
InputStream in = new FileInputStream("bigfile.dat");
ProgressMonitorInputStream pm =
new ProgressMonitorInputStream(f,"Reading a big file",in);
// 读取文件,如果总耗时超过2秒,将会自动弹出一个进度监视窗口。
// 显示已读取的百分比。
int c;
while((c=pm.read()) != -1) {
// 处理代码
}
pm.close();
}
catch(Exception ex) {
ex.printStackTrace();
}
}
}.start();
}});
// 设置缺省的窗口关闭行为,并显示窗口。
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
new ProgressMonitorInputStream(f,"Reading a big file",in);
在JPanel中显示:
protected void paintComponent(Graphics g) {
// 先设置这个Panel的大小为可显示区域的大小再乘以如200%, 这个由你指定.
// this.setSize(....); // 或者由外部指定200%时调用这个函数设定些JPanel的大小. super.paintComponent(g); bufferedImage.draw(....);
bufferedImage.fill(....);
.... // 这里都是你要显示的东西, 如图片, 字符串等. g.drawImage(bufferedImage, 0, 0, width, height, null);
}注意, 这个JPanel是要加入在JScrollPane中, 而这个JScrollPane要加入一个显示区域, 如JPanel的中间, 可以使用BorderLayout.
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.JProgressBar;
public class ProgressTest extends JDialog{
public JProgressBar jpb;
public int Max=100;
public int Min=0;
public int Count=1;
public JButton bStart;
public ProgressTest() {
Container con=(JPanel)this.getContentPane();
con.setLayout(null);
bStart=new JButton("开始");
bStart.setBounds(10,10,70,30);
bStart.addActionListener(new MyActionListener());
jpb=new JProgressBar();
jpb.setBounds(80,20,300,20);
jpb.setMaximum(Max);
jpb.setMinimum(Min);
jpb.setAutoscrolls(true);
jpb.setStringPainted(true);//进度字
//jpb.setValue(50);
con.add(bStart);
con.add(jpb);
this.setSize(400,300);
this.setTitle("进度条的小程序");
this.setLocationRelativeTo(this);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args){
ProgressTest test=new ProgressTest();
}
private class MyActionListener implements ActionListener{
public void actionPerformed(ActionEvent e){
if(e.getSource().equals(bStart)){
System.out.println ("ok");
UpThread thread=new UpThread();
thread.start();
}
}
}
class UpThread extends Thread{
public UpThread(){ }
public void run(){
Count=Min;
while(Count <=Max){
Count++;
jpb.setValue(Count);
try{
if(Count <40){
this.sleep(50);
}
if(Count>=40&&Count <70){
this.sleep(200);
}
if(Count>=70){
this.sleep(50);
}
}catch(Exception ae){
ae.printStackTrace();
}
}
}
} }
以前的也许有帮助