gpt4 book ai didi

java - 发送文件时无法更新 GUI

转载 作者:行者123 更新时间:2023-12-01 06:17:25 25 4
gpt4 key购买 nike

我正在使用套接字通过网络发送文件。文件已正确接收,没有任何问题。但现在我使用 JProgressBar 来显示已发送文件的百分比。我的问题是,即使我在单独的线程中更新 GUI,进度条也只有在文件完全发送后才会更新。我还尝试将主线程的优先级调整为 Thread.MIN_PRIORITY 但问题仍然存在。

完整的代码很长,所以我不会发布它(如果有人问我会发布)。发送文件和更新进度条的简短代码是

final double temp=(done/fileSize)*100; // percentage of file sent
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
try
{
jpb.setString("Completed : "+temp+" %");
jpb.setValue((int)temp); // this is executed only when 100%
}
catch(Exception e)
{
e.printStackTrace();
}
}
});
System.out.println(temp); // only this and
bos.write(b,0,read); // this statement is executed

最佳答案

问题出在下面一行:

final double temp=(done/fileSize)*100; // percentage of file sent

如果donefileSize都不是double,则done/fileSize的结果为0.0。

将它们加倍(至少其中一个)以保留除法的小数部分。

关于java - 发送文件时无法更新 GUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21065624/

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