//引用进度条
private class Cal extends Thread{
private String path;
private ClockingTimeListUI listUI;
private ActionEvent e;
public Cal(String path,ClockingTimeListUI listUI,ActionEvent e) {
this.path=path;
this.listUI=listUI;
this.e=e;
}
public void run() {
try {
StringBuffer result=importAttendanceData(path);
MsgBox.showDetailAndOK(null,"导入成功,点击详细信息查看详情",result.toString(),1);
listUI.actionRefresh_actionPerformed(e);
} catch (Exception e) {
MsgBox.showInfo(null, e.getMessage());
}finally{
progressBar.setVisible(false);
}
}
}
//进度条
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Rectangle;
import java.util.StringTokenizer;
import java.util.Vector;
import javax.swing.JWindow;
/**
* 进度条
* @author yell
*
*/
public class YDProgressBar extends JWindow{
private static final long serialVersionUID = 1L;
/**
* 启动窗口内容面板
*/
private JPanel jContentPane = null;
/**
* 启动进度提示文本标签
*/
private JLabel dynamicLabel = null;
/**
* 启动进度提示静态文本标签
*/
private JLabel stateLabel = null;
/**
* 启动进度条
*/
private JProgressBar jpb = null;
/**图片的位置*/
private int imgX=0,imgY=0,imgWidth=400,imgHeight=110;
/**文字的位置*/
private int txtX=10,txtY=20,txtWidth=330,line=3;
/**显示的文字*/
protected Vector vectorLines;
/**背景图片*/
private Image bgImage = null;
/**文字提示的最多行数*/
private final int maxLine=9;
/**消息*/
private String info=null;
/**文字行高*/
private final int lineHeight = 15;
/**
* 构造方法,完整窗口的初始化工作
*/
public YDProgressBar(String info,Image bgImage) {
this.info=info;
this.bgImage=bgImage;
jpb = new JProgressBar(0, 100);
if(info!=null&&!info.trim().equals("")){
initStringArray();
}else{
this.setSize(imgWidth, imgHeight+50);
initialize();
}
}
/**
* 进度条get方法
*/
public JProgressBar getJpb() {
return jpb;
}
/**
* 启动窗口初始化方法
*/
private void initialize() {
this.setContentPane(getJContentPane());
Dimension dim = this.getToolkit().getScreenSize();
Rectangle rt = this.getBounds();
this.setLocation((dim.width - rt.width) / 2,
(dim.height - rt.height) / 2);
}
/**
* 启动窗口主体内容面板
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel() {
private static final long serialVersionUID = 1L;
/**
* 重载paint画出启动窗口中的图片
*/
public void paint(Graphics g) {
super.paint(g);
if(bgImage!=null){
g.drawImage(bgImage, imgX,imgY,imgWidth,imgHeight, this);
}
if (vectorLines != null) {
for (int i = 0; i < vectorLines.size(); i++) {
g.drawString(((StringBuffer) vectorLines.elementAt(i)).toString(),
txtX, txtY + i * lineHeight);
}
}
}
};
jContentPane.setLayout(new BorderLayout());
JPanel footPane = new JPanel();
footPane.setLayout(new GridLayout(2, 1));
footPane.setBackground(new Color(84, 141, 208));
JPanel txtPanel=new JPanel();
txtPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
txtPanel.add(stateLabel=new JLabel());
txtPanel.add(dynamicLabel = new JLabel("数据加载中,请稍后..."));
footPane.add(txtPanel);
jpb.setBorderPainted(false);
jpb.setBackground(new Color(84, 141, 208));
footPane.add(jpb);
jContentPane.add(footPane, BorderLayout.SOUTH);
}
return jContentPane;
}
/**
* 把字符串转换为字符串数组
*/
protected void initStringArray() {
FontMetrics fontMetrics = this.getFontMetrics(new Font("宋体",Font.PLAIN,12));
StringTokenizer st = new StringTokenizer(info, "/n");
String paragraphString = null;
vectorLines = new Vector(26);
StringBuffer lineString = null;
char word;
while (st.hasMoreTokens() && vectorLines.size() < maxLine) {
paragraphString = st.nextToken().trim();
System.out.println(paragraphString);
paragraphString = " " + paragraphString;
lineString = new StringBuffer();
int lineLength = 0;
for (int i = 0; i < paragraphString.length()
&& vectorLines.size() < maxLine; i++) {
word = paragraphString.charAt(i);
lineLength += fontMetrics.charWidth(word);
if (lineLength < txtWidth) {
lineString.append(word);
} else {
lineString.append(word);
vectorLines.add(lineString);
lineString = new StringBuffer();
lineLength = 0;
}
}
vectorLines.add(lineString);
}
String lastLineStr = vectorLines.get(vectorLines.size()-1).toString();
if(lastLineStr.trim().length() == 1)
{
if("!,.?!,。?".indexOf(lastLineStr.trim()) != -1)
{
vectorLines.remove(vectorLines.size()-1);
}
}
if (vectorLines.size() > line && vectorLines.size() <=maxLine) {// 默认3行,最多显示9行文本
int actHeight = vectorLines.size() * lineHeight;
setSize(new Dimension(imgWidth, actHeight+50));
System.out.println(vectorLines.size()+"行数");
} else {
System.out.println(vectorLines.size()+"行数");
if (vectorLines.size() > maxLine) {
setSize(new Dimension(imgWidth, maxLine * lineHeight+50));
}else{
setSize(new Dimension(imgWidth, line * lineHeight+50));
}
}
initialize();
}
public void setJpb(JProgressBar jpb) {
this.jpb = jpb;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
public JLabel getDynamicLabel() {
return dynamicLabel;
}
public void setDynamicLabel(JLabel dynamicLabel) {
this.dynamicLabel = dynamicLabel;
}
public JLabel getStateLabel() {
return stateLabel;
}
public void setStateLabel(JLabel stateLabel) {
this.stateLabel = stateLabel;
}
}