【发布时间】:2011-01-15 04:03:13
【问题描述】:
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 新手,所以如果我做错了,我深表歉意。
【问题讨论】:
标签: java multithreading swing user-interface swingworker