Я внедряю проблему Consumer-Producer на Java, и мне нужно добавить парня к этому. Моя проблема заключается в изменении компонентов пользовательского интерфейса от Customer
или Producer
классов.Изменение значений пользовательского интерфейса из другого класса
Я не знаю, как я могу назвать эти компоненты из другого класса, не относящегося к классу. Когда я попытался получить, например, height
компонента, все работает как шарм, но когда я пытаюсь сделать set
ничего, ничего не происходит!
Это мой Producer
код класса с некоторыми попытками:
public class Producer extends Thread {
// Variable which holds shared queue
private BlockingQueue<String> queue;
// Amount products created by producer
private int steps;
// Object with normaln distribution
private NormalDistribution distribution;
// Accessors to the frame
private PCPMainFrame frame;
private JSlider queueSlider;
private JProgressBar queueProgressBar;
// Constructor with 4 arguments
// q - is our queue shared between customer and producer
// steps - amount of products
// mean - parameter rquired for normal distribution
// standardDeviation - ditto
public Producer(BlockingQueue<String> q, int steps, double mean, double standardDeviation){
this.queue=q;
this.steps = steps;
this.distribution = new NormalDistribution(mean, standardDeviation);
this.frame = new PCPMainFrame();
this.queueSlider = frame.getQueueSlider();
this.queueProgressBar = new JProgressBar();
}
@Override
public void run() {
// Generating products and filling queue with them
for(int i = 0; i < steps; i++){
try {
long sleepTime = Math.abs((long)distribution.sample()*100);
Thread.sleep(sleepTime);
// Saving element in queue
queue.put(String.valueOf(i));
// This is a log for developer needs, feel free to uncomment
System.out.println("Produced: " + i);
queueSlider.setValue(steps);
frame.setQueueProgressBar(queueProgressBar);
} catch (InterruptedException e) {
System.out.println("Producer exception: " + e);
}
}
// Ading exit message at the end of the queue
String exit = new String("exit");
try {
queue.put(exit);
} catch (InterruptedException e) {
System.out.println("Queue exception: " + e);
}
}
}
Хммм нет ничего особенного в свинге, просто вызовите свои компоненты так же, как вы бы назвали любой объект любого нормального класса? –
Я знаю, что он должен работать таким образом, но, как вы можете видеть в этом примере кода, когда я вызываю 'queueSlider.setValue (шаги);' ничего не происходит ... – cojoj
Значение шагов никогда не изменяется – Philippe