gpt4 book ai didi

java - 进度条: how to create deep copy of original object

转载 作者:行者123 更新时间:2023-12-01 14:05:08 24 4
gpt4 key购买 nike

我正在尝试在 Java 中使用类似于下面的进度条:

public class MyProgressSplashScreen extends JWindow
{
private final JProgressBar progressbar;
private final ExecutorService autoProgressExecutor = Executors.newFixedThreadPool(1);

public MyProgressSplashScreen(final int theMin, final int theMax)
{
super();
final JPanel contentPanel = new JPanel(new BorderLayout());
contentPanel.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
if (theMin != -1 && theMax != -1)
{
progressbar = new JProgressBar(SwingConstants.HORIZONTAL, theMin, theMax);
}
else
{
progressbar = new JProgressBar(SwingConstants.HORIZONTAL);
progressbar.setIndeterminate(true);
}
progressbar.setStringPainted(true);
contentPanel.add(progressbar, BorderLayout.SOUTH);
add(contentPanel);
pack();
setAlwaysOnTop(true);
}

public void showProgress(final int theValueTo, final int theEstimatedTimeInSeconds)
{
showProgress(progressbar.getValue(), theValueTo, theEstimatedTimeInSeconds);
}

public void showProgress(final int theValueFrom, final int theValueTo,
final int theEstimatedTimeInSeconds)
{
setVisible(true);
autoProgressExecutor.execute(new Runnable()
{
@Override
public void run()
{
int numberOfSteps = theValueTo - theValueFrom;
long timeToWait = TimeUnit.SECONDS.toMillis(theEstimatedTimeInSeconds)
/ numberOfSteps;
for (int i = theValueFrom; i <= theValueTo; i++)
{
progressbar.setValue(i);
try
{
TimeUnit.MILLISECONDS.sleep(timeToWait);
}
catch (final InterruptedException e) { }
}
if (progressbar.getValue() == 100) { setVisible(false); }
}
});
}
}

但是,我无法传递 MyProgressSplashScreen 的副本以便让单独的线程更新进度。例如,下面的程序从 0 开始计数到 ​​10,然后从 0 重新开始计数到 ​​30,但它不应该重置为零!

public class TestSplashScreen
{
private final MyProgressSplashScreen myProgressSplashScreen = new MyProgressSplashScreen(-1,-1);

public static void main(String args[])
{
TestSplashScreen testInvoke = new TestSplashScreen();
testInvoke.synchronize();
}

public void synchronize()
{
Runnable runnable = new Runnable()
{
@Override
public void run()
{
myProgressSplashScreen.showProgress(10, 2);
myProgressSplashScreen.toFront();

MyRunnable myRunnable = new MyRunnable();
myRunnable.setSyncProgressSplashScreen(myProgressSplashScreen);
Thread t1 = new Thread(myRunnable);
t1.start();
}
};
runnable.run();
}
}
class MyRunnable implements Runnable
{
MyProgressSplashScreen syncProgressSplashScreen;
public void setSyncProgressSplashScreen(MyProgressSplashScreen syncProgressSplashScreen)
{
this.syncProgressSplashScreen = syncProgressSplashScreen;
}

@Override
public void run()
{
syncProgressSplashScreen.showProgress(30, 3);
}
}

最佳答案

问题是您调用 syncProgressSplashScreen.showProgress 2 次。第一次它会阻塞线程,导致其从 0 增加到 10,然后您再次从 0 到 30 调用它。删除显示 myProgressSplashScreen.showProgress(10, 2); 的行,它就不会做2次。另外我注意到您没有设置进度条的最大值,因此除非您调用 myProgressSplashScreen.showProgress(100, 2) 它不会达到 100%。

关于java - 进度条: how to create deep copy of original object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18961057/

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