JProgressBar使用示例

通过JAVA读取文件来演示JProgressBar
jar包在附件中……
import java.awt.FileDialog;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import javax.swing.JFrame;
import javax.swing.UnsupportedLookAndFeelException;

/*
* TestFrame.java
*
* Created on 2008年10月8日, 下午2:02
*/


/**
*
* @author lzkj
*/
public class TestProgressBar extends javax.swing.JFrame {

/** Creates new form TestFrame */
public TestProgressBar() {
initComponents();
}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
this.setTitle("JProgress Bar Demo");
setLocation(300,300);
bar = new javax.swing.JProgressBar();
info = new javax.swing.JLabel();
open = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

bar.setVisible(false);
bar.setValue(0);
bar.setStringPainted(true);

open.setText("Open");
open.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
FileDialog fd = new FileDialog(new JFrame(),"打开文件",FileDialog.LOAD);
fd.show(true);
String path = fd.getDirectory();
String fn = fd.getFile();
info.setText("FilePath:"+path+fn);
readFile(path+fn);
open.setEnabled(false);
}

});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(info, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 354, Short.MAX_VALUE)
.addComponent(open, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(bar, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 354, Short.MAX_VALUE))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(open)
.addGap(18, 18, 18)
.addComponent(info, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(bar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);

pack();
}// </editor-fold>

public void readFile(String fname){
new Thread(new ReadThread(fname)).start();
}

class ReadThread implements Runnable{
String fileName;
public ReadThread(String fn){
fileName = fn;
}
public void run() {
// TODO Auto-generated method stub
System.out.println("thread start......");
long startTime = System.currentTimeMillis();
File file = new File(fileName);

InputStream in;
try {
in = new FileInputStream(file);
// BufferedReader reader = new BufferedReader(new InputStreamReader(in));
bar.setMaximum((int)file.length());
bar.setVisible(true);
int cv = 0;
int size = 0;
byte[] cache = new byte[256];
while((size = in.read(cache))>0){
cv+=size;
System.out.println("currentSize:"+size);
bar.setValue(cv);
}
System.out.println("TotalSize:"+cv);
System.out.println("FileSize:"+file.length());
bar.setVisible(false);
bar.setValue(0);
long finishTime = System.currentTimeMillis();
info.setText("Finish... use time : "+(finishTime-startTime)+"ms");
open.setEnabled(true);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
}

}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
try {
javax.swing.UIManager.setLookAndFeel("com.birosoft.liquid.LiquidLookAndFeel");
com.birosoft.liquid.LiquidLookAndFeel.setLiquidDecorations(true);
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new TestProgressBar().setVisible(true);
}
});
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

// Variables declaration - do not modify
private javax.swing.JProgressBar bar;
private javax.swing.JLabel info;
private javax.swing.JButton open;
// End of variables declaration

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值