我昨天发帖,当时我完全不知道进度条是如何工作的。在得到一些帮助并继续努力之后,我已经完成了很多工作,但是我仍然坚持用进度来更新条形图。
我试图使用我的setProgress方法并在每次轮换时发送更新,但这似乎不起作用
许多人告诉我使用摇摆计时器来发送进度,但我似乎并不知道如何实现它。如果有人能告诉我他们将如何做到这一点,我会从中学到很多东西。
编辑:要看的重要地方很可能是我在Robot中的run方法以及整个progressbar类。
目前代码的问题是没有进展更新。
机器人类
public class Robot implements Runnable{
public static final int on = 0x0001;
public static final int clockwise = 0x0002;
public static final int counter = 0x0004;
int opcode;
int moveCount = 0;
int rotation, increment, progress = 0;
boolean CW;
ProgressBar prgBar;
CyclicBarrier cyclicBarrier;
Controller c;
Motor m;
public Robot(CyclicBarrier cyclicBarrier, Motor m)
{
opcode = 0;
this.cyclicBarrier = cyclicBarrier;
this.m = m;
}
public Boolean Set(int value)
{
boolean bvalue = false;
switch (value)
{
case on :
bvalue = true;
break;
case clockwise :
if ( (opcode & counter) == 0 )
bvalue = true;
break;
case counter :
if ( (opcode & clockwise) == 0 )
bvalue = true;
break;
}
if (bvalue == true)
opcode += value;
return bvalue;
}
private String Validate()
{
String sresult = "Executing: ";
boolean bvalue = false;
if ((opcode & on) > 0)
{
bvalue = true;
sresult = "On";
if ((opcode & (clockwise+counter)) == (clockwise+counter))
bvalue = false;
if (bvalue == true)
{
if ((opcode & clockwise) > 0)
sresult +="+Clockwise";
if ((opcode & counter) > 0)
sresult += "+Counter";
}
if (bvalue == false)
sresult = "Invalid opcode";
}
return sresult;
}
public void robotExecute()
{
String svalue = Validate();
System.out.println(svalue);
opcode = 0;
}
public void setOn(ProgressBar prgBar){
this.prgBar = prgBar;
Set(1);
}
public void run(){
System.out.println("Running: ");
m.Engage(this);
prgBar.setProgress(this, progress);
try {
System.out.println("Sleeping: ");
Thread.sleep(3000);
cyclicBarrier.await();
} catch (Exception e){
e.printStackTrace();
}
System.out.println("Engaging: ");
}
public void Rotate(){
if ((opcode & clockwise) > 0){
rotation++;
if(rotation == 360){
rotation = 0;
moveCount++;
}
}
if ((opcode & counter) > 0){
rotation--;
if(rotation == -360){
rotation = 0;
moveCount --;
}
}
prgBar.setProgress(this, progress);
}
}
进度
public class ProgressBar implements ActionListener {
final int MAX = 100;
final JFrame frame = new JFrame("JProgress ");
final JProgressBar pbOne = new JProgressBar();
final JProgressBar pbTwo = new JProgressBar();
final JProgressBar pbThree = new JProgressBar();
private Map<Robot, JProgressBar> robotBars = new HashMap<>();
public ProgressBar(){
pbOne.setMinimum(0);
pbOne.setMaximum(MAX);
pbOne.setStringPainted(true);
pbTwo.setMinimum(0);
pbTwo.setMaximum(MAX);
pbTwo.setStringPainted(true);
pbThree.setMinimum(0);
pbThree.setMaximum(MAX);
pbThree.setStringPainted(true);
frame.setLayout(new FlowLayout());
frame.getContentPane().add(pbOne);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
frame.setVisible(true);
}
public ProgressBar(Robot[] robots){
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
frame.setVisible(true);
for(Robot r: robots) {
JProgressBar bar = new JProgressBar();
robotBars.put(r, bar);
bar.setMinimum(0);
bar.setMaximum(MAX);
frame.setLayout(new FlowLayout());
frame.add(bar);
}
/*Timer timer = new Timer(1000, new ActionListener(){
public void actionPerformed(ActionEvent evt){
bar.setValue(progress);
}
});*/
}
public void setProgress(Robot r, int progress){
final JProgressBar bar = robotBars.get(r);
if(bar == null) { return;}
try{
SwingUtilities.invokeLater(new Runnable() {
public void run() {
bar.setValue(progress);
}
});
java.lang.Thread.sleep(100);
} catch (InterruptedException e) {
JOptionPane.showMessageDialog(frame, e.getMessage());
}
}
}
我的汽车课 公共汽车{ boolean CW;
public void setMotor(Boolean CW, int increment, Robot r){
if(CW == true){
r.Set(1<<1);
}
else if (CW == false)
((Robot)r).Set(1<<2);
r.increment = increment;
}
public void Engage(Robot r){
System.out.println("Rotating: ");
for(int i = 1; i <= r.increment; i++){
r.Rotate();
}
r.robotExecute();
System.out.println("Finished");
}
}
并持续我的主要课程
public class Controller {
public static void main(String[] args){
CyclicBarrier cyclicBarrier = new CyclicBarrier(3);
Motor m = new Motor();
Robot xRob = new Robot(cyclicBarrier, m);
Robot yRob = new Robot(cyclicBarrier, m);
Robot zRob = new Robot(cyclicBarrier, m);
Robot[] robotList = new Robot[]{xRob, yRob, zRob};
Thread xRobThread = new Thread(xRob);
Thread yRobThread = new Thread(yRob);
Thread zRobThread = new Thread(zRob);
ProgressBar prgBar = new ProgressBar(robotList);
xRob.setOn(prgBar);
yRob.setOn(prgBar);
zRob.setOn(prgBar);
boolean clockwise = true, counterClockwise = false;
m.setMotor(clockwise, 14400000, xRob);
m.setMotor(clockwise, 0, yRob);
m.setMotor(counterClockwise, 36000000, zRob);
xRobThread.start();
yRobThread.start();
zRobThread.start();
try {
xRobThread.join();
yRobThread.join();
zRobThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.printf("x = %d y = %d z = %d\n\n", xRob.moveCount, yRob.moveCount, zRob.moveCount);
/*m.setMotor(counterClockwise, 360000000, xRob);
m.setMotor(clockwise, 1440000000, yRob);
m.setMotor(clockwise, 1440000000, zRob);*/
}
}
答案 0 :(得分:1)
经过调查,你有几个问题。
JProgressBar
没有出现在框架中。基本上在ProgressBar(Robot[])
你正在做
frame.setSize(300, 200);
frame.setVisible(true);
在添加进度条之前,这是一个问题。在添加子项之前调用setSize(...)...setVisible(true)
会导致不可预测的行为。如果您只是在添加进度条后移动这两行,它们应该出现在框架中;但是,你所做的并不是一种好的风格。
我会推荐像
这样的东西Container pane = frame.getContentPane();
pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
for(Robot r: robots) {
JProgressBar bar = new JProgressBar();
robotBars.put(r, bar);
bar.setMinimum(0);
bar.setMaximum(MAX);
pane.add(bar);
}
frame.pack();
frame.setVisible(true);
使用a proper layout并调用pack
自动调整框架大小。
您也不是creating your GUI on the EDT,基本上您的main
逻辑需要包含在invokeLater
的调用中。您拨打join
时会很复杂。也许快速修复就像:
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
// ... create GUI ... create Threads ...
new Thread(new Runnable() {
@Override
public void run() {
// ... start Threads ... join Threads ...
}
}).start();
}
}
}
progress
,因此progress
始终为0。我不确定您实际上是如何更新进度的。出于测试目的,我将其更改为:
// vv
prgBar.setProgress(this, progress++);
一旦我做了这些事情,我会看到框架中的JProgressBar
,我看到它们正在更新:
其他事项:
javax.swing.Timer
将取代Thread
。也许你的Motor
会在timer事件中创建一个Timer
轮换。这不是调用sleep
来管理您的轮换增量。Set, Rotate, Engage, etc
应为set, rotate, engage, etc
。