import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; class CollectionFrame extends JComponent { private final CarsGenericCollection _tanks; protected final int Width; protected final int Height; public CollectionFrame(int width, int height) { Width = width; Height = height; _tanks = new CarsGenericCollection<>(Width, Height); } @Override public void paintComponent(Graphics g) { super.paintComponent(g); g.drawImage(_tanks.ShowCars(), 0, 0, this); super.repaint(); } protected void ButtonAddTank_Click() { BaseFrame form = new BaseFrame(); form.setVisible(true); form.buttonSelect.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e){ if (_tanks.plus(form.Gasoline.GetSelectedCar()) != -1) { form.dispose(); JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE); System.out.println("Объект добавлен"); } else { form.dispose(); JOptionPane.showMessageDialog(null, "Не удалось добавить объект", "Информация", JOptionPane.INFORMATION_MESSAGE); System.out.println("Не удалось добавить объект"); } } } ); } protected void ButtonRemoveTank_Click(String text) { if (JOptionPane.showConfirmDialog(null, "Delete Tanker?", "Delete", JOptionPane.YES_NO_OPTION) == JOptionPane.OK_OPTION) { if (_tanks.minus(Integer.parseInt(text))) { JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE); super.repaint(); } else { JOptionPane.showMessageDialog(null, "Не удалось удалить объект", "Информация", JOptionPane.INFORMATION_MESSAGE); } } super.repaint(); } protected void ButtonUpdate_Click() {super.repaint();} } class GarageFrame extends JFrame { public GarageFrame() { initUI(); } protected static final int Width = 1000; protected static final int Height = 600; CollectionFrame Collection; private void initUI() { setSize(Width, Height); setTitle("TankGasoline"); setLocationRelativeTo(null); setLayout(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setResizable(false); JButton addTankerButton = new JButton("Add tanker"); addTankerButton.setLayout(null); addTankerButton.setBounds(Width-200, Height-550, 200, 30); add(addTankerButton); addTankerButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { Collection.ButtonAddTank_Click(); } }); JTextField indexTankerField = new JTextField(); indexTankerField.setBounds(Width- 200, Height-500, 200, 30); indexTankerField.setLayout(null); add(indexTankerField); JButton deleteTankerButton = new JButton("Delete tanker"); deleteTankerButton.setLayout(null); deleteTankerButton.setBounds(Width-200, Height-450, 200, 30); add(deleteTankerButton); deleteTankerButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { Collection.ButtonRemoveTank_Click(indexTankerField.getText()); } }); JButton updateCollectionButton = new JButton("Update collection"); updateCollectionButton.setLayout(null); updateCollectionButton.setBounds(Width-200, Height-400, 200, 30); add(updateCollectionButton); updateCollectionButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { Collection.ButtonUpdate_Click(); } }); Collection = new CollectionFrame(Width-200, Height); Collection.setLayout(null); Collection.setBounds(0, 0, Width-200, Height); add(Collection); } }