关于Java中进度条的问题
为什么我在读取一个文件时进度条一直不走,一直0%,而到了文件读取完成时就一下子跳到100%了,中间的过程进度条工程进度条一直在0%处不动?//item为读取菜单pro为进...
为什么我在读取一个文件时进度条一直不走,一直0%,而到了文件读取完成时就一下子跳到100%了,中间的过程进度条工程进度条一直在0%处不动?
//item为读取菜单 pro为进度条
item.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
File file = new File("F:/111.wmv");
long size = file.length();
byte[] bts = new byte[1024*1024];
int length = 0;
long cLength = 0;
try {
InputStream stream = new FileInputStream(file);
OutputStream os = new FileOutputStream(new File("F:/q.wmv"));
while((length=stream.read(bts))>0){
cLength += length;
os.write(bts,0,length);
int value = (int)(cLength*100/size);
pro.setValue(value);
}
os.close();
stream.close();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}); 展开
//item为读取菜单 pro为进度条
item.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
File file = new File("F:/111.wmv");
long size = file.length();
byte[] bts = new byte[1024*1024];
int length = 0;
long cLength = 0;
try {
InputStream stream = new FileInputStream(file);
OutputStream os = new FileOutputStream(new File("F:/q.wmv"));
while((length=stream.read(bts))>0){
cLength += length;
os.write(bts,0,length);
int value = (int)(cLength*100/size);
pro.setValue(value);
}
os.close();
stream.close();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}); 展开
2个回答
展开全部
import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.SwingWorker;
public class Bar extends JFrame implements PropertyChangeListener
{
private static final long serialVersionUID = 1L;
private JProgressBar progressBar;
private JButton copy;
private Task task;
public Bar(String title)
{
setTitle(title);
setLayout(new BorderLayout());
progressBar = new JProgressBar(0, 100);
progressBar.setValue(0);
progressBar.setStringPainted(true);
add(progressBar, BorderLayout.CENTER);
copy = new JButton("拷贝");
copy.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
start();
copy.setEnabled(false);
}
});
add(copy, BorderLayout.EAST);
}
class Task extends SwingWorker<Void, Void>
{
@Override
public Void doInBackground()
{
setProgress(0);
File file = new File("aa.mp3");
long size = file.length();
byte[] bts = new byte[1024 * 10];
int length = 0;
long cLength = 0;
try
{
InputStream stream = new FileInputStream(file);
OutputStream os = new FileOutputStream(new File("a.mp3"));
while((length = stream.read(bts)) > 0)
{
os.write(bts, 0, length);
cLength += length;
setProgress((int) (cLength * 100 / size));
Thread.sleep(100);
}
os.close();
stream.close();
}
catch(Exception e1)
{
e1.printStackTrace();
}
return null;
}
@Override
public void done()
{
setTitle("拷贝完毕");
Toolkit.getDefaultToolkit().beep();
copy.setEnabled(true);
setCursor(null);
}
}
public void propertyChange(PropertyChangeEvent evt)
{
if("progress" == evt.getPropertyName())
{
int progress = (Integer) evt.getNewValue();
progressBar.setValue(progress);
}
}
private void start()
{
task = new Task();
task.addPropertyChangeListener(this);
task.execute();
setTitle("正在拷贝...");
setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
}
private static void createAndShowGUI()
{
Bar frame = new Bar("Bar");
frame.setAlwaysOnTop(true);
frame.setSize(280, 60);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args)
{
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询