package ProjectElectricLocomotive; import javax.swing.*; import java.awt.*; public class FormLocomotiveCollections { FrameDopClassParameters frameDopClassParameters; private JPanel MainPanel; private JPanel pictureBoxCollections; private JPanel Instruments; private JButton ButtonAddLocomotive; private JTextField textFieldNumber; private JButton ButtonRefreshCollection; private JButton ButtonRemoveLocomotive; private JButton ButtonCreateRandomLoco; public DrawingLocomotive loco; LocomotiveGenericCollection _locomotives; public JPanel getPictureBoxCollections() { return MainPanel; } public FormLocomotiveCollections() { _locomotives = new LocomotiveGenericCollection<>(400, 300); ButtonAddLocomotive.addActionListener(e -> { FrameElectricLocomotive frameElectricLocomotive = new FrameElectricLocomotive(); frameElectricLocomotive.setVisible(true); frameElectricLocomotive._formLocomotiveCollection.ButtonSelectLocomotive.addActionListener(e2 -> { loco = frameElectricLocomotive._formLocomotiveCollection._drawingLocomotive; frameElectricLocomotive.dispose(); if (loco != null) { //проверяем, удалось ли нам загрузить объект if (_locomotives.AddOverload(loco) != -1) { JOptionPane.showMessageDialog(getPictureBoxCollections(), "Объект добавлен"); Refresh(); } else { JOptionPane.showMessageDialog(getPictureBoxCollections(), "Не удалось добавить объект"); } } }); }); ButtonCreateRandomLoco.addActionListener(e->{ if(frameDopClassParameters!=null) frameDopClassParameters.dispose(); frameDopClassParameters = new FrameDopClassParameters(); frameDopClassParameters.setVisible(true); }); ButtonRemoveLocomotive.addActionListener(e -> { try { int pos = Integer.parseInt(textFieldNumber.getText()); if (_locomotives.SubOverload(pos) != null) { Refresh(); JOptionPane.showMessageDialog(this.getPictureBoxCollections(), "Объект удален", "Успех", JOptionPane.INFORMATION_MESSAGE); } else { JOptionPane.showMessageDialog(this.getPictureBoxCollections(), "Не удалось удалить объект", "Ошибка", JOptionPane.ERROR_MESSAGE); } } catch (Exception ex) { JOptionPane.showMessageDialog(this.getPictureBoxCollections(), "Неверное значение", "Ошибка", JOptionPane.ERROR_MESSAGE); } }); ButtonRefreshCollection.addActionListener(e -> { Refresh(); }); } public void Refresh() { Graphics g = pictureBoxCollections.getGraphics(); pictureBoxCollections.paint(g); _locomotives.ShowLocomotives(g); } }