public class download {
public static void Download() {
final String saveTo = System.getProperty("user.home").replace("\\", "/") + "/Desktop/";
try {
URL url = null;
url = new URL("http://cachefly.cachefly.net/10mb.test");
ReadableByteChannel rbc = Channels.newChannel(url.openStream());
FileOutputStream fos = new FileOutputStream(saveTo + "10mb.test");
fos.getChannel().transferFrom(rbc, 0, 1 << 24);
} catch (Exception e) {
e.printStackTrace();
}
}
在我的另一堂课中,我有一个事件监听器
public void download_buttonActionPerformed(ActionEvent e) {
download_button.setEnabled(false);
label_status.setText("- Downloading...");
download.Download();
}
当我单击 GUI 上的按钮时,它会冻结,并且标签和按钮在下载文件之前永远不会改变:
http://img200.imageshack.us/img200/2435/45019860.png
我必须在新线程上开始下载吗?如果我在新线程上启动它,是否仍然可以使用进度条?我对 java 还是很陌生,所以如果我做错了,我深表歉意。