gpt4 book ai didi

java - JProgressBar不会动态更新

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

我有一个 JFrame 类,里面有这个函数:

o=1;
private String performTest(int i,File file) throws IOException, InterruptedException {
//CRAPPY CODE
while(o<=20) {
Thread.sleep(1000);
progressBar.setValue((o/20)*100);
o++;
}
//CRAPPY CODE
}

所以,进度条应该每秒更新一个更大的值,但我得到的只是 1 个最终更新(到 100),当函数完成它的工作时;也许有一些方法可以解决这个问题?谢谢你的期待。 完整代码。

   btnTesteaz.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
int i = choice.getSelectedIndex();
File file = fc.getSelectedFile();


StringBuilder builder = new StringBuilder();
Thread t = new Thread(new Runnable() {
@Override
public void run() {

String name = Problems[i];
int o = 1;
try {

String pathToPas = file.getPath();
String pathToExe = pathToPas.replace(".pas", ".exe");
Process process = Runtime.getRuntime().exec("ppc386 "+pathToPas);
Thread.sleep(1000);
process.destroy();

while (new File("Res\\"+Problems[i]+"\\"+"Input"+o+".txt").exists()) {
//FILES WITH INPUTS FROM RES
File in = new File("Res\\"+Problems[i]+"\\"+"Input"+o+".txt");
//FILES FOR .PAS
File inputtxt = new File(file.getParent()+"\\Input.txt");



BufferedReader bf = new BufferedReader(new FileReader(in));
PrintWriter w = new PrintWriter(new FileWriter(inputtxt));
String s = null;
while ((s = bf.readLine())!=null) {
w.println(s);
}
w.close();
bf.close();
Process process1 = Runtime.getRuntime().exec(pathToExe,null, new File(file.getParent()));
Thread.sleep(1000);
process1.destroy();

File outputtxt = new File(file.getParent()+"\\Output.txt");
File out = new File("Res\\"+Problems[i]+"\\"+"Output"+o+".txt");
BufferedReader r1 = new BufferedReader(new FileReader(out));
BufferedReader r2 = new BufferedReader(new FileReader(outputtxt));
StringBuilder ansRes = new StringBuilder();
StringBuilder ansPas = new StringBuilder();
while ( (s=r1.readLine())!=null) ansRes.append(s);
while ( (s=r2.readLine())!=null) ansPas.append(s);

if (ansRes.toString().trim().equals(ansPas.toString().trim())) {
builder.append("<p font color=green>"+(o)+". Test succesful </p> ") ;
}
else builder.append("<p font color=red>"+(o)+". Test failed </p>");
r1.close();
r2.close();

inputtxt.delete();
outputtxt.delete();

final int temp = o;
SwingUtilities.invokeLater(new Runnable() {
public void run() {
model.setValue((int) (((float) temp / 20.0f) * 100));
}
});
o++;
}
File f = new File(pathToExe);
f.delete();
File fa = new File(file.getAbsolutePath().replace(".pas",".o"));
fa.delete();
}
catch (Exception e) {
e.printStackTrace();
}
}
});
t.run();
Result.setText(builder.toString());
}
});

最佳答案

这是 Swing 中的一个经典问题。 Swing 从其主线程进行所有图形更新,因此您不应该从那里执行长时间操作(您不会让它重新绘制!)

尝试将长时间操作转移到另一个线程,如本教程所示:

https://docs.oracle.com/javase/tutorial/uiswing/concurrency/worker.html

这将教您如何在后台执行长时间耗时的任务,同时实时更新 UI,而不会扰乱 Swing 架构

关于java - JProgressBar不会动态更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27469334/

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