lab4 some minor fixes

This commit is contained in:
Zakharov_Rostislav 2023-11-24 10:03:45 +04:00
parent c526d2cfc1
commit d210db6966
2 changed files with 5 additions and 25 deletions

View File

@ -5,15 +5,15 @@ import javax.swing.border.StrokeBorder;
import java.awt.*; import java.awt.*;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Objects; import java.util.Objects;
import drawing_objects.DrawingShip; import drawing_objects.DrawingShip;
import generics.ShipsGenericStorage; import generics.ShipsGenericStorage;
import generics.TrashCollection;
public class FrameShipsCollection extends JFrame { public class FrameShipsCollection extends JFrame {
private final ShipsGenericStorage storage; private final ShipsGenericStorage storage;
TrashCollection<DrawingShip> trashCollection = new TrashCollection<>(); LinkedList<DrawingShip> trashCollection = new LinkedList<>();
private JComponent pictureBoxCollection; private JComponent pictureBoxCollection;
private TextField textFieldNumber; private TextField textFieldNumber;
private TextField textFieldStorageName; private TextField textFieldStorageName;
@ -45,6 +45,7 @@ public class FrameShipsCollection extends JFrame {
pictureBoxCollection.setPreferredSize(new Dimension(700, 450)); pictureBoxCollection.setPreferredSize(new Dimension(700, 450));
JButton buttonAddShip = new JButton("Добавить корабль"); JButton buttonAddShip = new JButton("Добавить корабль");
textFieldNumber = new TextField(); textFieldNumber = new TextField();
textFieldNumber.setText("0");
JButton buttonRemoveShip = new JButton("Удалить корабль"); JButton buttonRemoveShip = new JButton("Удалить корабль");
JButton buttonRefreshCollection = new JButton("Обновить коллекцию"); JButton buttonRefreshCollection = new JButton("Обновить коллекцию");
JButton buttonAddSet = new JButton("Добавить набор"); JButton buttonAddSet = new JButton("Добавить набор");
@ -167,7 +168,7 @@ public class FrameShipsCollection extends JFrame {
}); });
} }
private void buttonRemoveShipClick(){ private void buttonRemoveShipClick(){
if (listStorages.getSelectedIndex() == -1) if (listStorages.getSelectedIndex() == -1 || Objects.equals(textFieldNumber.getText(), "") || textFieldNumber.getText() == null)
return; return;
var obj = storage.getSet(listStorages.getSelectedValue()); var obj = storage.getSet(listStorages.getSelectedValue());
if (obj == null) if (obj == null)
@ -187,7 +188,7 @@ public class FrameShipsCollection extends JFrame {
pictureBoxCollection.repaint(); pictureBoxCollection.repaint();
} }
private void buttonTrashClick(){ private void buttonTrashClick(){
if(trashCollection.getSize() == 0) if(trashCollection.peek() == null)
return; return;
FrameBattleship form; FrameBattleship form;
try { try {

View File

@ -1,21 +0,0 @@
package generics;
import java.util.LinkedList;
public class TrashCollection<T> {
LinkedList<T> list;
public TrashCollection(){
list = new LinkedList<>();
}
public int getSize(){
return list.size();
}
public void add(T ship){
list.add(ship);
}
public T pop(){
if(list.size() == 0)
return null;
return list.pop();
}
}