创建进度条以更新 java 中 for 循环的结果
Creating a Progress Bar to update with results from a for loop in java
我创建了一个从 0 到 850000 计数的 for 循环。我选择了一个较大的数字,因为我想创建一个进度条,该进度条将使用 for 循环中的结果进行更新。这是我到目前为止的代码。
public class ProgressBarTest extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ProgressBarTest frame = new ProgressBarTest();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public ProgressBarTest() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
for(int i = 850000; i>0; i--) {
System.out.println("i = " + i);
}
}
}
我似乎找不到使用 for 循环更新进度条的方法。我确实创建了一个在计时器上工作的进度条,但这不是我想要的。我基本上希望它从 0% 更新到 100%。这对我来说似乎很简单,但我已经坚持了一段时间。我很感激能帮助我理解它是如何工作的任何类型的示例代码。
你需要用到SwingWorker,当一个Swing程序需要执行一个长时间的运行任务时,它通常会使用其中一个工作线程。所以,试试这个:
public class JProgressBarTest extends SwingWorker<Void,Integer> {
private JProgressBar jpb;
public ProgrssBarTest(JProgressBar jpb){
this.jpb = jpb;
}
@Override
protected void process(List<Integer> chunks){
int i = chunks.get(chunks.size()-1);
jpb.setValue(i);
}
@Override
protected void doInBackground() throws Exception{
int x = 0;
for(int i = 850000; i>0; i--) {
publish(x); //replace x with the number you want to increment the progressbar each time pubish is called
x = x + i; //or replace i with the increment you want
System.out.println("i = " + i);
}
}
@Override
protected void done(){
try{
get();
}catch(ExecutionException |InterruptedException e){
;
}
}
}
在你的 class 中,通过调用我上面写的 class 的新 class 来替换循环,并向它传递一个新的 JProgressBar() 并添加 .execute(),比如这个:
new JProgressBarTest(new JProgressBar()).execute();
这是完整的示例,使用 Saran
提出的 SwingWorker
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingWorker;
import javax.swing.border.EmptyBorder;
public class ProgressBarTest extends JFrame {
private static final long LOOP_LENGTH = 85000000;
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
ProgressBarTest frame = new ProgressBarTest();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public ProgressBarTest() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
JProgressBar progress = new JProgressBar();
progress.setStringPainted(true);
contentPane.add(new JLabel("Loop progress is: "), BorderLayout.NORTH);
contentPane.add(progress, BorderLayout.SOUTH);
setContentPane(contentPane);
ProgressWorker worker = new ProgressWorker(progress);
worker.execute();
}
private static class ProgressWorker extends SwingWorker<Void, Integer> {
private final JProgressBar progress;
public ProgressWorker(JProgressBar progress) {
this.progress = progress;
}
@Override
protected Void doInBackground() throws Exception {
for (long i = LOOP_LENGTH; i > 0; i--) {
final int progr = (int) ((100L * (LOOP_LENGTH - i)) / LOOP_LENGTH);
publish(progr);
}
return null;
}
@Override
protected void process(List<Integer> chunks) {
progress.setValue(chunks.get(chunks.size() - 1));
super.process(chunks);
}
@Override
protected void done() {
progress.setValue(100);
}
}
}
我创建了一个从 0 到 850000 计数的 for 循环。我选择了一个较大的数字,因为我想创建一个进度条,该进度条将使用 for 循环中的结果进行更新。这是我到目前为止的代码。
public class ProgressBarTest extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ProgressBarTest frame = new ProgressBarTest();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public ProgressBarTest() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
for(int i = 850000; i>0; i--) {
System.out.println("i = " + i);
}
}
}
我似乎找不到使用 for 循环更新进度条的方法。我确实创建了一个在计时器上工作的进度条,但这不是我想要的。我基本上希望它从 0% 更新到 100%。这对我来说似乎很简单,但我已经坚持了一段时间。我很感激能帮助我理解它是如何工作的任何类型的示例代码。
你需要用到SwingWorker,当一个Swing程序需要执行一个长时间的运行任务时,它通常会使用其中一个工作线程。所以,试试这个:
public class JProgressBarTest extends SwingWorker<Void,Integer> {
private JProgressBar jpb;
public ProgrssBarTest(JProgressBar jpb){
this.jpb = jpb;
}
@Override
protected void process(List<Integer> chunks){
int i = chunks.get(chunks.size()-1);
jpb.setValue(i);
}
@Override
protected void doInBackground() throws Exception{
int x = 0;
for(int i = 850000; i>0; i--) {
publish(x); //replace x with the number you want to increment the progressbar each time pubish is called
x = x + i; //or replace i with the increment you want
System.out.println("i = " + i);
}
}
@Override
protected void done(){
try{
get();
}catch(ExecutionException |InterruptedException e){
;
}
}
}
在你的 class 中,通过调用我上面写的 class 的新 class 来替换循环,并向它传递一个新的 JProgressBar() 并添加 .execute(),比如这个:
new JProgressBarTest(new JProgressBar()).execute();
这是完整的示例,使用 Saran
提出的SwingWorker
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingWorker;
import javax.swing.border.EmptyBorder;
public class ProgressBarTest extends JFrame {
private static final long LOOP_LENGTH = 85000000;
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
ProgressBarTest frame = new ProgressBarTest();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public ProgressBarTest() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
JProgressBar progress = new JProgressBar();
progress.setStringPainted(true);
contentPane.add(new JLabel("Loop progress is: "), BorderLayout.NORTH);
contentPane.add(progress, BorderLayout.SOUTH);
setContentPane(contentPane);
ProgressWorker worker = new ProgressWorker(progress);
worker.execute();
}
private static class ProgressWorker extends SwingWorker<Void, Integer> {
private final JProgressBar progress;
public ProgressWorker(JProgressBar progress) {
this.progress = progress;
}
@Override
protected Void doInBackground() throws Exception {
for (long i = LOOP_LENGTH; i > 0; i--) {
final int progr = (int) ((100L * (LOOP_LENGTH - i)) / LOOP_LENGTH);
publish(progr);
}
return null;
}
@Override
protected void process(List<Integer> chunks) {
progress.setValue(chunks.get(chunks.size() - 1));
super.process(chunks);
}
@Override
protected void done() {
progress.setValue(100);
}
}
}