gpt4 book ai didi

java - 如何显示进度条?多线程java

转载 作者:行者123 更新时间:2023-12-02 01:54:20 25 4
gpt4 key购买 nike

我是java初学者,很抱歉,如果我问了一个愚蠢的问题,但是如何在我的gui类中创建一个新线程来创建进度条。我有一个名为 Progress 的类,并使用我创建的构造函数在我的 gui 类中创建了一个新线程。但由于某种原因,我收到了一个奇怪的错误:

"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);


}
}

进度.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));

因为您的进度类实现了 Runnable 并且不扩展 Thread。

您的错误消息也很奇怪:

constructor progress in class NewJFrame.progress cannot be applied to given types

表明问题出在 NewJFrame.progress 类的构造函数中,该类看起来嵌套在 NewJFrame 类中。如果是这样,请摆脱嵌套类,只处理独立的进度(请重命名为“Progress”)类。

<小时/>

但是话虽如此,当您直接从后台线程中更改 JProgressBar(一个 Swing 组件)的状态时,您的代码存在潜在问题,而这不是 Swing 线程安全的。更好的是使用 SwingWorker 并将其链接到 JProgressBar 的状态,如 JProgressBar standard tutorial 所示。 (请检查链接)。

例如:

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 。变量名应全部以小写字母开头,而类名应以大写字母开头。学习并遵循这一点将使我们更好地理解您的代码,并且将使您更好地理解其他人的代码。

关于java - 如何显示进度条?多线程java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52508828/

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