我JProgressBars
在 Java GUI 应用程序中有 3 个“”,我需要以恒定的时间间隔不断更新值。
这是我的应用程序,突出显示的是我想要读取值的进度表。
要读取每个进度表的值,我的对象(这里是机器人类)示例中有 3 个方法:
robot.readBattery();
robot.readSonic();
robot.readLight();
所有方法都将返回 0 到 100 之间的值。
我已经看到了一个进度条,这可以使用 swingworker 来完成。那么这是否意味着我的程序中需要 3 个摇摆工人课程来服务所有三个进度条?
即使我这样做了,属性更改侦听器如何区分进度条?
如果有人可以指导我完成此操作,我将不胜感激。
我在机器人中有一个连接方法,如果程序连接到机器人,它会返回 true。
IE: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;
}
}