Witam
W zalączonym kodzie chcę doodać do obsługi przycisku STARt jednoczesne odliczanioe 3 progressbarow.
Ogolnie w oknie są 3 progressBary symbolizujące poziom zapelnienia skladnikiem i 1 głowny ProgressbAr który odlicza wyprodukowane ciastka wypisując w oknie ponizej.
Do zainteresowanych poprzednim moim postem i sposobem odwolania sie do wartosci pola to wydaje mi sie ze public static int w a potem PRogressBarDemo.w dziala Jezeli nie to wyprowadzcie mnei z bledu.
Wracajac do ProgressBarow mam problem z prawidłowym ich działaniem procenty powinny spadać i synchronizowaniem ich z przyciskiem start . Normalnie jakby te 3 progressBary byly w jednej klasie to odwolanie do metody iterate w klasie main wygladalo by :
frame.iterate());
A w tak złożonej klasie to nie wiem.
Prosba o wskazówki
package GUI;
import java.awt.*;
import java.awt.event.*;
import javax.print.attribute.standard.JobName;
import javax.swing.*;
public class ProgressbarDemo extends JPanel
implements ActionListener {
public final static int ONE_SECOND = 1000;
private JProgressBar progressBar;
private Timer timer;
private JButton startButton;
private LongTask task;
private JTextArea taskOutput;
private String newline = "\n";
public static int c,tl,m,z;
private NumericTextField flourInt,fatInt;
JProgressBar maka,tluszcz,cukier;
JTextArea out;
JButton find;
Thread runner;
int num = 0;
JLabel cukierText,tluszczText,makaText;
public ProgressbarDemo() {
super(new BorderLayout());
task = new LongTask();
int k;
//Create the demo's UI.
startButton = new JButton("Start");
startButton.setActionCommand("start");
startButton.addActionListener(this);
JTextField flourText= new JTextField("85",5);
JLabel flourLabel = new JLabel("maka");
JTextField sugarText= new JTextField("80",5);
JLabel sugarLabel = new JLabel("cukier");
JTextField fatText= new JTextField("60",5);
JLabel fatLabel = new JLabel("tluszcz");
tl = Integer.parseInt(fatText.getText());
m = Integer.parseInt(flourText.getText());
c = Integer.parseInt(sugarText.getText());
z = Math.min((Math.min(c,tl)),m);
// Temp pieca
JTextField piecText= new JTextField("120",5);
JLabel piecLabel = new JLabel("temperatura pieca");
progressBar = new JProgressBar(0, task.getLengthOfTask());
progressBar.setValue(0);
progressBar.setStringPainted(true);
taskOutput = new JTextArea(4, 65);
taskOutput.setMargin(new Insets(5,5,5,5));
taskOutput.setEditable(false);
taskOutput.setCursor(null); //inherit the panel's cursor
//see bug 4851758
JPanel panel = new JPanel();
panel.add(startButton);
panel.add(progressBar);
JPanel panel2 = new JPanel();
panel2.add(flourLabel);
panel2.add(flourText);
panel2.add(sugarLabel);
panel2.add(sugarText);
panel2.add(fatLabel);
panel2.add(fatText);
panel2.add(piecLabel);
panel2.add(piecText);
add(panel2, BorderLayout.WEST);
JPanel pane = new JPanel();
pane.setLayout(new GridLayout(3, 1));
cukierText = new JLabel(" ilosc cukru");
UIManager.put("ProgressBar.foreground", new Color(118, 32, 128));
cukier = new JProgressBar(0, 100);
cukier.setValue(87);
cukier.setStringPainted(true);
pane.add(cukier);
pane.add(cukierText);
setContentPane(pane);
makaText = new JLabel(" ilosc maki");
UIManager.put("ProgressBar.foreground", new Color(8, 32, 128));
maka = new JProgressBar(0, 100);
maka.setValue(50);
maka.setStringPainted(true);
pane.add(maka);
pane.add(makaText);
setContentPane(pane);
tluszczText = new JLabel(" ilosc tluszczu");
UIManager.put("ProgressBar.foreground", new Color(8, 132, 128));
tluszcz = new JProgressBar(0, 100);
tluszcz.setValue(41);
tluszcz.setStringPainted(true);
pane.add(tluszcz);
pane.add(tluszczText);
add(panel, BorderLayout.PAGE_START);
add(new JScrollPane(taskOutput), BorderLayout.EAST);
setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
add(pane,BorderLayout.SOUTH);
//Create a timer.
timer = new Timer(ONE_SECOND, new ActionListener() {
public void actionPerformed(ActionEvent evt) {
progressBar.setValue(task.getCurrent());
String s = task.getMessage();
if (s != null) {
taskOutput.append(s + newline);
taskOutput.setCaretPosition(
taskOutput.getDocument().getLength());
}
if (task.isDone()) {
Toolkit.getDefaultToolkit().beep();
timer.stop();
startButton.setEnabled(true);
setCursor(null); //turn off the wait cursor
progressBar.setValue(progressBar.getMinimum());
}
}
});
}
private void setContentPane(JPanel pane) {
}
public void actionPerformed(ActionEvent evt) {
startButton.setEnabled(false);
setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
task.go();
timer.start();
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
public void iterate() {
while (m > 0) {
maka.setValue(m);
try {
Thread.sleep(100);
} catch (InterruptedException e) { }
m -= 5;
}
}
public void iterate2() {
while (c > 0) {
cukier.setValue(c);
try {
Thread.sleep(100);
} catch (InterruptedException e) { }
c -= 5;
}
}
public void iterate3() {
while (tl > 0) {
tluszcz.setValue(tl);
try {
Thread.sleep(100);
} catch (InterruptedException e) { }
tl -= 5;
}
}
}
class LongTask {
private int lengthOfTask;
private int current = 0;
private boolean done = false;
private boolean canceled = false;
private String statMessage;
public LongTask() {
lengthOfTask =ProgressbarDemo.z;
}
/**
* Called from ProgressBarDemo to start the task.
*/
public void go() {
final SwingWorker worker = new SwingWorker() {
public Object construct() {
current = 0;
done = false;
canceled = false;
statMessage = null;
return new ActualTask();
}
};
worker.start();
}
public int getLengthOfTask() {
return ProgressbarDemo.z;
}
public int getCurrent() {
return current;
}
public void stop() {
canceled = true;
statMessage = null;
}
public boolean isDone() {
return done;
}
public String getMessage() {
return statMessage;
}
class ActualTask {
ActualTask() {
//Fake a long task,
//making a random amount of progress every second.
while (!canceled && !done) {
try {
Thread.sleep(1000); //sleep for a second
current +=5 ; //make some progress
if (current >= ProgressbarDemo.z) {
done = true;
current = ProgressbarDemo.z;
}
statMessage = "Wyprodukowano " + current +
" z " + ProgressbarDemo.z + " Ciastek .";
} catch (InterruptedException e) {
System.out.println("ActualTask interrupted");
}
}
}
}
}
abstract class SwingWorker {
private Object value; // see getValue(), setValue()
private static class ThreadVar {
private Thread thread;
ThreadVar(Thread t) { thread = t; }
synchronized Thread get() { return thread; }
synchronized void clear() { thread = null; }
}
private ThreadVar threadVar;
protected synchronized Object getValue() {
return value;
}
private synchronized void setValue(Object x) {
value = x;
}
public abstract Object construct();
public void finished() {
}
public void interrupt() {
Thread t = threadVar.get();
if (t != null) {
t.interrupt();
}
threadVar.clear();
}
public Object get() {
while (true) {
Thread t = threadVar.get();
if (t == null) {
return getValue();
}
try {
t.join();
}
catch (InterruptedException e) {
Thread.currentThread().interrupt(); // propagate
return null;
}
}
}
public SwingWorker() {
final Runnable doFinished = new Runnable() {
public void run() { finished(); }
};
Runnable doConstruct = new Runnable() {
public void run() {
try {
setValue(construct());
}
finally {
threadVar.clear();
}
SwingUtilities.invokeLater(doFinished);
}
};
Thread t = new Thread(doConstruct);
threadVar = new ThreadVar(t);
}
/**
* Start the worker thread.
*/
public void start() {
Thread t = threadVar.get();
if (t != null) {
t.start();
}
}
}