gpt4 book ai didi

java - 有没有办法显示另一个类中函数的进度?

转载 作者:行者123 更新时间:2023-12-02 09:30:57 25 4
gpt4 key购买 nike

我在制作进度条时遇到了麻烦。我希望该栏显示“GiveRating”功能的进度。它不必非常精确。我是Java新手,所以请用简单的话解释一下。我正在使用 Java SWT 作为 GUI

// this is the new progress bar which is showing in a shell:

ProgressBar progressBar = new ProgressBar(shlRatingScheme, SWT.NONE);
progressBar.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
progressBar.setBounds(10, 208, 170, 17);


// this is the button that's starts the process:

btnFotosRaten.setText("Foto's raten");
btnFotosRaten.setBounds(185, 270, 75, 25);
btnFotosRaten.addListener(SWT.Selection, new Listener(){
public void handleEvent(Event e) {switch (e.type) {case SWT.Selection:
PhotoRating d = new PhotoRating();
if(sFolder == null | dFolder == null) {
String message ="Vul bronmap en doelmap in AUB!";
JOptionPane.showMessageDialog(new JFrame(), message, "Oeps", JOptionPane.ERROR_MESSAGE);
}else {
d.GiveRating(dFolder);

// so I would like to let the progress bar show how far d.GiveRating(dfolder) is. Is there a simple way to do that?

}
String message ="De foto's in "+dFolder+" zijn gerated!"; JOptionPane.showMessageDialog(new JFrame(), message, "Info", JOptionPane.INFORMATION_MESSAGE);
break;
}
}
});

最佳答案

您无法“观看”函数运行时的情况。你能做的就是让函数告诉它它做了什么。

您可以实现称为“观察者模式”的东西( https://en.wikipedia.org/wiki/Observer_pattern )。在您的情况下,您将需要一个中间类来观察函数的运行,并且对于每个事件,它将更新您的进度条。

类似的东西

public class ProgressBarObserver implements Observer{
ProgressBar pb;

public ProgressBarObserver(ProgressBar pb){
this.pb = pb;
}

@Override
public void update(int progress){
pb.step(progress); // cannot recall the correct function for progressbar
}
}

public class Worker implements Observable{
Observer o;
public Worker(Observer o){
this.o = o;
}

public void doSomething(){
//something
o.update(1);
//something else
o.update(1);
//some other stuff
o.update(1);
//and so on
}
}

public class Manager {
public static void main(String... args){
ProgressBar pb = new ProgressBar();
ProgressBarObserver pbo = new ProgressBarObserver(pb);
Worker w = new Worker(pbo);

w.doSomething();
}
}

希望对您有帮助!

关于java - 有没有办法显示另一个类中函数的进度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57991987/

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