gpt4 book ai didi

Java:将值连续读取到多个从未完成的 jprogressbars

转载 作者:行者123 更新时间:2023-11-29 07:06:38 24 4
gpt4 key购买 nike

我在 Java GUI 应用程序中有 3 个“JProgressBars”,我需要以恒定的时间间隔不断更新值。

这是我的应用程序,突出显示的是我要将值读入其中的进度表。

enter image description here要读取每个进度表的值,我的对象中有 3 个方法(这里是机器人类)示例:

robot.readBattery();
robot.readSonic();
robot.readLight();

所有方法都会返回 0 到 100 之间的值。

我已经看到可以使用 swingworker 完成单个进度条。那么这是否意味着我的程序中需要 3 个 swing worker 类来服务所有三个进度条?

即使我这样做了,属性更改监听器如何区分进度条?

如果有人能指导我解决这个问题,我将不胜感激。

我在机器人中有一个连接方法,如果程序连接到机器人,它会返回 true。

即:boolean connected = robot.connect();

所以在 swingworker 中,代码应该是这样的,

while (true) {
if (connected){
setProgress(robot.readBattery());
}

sleep(1000);
}

我可能是错的。请指导我。

要求的 SSCCE:

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingWorker;

public class Tester extends JFrame implements PropertyChangeListener,
ActionListener {

/**
*
*/
private static final long serialVersionUID = 1L;

private JProgressBar batteryMeter, lightMeter, sonicMeter;

private Robot robot = new Robot();
private BatteryTask bt;

private JButton start = new JButton("Start");

public Tester() {

JPanel statusPanel = new JPanel();
statusPanel.setLayout(new GridBagLayout());

GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(2, 4, 2, 4);
c.fill = GridBagConstraints.HORIZONTAL;

c.gridx = 0;
c.gridy = 0;
statusPanel.add(new JLabel("Battery Level:"), c);

batteryMeter = new JProgressBar(0, 100);
batteryMeter.setStringPainted(false);
batteryMeter.setPreferredSize(new Dimension(215, 15));
batteryMeter.setValue(0);
c.gridx = 1;
c.gridy = 0;
statusPanel.add(batteryMeter, c);

c.gridx = 0;
c.gridy = 1;
statusPanel.add(new JLabel("Light Sensor:"), c);

lightMeter = new JProgressBar(0, 100);
lightMeter.setStringPainted(false);
lightMeter.setPreferredSize(new Dimension(215, 15));
lightMeter.setValue(0);
c.gridx = 1;
c.gridy = 1;
statusPanel.add(lightMeter, c);
c.gridx = 0;
c.gridy = 2;
statusPanel.add(new JLabel("Ultrasonic Sensor:"), c);

sonicMeter = new JProgressBar(0, 100);
sonicMeter.setStringPainted(false);
sonicMeter.setPreferredSize(new Dimension(215, 15));
sonicMeter.setValue(0);
c.gridx = 1;
c.gridy = 2;
statusPanel.add(sonicMeter, c);
c.gridx = 0;
c.gridy = 3;

start.addActionListener(this);
statusPanel.add(start, c);
this.getContentPane().add(statusPanel);

this.setSize(500, 300);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.setVisible(true);

}

public static void main(String[] args) {
// TODO Auto-generated method stub
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Tester();
}
});

}

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub

if (e.getSource() == start) {

bt = new BatteryTask();
bt.addPropertyChangeListener(this);
bt.execute();
}

}

@Override
public void propertyChange(PropertyChangeEvent evt) {
// TODO Auto-generated method stub
if ("progress" == evt.getPropertyName()) {
int progress = (Integer) evt.getNewValue();
batteryMeter.setValue(progress);
}

}

class BatteryTask extends SwingWorker<Void, Void> {
/*
* Main task. Executed in background thread.
*/
@Override
public Void doInBackground() {

// Initialize progress property.
setProgress(0);
while (true) {
// Sleep for up to one second.
try {
Thread.sleep(1000);
} catch (InterruptedException ignore) {
}
// Make random progress.

setProgress(robot.readBattery());
}
}

}

}

class Robot {

private Random r = new Random();

int readBattery() {

int i = r.nextInt(100 - 1) + 1;
System.out.println(i);
return i;
}

int readSonic() {

return r.nextInt(100 - 1) + 1;
}

int readLight() {

return r.nextInt(100 - 1) + 1;
}
}

最佳答案

名为“progress”的 PropertyChangeEvent 仅传达一个值,与 worker 的整体进度无关。相反,让单个 RobotTask 后台线程 publish() 一个 StatusRecord 保存三个新值。 worker 的 process() 方法在事件派发线程上执行,可以在新记录到达时更新三个进度条。有相关例子herehere .

class RobotTask extends SwingWorker<StatusRecord, StatusRecord> {…}

关于Java:将值连续读取到多个从未完成的 jprogressbars,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18958765/

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