gpt4 book ai didi

Java swing、SwingWorker、进程栏不会更新

转载 作者:行者123 更新时间:2023-11-30 03:55:14 24 4
gpt4 key购买 nike

我的 Swingworker 不会重新绘制我的进度条(我有 2 个类(class))。

这是我的文件下载器代码。它将下载百分比显示在进度栏中。

public class Downloader extends SwingWorker<String, Integer> {

private String fileURL, destinationDirectory;
private int fileTotalSize;

public void DownloaderF(String file, String dir) {
this.fileURL = file;
this.destinationDirectory = dir;
}

@Override
protected String doInBackground() throws Exception {
try {
URL url = new URL(fileURL);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
String downloadedFileName = fileURL.substring(fileURL.lastIndexOf("/")+1);
int filesize = httpConn.getContentLength();
int responseCode = httpConn.getResponseCode();
byte[] buffer = new byte[4096];
int bytesRead = 0;
int i = 0;
int total = 0;
BufferedInputStream in = new BufferedInputStream(httpConn.getInputStream());
FileOutputStream fos = new FileOutputStream(destinationDirectory + File.separator + downloadedFileName);
BufferedOutputStream bout = new BufferedOutputStream(fos,4096);
while ((i=in.read(buffer,0,4096))>=0) {
total = total + i;
bout.write(buffer,0,i);
fileTotalSize = (total * 100) / filesize;
publish(fileTotalSize);
}
bout.close();
in.close();
} catch(FileNotFoundException FNFE) {
System.out.print("HTTP: 404!");
} catch (IOException ex) {
Logger.getLogger(Downloader.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}

@Override
protected void process(List<Integer> chunks) {
try {
Skin barValue = new Skin();
barValue.setBar(fileTotalSize);
//System.out.print("PROCESS:" + fileTotalSize + "\n");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}

这是我的按钮代码和进度条值更改方法:

private void LoginButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
// TODO add your handling code here:
// Дебаг
Downloader downloadFile = new Downloader();
downloadFile.DownloaderF("http://ipv4.download.thinkbroadband.com/100MB.zip", ".");
downloadFile.execute();
}

public void setBar(int Value) {
DownloadBar.setValue(Value);
DownloadBar.repaint();

System.out.print("1\n");
}

将打印“1\n”,但进度条不会移动。

抱歉我的英语不好。

最佳答案

您的问题很可能来自这一行:

Skin barValue = new Skin();

您正在重新创建 Skin 类的新实例,而不是引用已存在的实例。因此,您很可能指向一些可能根本没有显示的东西,因此您看不到任何事情发生。

正确的方法是向您的类 Downloader 提供对包含显示的“进度条”的原始 Skin 的引用。

仅供引用:

  • 当您更改 JProgressBar 的值时,无需调用 repaint()(进度条会为您执行此操作)
  • 请遵循 Java 命名约定(即方法名称和变量必须以小写字母开头):对于有经验的用户来说,您的代码更难阅读。

这是从您的代码中派生的示例代码(尽管我做了一些快捷方式),它实际上按预期正常工作:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;

public class Skin {

private JProgressBar DownloadBar;

public static class Downloader extends SwingWorker<String, Integer> {

private final String fileURL, destinationDirectory;
private int fileTotalSize;
private final Skin barValue;

public Downloader(Skin skin, String file, String dir) {
this.barValue = skin;
this.fileURL = file;
this.destinationDirectory = dir;
}

@Override
protected String doInBackground() throws Exception {
try {
URL url = new URL(fileURL);
HttpURLConnection httpConn = (HttpURLConnection) url
.openConnection();
String downloadedFileName = fileURL.substring(fileURL
.lastIndexOf("/") + 1);
int filesize = httpConn.getContentLength();
int responseCode = httpConn.getResponseCode();
byte[] buffer = new byte[4096];
int bytesRead = 0;
int i = 0;
int total = 0;
BufferedInputStream in = new BufferedInputStream(
httpConn.getInputStream());
FileOutputStream fos = new FileOutputStream(
destinationDirectory + File.separator
+ downloadedFileName);
BufferedOutputStream bout = new BufferedOutputStream(fos, 4096);
while ((i = in.read(buffer, 0, 4096)) >= 0) {
total = total + i;
bout.write(buffer, 0, i);
fileTotalSize = total * 100 / filesize;
publish(fileTotalSize);
}
bout.close();
in.close();
} catch (FileNotFoundException FNFE) {
System.out.print("HTTP: 404!");
} catch (IOException ex) {
Logger.getLogger(Downloader.class.getName()).log(Level.SEVERE,
null, ex);
}
return null;
}

@Override
protected void process(List<Integer> chunks) {
barValue.setBar(fileTotalSize);
}
}

private void loginButtonActionPerformed(java.awt.event.ActionEvent evt) {
Downloader downloadFile = new Downloader(this,
"http://ipv4.download.thinkbroadband.com/100MB.zip", ".");
downloadFile.execute();
}

protected void initUI() throws MalformedURLException {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton login = new JButton("Login");
login.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
loginButtonActionPerformed(e);
}
});
DownloadBar = new JProgressBar();
frame.add(login, BorderLayout.NORTH);
frame.add(new JLabel(new ImageIcon(new URL(
"http://home.scarlet.be/belperret/images/image1.jpg"))));
frame.add(DownloadBar, BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
}

public void setBar(int Value) {
DownloadBar.setValue(Value);
DownloadBar.repaint();

System.out.println("1");
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
new Skin().initUI();
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
});
}
}

关于Java swing、SwingWorker、进程栏不会更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23394904/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com