gpt4 book ai didi

java - 如何异步触发JProgressBar?

转载 作者:行者123 更新时间:2023-11-29 04:22:39 33 4
gpt4 key购买 nike

我在 JPanel 中有一个 JButton(swing),如果按下它,我将在 for 循环上执行任务EDT 线程 中的列表。在此过程中,我需要更新 JProgressBar。

问题是,当我按下 JButton 时,任务是在事件调度线程 (EDT) 中执行的。所以我无法更新在主线程或 UI 线程中触发的 JProgressBar

现在源代码对我来说不可用,因为我完全改变了它,并尝试使用 Eclipse SWT 来实现 JProgressBar 当 Swing JButton 被触发时,它变得困惑。

现在我收到了无效线程访问错误,因为 Display 对象在单独的 UI 线程中运行。一次,仅显示 Swing JPanel 或 Eclipse SWT Shell

我正在使用 JOptionPane 触发 JPanel

最佳答案

异步使用SwingWorker处理JProgressBar进度,详细使用两个Swing JButton btnStart和btnStop.

enter image description here

我们来获取源代码-

package com.practice;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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

public class ProgressExample {

private static JFrame frame;
private static JButton btnStart;
private static JButton btnStop;
private static JProgressBar progress;
private static Integer currValue;
private static boolean swingWorkerHelper;
private static SwingWorker<Integer, Void> swingWorker;

public static void main(String[] args) {
// Scheduling Swing app for the event dispatch thread(EDT) Asynchronously
SwingUtilities.invokeLater(new Runnable() {
public void run() {
runSwingAsynchronousApp();
}
});
}

private static void runSwingAsynchronousApp() {
currValue = new Integer(0);

btnStart = new JButton("Start");
btnStop = new JButton("Stop");

progress = new JProgressBar();
progress.setMinimum(0);
progress.setMaximum(100);
progress.setStringPainted(true);

btnStart.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {

if (btnStart.getText().equals("Pause")) {
// interrupting swingWorker method doInBackground() implicitly safely.
swingWorkerHelper = false;
btnStart.setText("Resume");
} else {
if (btnStart.getText().equals("Start") || btnStart.getText().equals("Resume"))
btnStart.setText("Pause");

showProgress();
}
}
});

btnStop.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
// checking swingWorker Object is initialized or not
if (swingWorker != null) {
// checking is swingWorker doing process in background or not
if (swingWorker.getState() == SwingWorker.StateValue.STARTED) {
btnStart.setText("Start");
// interrupting swingWorker method doInBackground() explicitly
swingWorker.cancel(true);
currValue = new Integer(0);
}
}
}
});

frame = new JFrame();
frame.setTitle("Asynchronously trigger JProgressBar");
frame.getContentPane().setLayout(new GridLayout(3, 2));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(400, 250, 400, 300);

frame.getContentPane().add(btnStart);
frame.getContentPane().add(btnStop);
frame.getContentPane().add(progress);

frame.setVisible(true);
}

private static void showProgress() {
swingWorker = new SwingWorker<Integer, Void>() {
@Override
protected Integer doInBackground() throws Exception {

if (!swingWorkerHelper)
swingWorkerHelper = true;

while (currValue < progress.getMaximum() && swingWorkerHelper) {
try {
progress.setValue(++currValue);

if (isCancelled())
return currValue;

Thread.sleep(70);
} catch (InterruptedException iex) {
swingWorkerHelper = false;
System.out.println("Stop Button interrupted SwingWorker process...");
}
}
return currValue;
}

@Override
public void done() {
System.out.printf("Progress ends with value %s " +
(isCancelled() ? "with" : "without") + " interruption.\n", currValue);
if (currValue >= progress.getMaximum()) {
btnStart.setText("Start");
currValue = new Integer(0);
}
}
};
swingWorker.execute();
}

}

希望这对您有帮助。

关于java - 如何异步触发JProgressBar?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48144967/

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