У меня проблема. Я создаю JFrame, где я даю несколько чисел, и после нажатия кнопки «Принять» должен быть открыт новый JFrame. Затем, когда я нажимаю кнопку «Пуск», начинается моделирование, но я хочу обновить sh это окно. Я хочу обновить sh JLabels со значениями. Это не. Я пытался сделать это по потокам, но печать новых значений не работает. Думаю, проблема в методе Thread3 и printValues. Что случилось? Помогите!
Class Swing:
public class Swingg extends JFrame {
private String[] operations = {"starting money value", "upper Limit of warehouse", "lower Limit of warehouse", "starting amount of products of warehouse", "upper Limit of wholesale", "amount of items to delivery"};
private JTextField[] fields = new JTextField[operations.length];
private Store store = new Store();
public void inputValues() {
Box valuesLabels = new Box(BoxLayout.Y_AXIS);
Box valuesFields = new Box(BoxLayout.Y_AXIS);
for (String labels : operations) {
valuesLabels.add(new Label(labels));
}
for (int i = 0; i < operations.length; i++) {
fields[i] = new JTextField(1);
valuesFields.add(fields[i]);
fields[i].setDocument(new JTextFieldLimit(6));
}
JFrame inputWindow = new JFrame("Starting values");
inputWindow.setSize(500, 400);
JButton accept = new JButton("Accept");
JPanel startingValuesPanel = new JPanel();
startingValuesPanel.add(accept);
JPanel inputValuesPanel = new JPanel();
inputValuesPanel.setBorder(BorderFactory.createEmptyBorder(30, 30, 30, 30));
inputValuesPanel.add(BorderLayout.WEST, valuesLabels);
inputValuesPanel.add(BorderLayout.EAST, valuesFields);
inputWindow.getContentPane().add(BorderLayout.CENTER, inputValuesPanel);
inputWindow.getContentPane().add(BorderLayout.SOUTH, startingValuesPanel);
inputWindow.setLocationRelativeTo(null);
GroupLayout layout = new GroupLayout(inputValuesPanel);
inputValuesPanel.setLayout(layout);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();
hGroup.addGroup(layout.createParallelGroup().addComponent(valuesLabels));
hGroup.addGroup(layout.createParallelGroup().addComponent(valuesFields));
layout.setHorizontalGroup(hGroup);
GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();
vGroup.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(valuesLabels).addComponent(valuesFields));
layout.setVerticalGroup(vGroup);
inputWindow.setVisible(true);
accept.addActionListener(e -> {
double number1 = Double.parseDouble(fields[0].getText());
store.setAccountBalance(number1);
int number2 = Integer.parseInt(fields[1].getText());
store.getWarehouse().setUpperProductLimit(number2);
int number3 = Integer.parseInt(fields[2].getText());
store.getWarehouse().setLowerProductLimit(number3);
int number4 = Integer.parseInt(fields[3].getText());
store.getWarehouse().setCurrentQuantity(number4);
int number5 = Integer.parseInt(fields[4].getText());
store.getWholesale().setUpperProductLimit(number5);
store.getWholesale().setCurrentProductQuantity(store.getWholesale().getUpperProductLimit());
int number6 = Integer.parseInt(fields[5].getText());
store.getWholesale().setFillProductQuantity(number6);
startSimulation();
inputWindow.dispose();
});
}
public void startSimulation() {
JFrame simulationWindow = new JFrame("Simulation");
simulationWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
simulationWindow.setSize(500, 300);
simulationWindow.setLocationRelativeTo(null);
simulationWindow.setVisible(true);
Box warehouseLabels = new Box(BoxLayout.Y_AXIS);
Box wholesaleLabels = new Box(BoxLayout.Y_AXIS);
JPanel warehousePanel = new JPanel();
JPanel wholesalePanel = new JPanel();
JPanel moneyPanel = new JPanel();
JPanel buttonPanel = new JPanel();
JButton start = new JButton("Start");
JButton exit = new JButton("Exit");
store.fillWithProducts(store.getWholesale().products, 0.0, store.getWholesale().getCurrentProductQuantity());
store.fillWithProducts(store.getWarehouse().storedProducts, 0.2, store.getWarehouse().getCurrentQuantity());
moneyPanel.add(new Label("money: " + store.getAccountBalance()), BorderLayout.CENTER);
for (Product product : store.getWarehouse().storedProducts) {
warehouseLabels.add(new Label(product.getName() + ": " + product.getQuantity()));
}
for (Product product : store.getWholesale().products) {
wholesaleLabels.add(new Label(product.getName() + ": " + product.getQuantity()));
}
buttonPanel.add(start);
buttonPanel.add(exit);
warehousePanel.add(BorderLayout.CENTER, warehouseLabels);
wholesalePanel.add(BorderLayout.CENTER, wholesaleLabels);
simulationWindow.add(moneyPanel, BorderLayout.NORTH);
simulationWindow.add(wholesalePanel, BorderLayout.EAST);
simulationWindow.add(warehousePanel, BorderLayout.WEST);
simulationWindow.add(buttonPanel, BorderLayout.SOUTH);
exit.addActionListener(e ->
System.exit(0)
);
start.addActionListener(e -> {
Client sellingTask = new Client();
//watek uzupelniania przedmiotow w magazynie
Thread t2 = new Thread(() -> {
while (true) {
try {
Thread.sleep(10000); // 10 seconds
sellingTask.buyProductsFromWarehouse(store);
//printValues(moneyPanel, simulationWindow);
} catch (Exception e12) {
e12.printStackTrace();
}
}
});
Thread t3 = new Thread(() -> {
while (true) {
try {
store.fillMissingProducts();
printValues(moneyPanel);
Thread.sleep(25000); // 25 seconds
} catch (Exception e1) {
e1.printStackTrace();
}
}
});
t2.start();
t3.start();
});
}
public void printValues(JPanel panel) {
for (Component c : panel.getComponents()) {
if (c instanceof JLabel) {
((JLabel) c).setText("money" + store.getAccountBalance());
}
}
}}