如何显示进度条?多线程 java
How to display progress bar? multithreading java
我是 java 的初学者,如果我问了一个愚蠢的问题,我很抱歉,但是我如何在我的 gui class 中创建一个新线程来创建一个进度条。我有一个名为 progress 的 class 并使用我创建的构造函数在我的 gui class 中创建了一个新线程。但出于某种原因,我收到一个奇怪的错误:
"constructor progress in class NewJFrame.progress cannot be applied to given types;
required: no arguments
found: JProgressBar
reason: actual and formal argument lists differ in length
NewJframe.java
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if (jRadioButton1.isSelected()){
App m = new App();
Thread t1 = new Thread(new progress(jProgressBar1));
m.sendPingRequest2("104.160.142.3",jTextPane1,jTextPane2,jTextField1);
}
}
progress.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author User
*/
import javax.swing.JProgressBar;
public class progress implements Runnable {
private static int DELAY = 500;
JProgressBar progressBar;
public progress (JProgressBar bar) {
progressBar = bar;
}
public void run() {
int minimum = progressBar.getMinimum();
int maximum = progressBar.getMaximum();
for (int i = minimum; i < maximum; i++) {
try {
int value = progressBar.getValue();
progressBar.setValue(value + 1);
Thread.sleep(DELAY);
} catch (InterruptedException ignoredException) {
}
}
}
}
这个:
Thread t1= new progress ( jProgressBar1);
应该是:
Thread t1 = new Thread(new progress(jProgressBar1));
由于您的进度 class 实现了 Runnable 并且没有扩展 Thread。
你的错误信息也很奇怪:
constructor progress in class NewJFrame.progress cannot be applied to given types
表明问题出在 NewJFrame.progress
class 的构造函数中,一个看起来 嵌套 的 class 在 NewJFrame 中class。如果是这样,去掉嵌套的class,只处理独立的进度(请重命名"Progress")class.
但是话虽如此,当您直接从后台线程中更改 Swing 组件 JProgressBar 的状态时,您的代码存在潜在问题,而且这不是 Swing 线程安全的。根据 JProgressBar standard tutorial 使用 SwingWorker 和 link 它到 JProgressBar 的状态要好得多(请检查 link)。
例如:
import java.awt.event.KeyEvent;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import javax.swing.*;
@SuppressWarnings("serial")
public class ProgressFun extends JPanel {
private JProgressBar progressBar = new JProgressBar(0, 100);
public ProgressFun() {
progressBar.setStringPainted(true);
final JButton startProgress = new JButton("Start Progress");
startProgress.setMnemonic(KeyEvent.VK_S);
startProgress.addActionListener(l -> {
startProgress.setEnabled(false);
progressBar.setValue(0);
final MyWorker myWorker = new MyWorker();
myWorker.execute();
myWorker.addPropertyChangeListener(pcEvent -> {
if (pcEvent.getPropertyName().equals("progress")) {
int value = (int) pcEvent.getNewValue();
progressBar.setValue(value);
} else if (pcEvent.getNewValue() == SwingWorker.StateValue.DONE) {
startProgress.setEnabled(true);
try {
myWorker.get();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
});
});
add(progressBar);
add(startProgress);
}
private static void createAndShowGui() {
ProgressFun mainPanel = new ProgressFun();
JFrame frame = new JFrame("Progress Fun");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}
class MyWorker extends SwingWorker<Void, Integer> {
@Override
protected Void doInBackground() throws Exception {
int progress = 0;
setProgress(progress);
while (progress < 100) {
progress += (int)(5 * Math.random());
progress = Math.min(progress, 100);
TimeUnit.MILLISECONDS.sleep((int) (500 * Math.random()));
setProgress(progress);
}
return null;
}
}
顺便说一句,您会想要学习和使用 Java naming conventions。变量名称应全部以小写字母开头,而 class 名称应以大写字母开头。了解并遵循这一点将使我们能够更好地理解您的代码,并使您能够更好地理解其他人的代码。
我是 java 的初学者,如果我问了一个愚蠢的问题,我很抱歉,但是我如何在我的 gui class 中创建一个新线程来创建一个进度条。我有一个名为 progress 的 class 并使用我创建的构造函数在我的 gui class 中创建了一个新线程。但出于某种原因,我收到一个奇怪的错误:
"constructor progress in class NewJFrame.progress cannot be applied to given types;
required: no arguments
found: JProgressBar
reason: actual and formal argument lists differ in length
NewJframe.java
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if (jRadioButton1.isSelected()){
App m = new App();
Thread t1 = new Thread(new progress(jProgressBar1));
m.sendPingRequest2("104.160.142.3",jTextPane1,jTextPane2,jTextField1);
}
}
progress.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author User
*/
import javax.swing.JProgressBar;
public class progress implements Runnable {
private static int DELAY = 500;
JProgressBar progressBar;
public progress (JProgressBar bar) {
progressBar = bar;
}
public void run() {
int minimum = progressBar.getMinimum();
int maximum = progressBar.getMaximum();
for (int i = minimum; i < maximum; i++) {
try {
int value = progressBar.getValue();
progressBar.setValue(value + 1);
Thread.sleep(DELAY);
} catch (InterruptedException ignoredException) {
}
}
}
}
这个:
Thread t1= new progress ( jProgressBar1);
应该是:
Thread t1 = new Thread(new progress(jProgressBar1));
由于您的进度 class 实现了 Runnable 并且没有扩展 Thread。
你的错误信息也很奇怪:
constructor progress in class NewJFrame.progress cannot be applied to given types
表明问题出在 NewJFrame.progress
class 的构造函数中,一个看起来 嵌套 的 class 在 NewJFrame 中class。如果是这样,去掉嵌套的class,只处理独立的进度(请重命名"Progress")class.
但是话虽如此,当您直接从后台线程中更改 Swing 组件 JProgressBar 的状态时,您的代码存在潜在问题,而且这不是 Swing 线程安全的。根据 JProgressBar standard tutorial 使用 SwingWorker 和 link 它到 JProgressBar 的状态要好得多(请检查 link)。
例如:
import java.awt.event.KeyEvent;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import javax.swing.*;
@SuppressWarnings("serial")
public class ProgressFun extends JPanel {
private JProgressBar progressBar = new JProgressBar(0, 100);
public ProgressFun() {
progressBar.setStringPainted(true);
final JButton startProgress = new JButton("Start Progress");
startProgress.setMnemonic(KeyEvent.VK_S);
startProgress.addActionListener(l -> {
startProgress.setEnabled(false);
progressBar.setValue(0);
final MyWorker myWorker = new MyWorker();
myWorker.execute();
myWorker.addPropertyChangeListener(pcEvent -> {
if (pcEvent.getPropertyName().equals("progress")) {
int value = (int) pcEvent.getNewValue();
progressBar.setValue(value);
} else if (pcEvent.getNewValue() == SwingWorker.StateValue.DONE) {
startProgress.setEnabled(true);
try {
myWorker.get();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
});
});
add(progressBar);
add(startProgress);
}
private static void createAndShowGui() {
ProgressFun mainPanel = new ProgressFun();
JFrame frame = new JFrame("Progress Fun");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}
class MyWorker extends SwingWorker<Void, Integer> {
@Override
protected Void doInBackground() throws Exception {
int progress = 0;
setProgress(progress);
while (progress < 100) {
progress += (int)(5 * Math.random());
progress = Math.min(progress, 100);
TimeUnit.MILLISECONDS.sleep((int) (500 * Math.random()));
setProgress(progress);
}
return null;
}
}
顺便说一句,您会想要学习和使用 Java naming conventions。变量名称应全部以小写字母开头,而 class 名称应以大写字母开头。了解并遵循这一点将使我们能够更好地理解您的代码,并使您能够更好地理解其他人的代码。