PIbd-21_KozyrevSS_GasolineT.../CollectionFrame.java

137 lines
4.6 KiB
Java
Raw Normal View History

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
class CollectionFrame extends JComponent {
2023-11-15 14:22:35 +04:00
private final CarsGenericCollection<DrawTanker, DrawingObjectTanker> _tanks;
protected final int Width;
protected final int Height;
2023-11-15 14:22:35 +04:00
public CollectionFrame(int width, int height)
{
Width = width;
Height = height;
2023-11-15 14:22:35 +04:00
_tanks = new CarsGenericCollection<>(Width, Height);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
2023-11-15 14:22:35 +04:00
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){
2023-11-15 14:22:35 +04:00
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)
{
2023-11-15 14:22:35 +04:00
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);
2023-11-15 14:22:35 +04:00
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();
}
});
2023-11-15 14:22:35 +04:00
JTextField indexTankerField = new JTextField();
indexTankerField.setBounds(Width- 200, Height-500, 200, 30);
indexTankerField.setLayout(null);
add(indexTankerField);
2023-11-15 14:22:35 +04:00
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());
}
});
2023-11-15 14:22:35 +04:00
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();
}
});
2023-11-15 14:22:35 +04:00
Collection = new CollectionFrame(Width-200, Height);
Collection.setLayout(null);
Collection.setBounds(0, 0, Width-200, Height);
add(Collection);
}
}