我想创建一个复制文件的swing工作器,并在进度条中显示进度。虽然我复制了粘贴我在博客中找到的代码,但是除了process()函数之外,所有工作都很正常 - 我不记得显示给你的链接,我收到了编译错误。这是我的代码:
class Copy extends SwingWorker<Void, Void>
{
File src,dest;
InputStream in;
OutputStream out;
JProgressBar bar;
public Copy(File source,File dst)
{
src=source;
dest=dst;
//bar=Br;
progressBar.setValue(0);
}
public Copy(MouseAdapter mouseAdapter) {
// TODO Auto-generated constructor stub
}
protected Void doInBackground() throws Exception
{
System.out.print("Started\n");
InputStream input = null;
OutputStream output = null;
try {
input = new FileInputStream(src);
output = new FileOutputStream(dest);
byte[] buf = new byte[1024];
int bytesRead;
while ((bytesRead = input.read(buf)) > 0) {
output.write(buf, 0, bytesRead);
}
} finally {
input.close();
output.close();
}
progressBar.setVisible(true);
return null;
}
//Here I get the error:
@Override //Description Resource Path Location Type
//The method process() of type Main.Copy must override or implement a supertype method
protected Void process()
{
long expectedBytes = src.length(); // This is the numbe
r of bytes we expected to copy..
byte[] buffer = new byte[1024];
int length;
long totalBytesCopied = 0; // This will track the total number of bytes we've copied
try {
while ((length = in.read(buffer)) > 0){
out.write(buffer, 0, length);
totalBytesCopied += length;
int progress = (int)Math.round(((double)totalBytesCopied / (double)expectedBytes) * 100);
System.out.print(progress);
progressBar.setValue(progress);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// return progress;
}
protected void done()
{
System.out.print("Complete\n");
}
}
}
我无法理解我做错了什么,所以会得到一点帮助。 还有什么区别Void
例如protected Void doInBackground()
和无效
例如protected void done()
我目前在Eclipse工作,这两个在编辑器中有不同的颜色,所以它们之间存在差异。