92 lines
3.7 KiB
Java
92 lines
3.7 KiB
Java
import javax.swing.*;
|
||
import java.awt.*;
|
||
import java.io.IOException;
|
||
|
||
public class FrameBusCollection extends JFrame {
|
||
private BusesGenericCollection<DrawingBus, DrawingObjectBus> buses;
|
||
JComponent pictureBoxCollection;
|
||
TextField textFieldNumber;
|
||
public FrameBusCollection(){
|
||
super("Набор автобусов");
|
||
setSize(new Dimension(900,500));
|
||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||
createGui();
|
||
buses = new BusesGenericCollection<>(pictureBoxCollection.getWidth(),pictureBoxCollection.getHeight());
|
||
setVisible(true);
|
||
}
|
||
private void createGui(){
|
||
pictureBoxCollection = new JComponent(){
|
||
public void paintComponent(Graphics graphics){
|
||
super.paintComponent(graphics);
|
||
Graphics2D graphics2D = (Graphics2D) graphics;
|
||
if (buses != null) buses.showBuses(graphics2D);
|
||
super.repaint();
|
||
}
|
||
};
|
||
pictureBoxCollection.setSize(new Dimension(700,450));
|
||
JButton buttonAddBus = new JButton("Добавить автобус");
|
||
buttonAddBus.setPreferredSize(new Dimension(160,30));
|
||
textFieldNumber = new TextField();
|
||
JButton buttonRemoveBus = new JButton("Удалить автобус");
|
||
buttonRemoveBus.setPreferredSize(new Dimension(160,30));
|
||
JButton buttonRefreshCollection = new JButton("Обновить коллекцию");
|
||
buttonAddBus.setPreferredSize(new Dimension(160,30));
|
||
buttonAddBus.addActionListener(e -> buttonAddBusClick());
|
||
buttonRemoveBus.addActionListener(e -> buttonRemoveBusClick());
|
||
buttonRefreshCollection.addActionListener(e -> buttonRefreshBusClick());
|
||
JPanel panelCollection = new JPanel(new GridBagLayout());
|
||
GridBagConstraints constraints = new GridBagConstraints();
|
||
constraints.insets.left = constraints.insets.right = 2;
|
||
constraints.insets.top = constraints.insets.bottom = 30;
|
||
constraints.fill = GridBagConstraints.BOTH;
|
||
constraints.gridx = 0;
|
||
constraints.gridy = 0;
|
||
panelCollection.add(buttonAddBus, constraints);
|
||
constraints.gridx = 0;
|
||
constraints.gridy = 1;
|
||
panelCollection.add(textFieldNumber, constraints);
|
||
constraints.gridx = 0;
|
||
constraints.gridy = 2;
|
||
panelCollection.add(buttonRemoveBus, constraints);
|
||
constraints.gridx = 0;
|
||
constraints.gridy = 3;
|
||
panelCollection.add(buttonRefreshCollection, constraints);
|
||
setLayout(new BorderLayout());
|
||
add(panelCollection, BorderLayout.EAST);
|
||
add(pictureBoxCollection,BorderLayout.CENTER);
|
||
}
|
||
private void buttonAddBusClick(){
|
||
FrameTrolleybus form;
|
||
try{
|
||
form = new FrameTrolleybus();
|
||
}
|
||
catch (IOException e){
|
||
throw new RuntimeException();
|
||
}
|
||
form.selectBusButton.addActionListener(e -> {
|
||
form.selectBus();
|
||
if (buses.insert(form.getSelectedBus())) {
|
||
JOptionPane.showMessageDialog(this, "Объект добавлен");
|
||
pictureBoxCollection.repaint();
|
||
} else {
|
||
JOptionPane.showMessageDialog(this, "Не удалось добавить объект");
|
||
}
|
||
form.dispose();
|
||
});
|
||
}
|
||
private void buttonRemoveBusClick() {
|
||
int pos = Integer.parseInt(textFieldNumber.getText());
|
||
if (buses.remove(pos))
|
||
{
|
||
JOptionPane.showMessageDialog(this,"Объект удалён");
|
||
}
|
||
else
|
||
{
|
||
JOptionPane.showMessageDialog(this,"Не удалось удалить объект");
|
||
}
|
||
}
|
||
private void buttonRefreshBusClick(){
|
||
pictureBoxCollection.repaint();
|
||
}
|
||
}
|