Добавлен MenuBar

This commit is contained in:
Никита Потапов 2023-12-17 19:08:22 +04:00
parent 7e33476f43
commit 6f24631855
2 changed files with 45 additions and 0 deletions

View File

@ -2,8 +2,11 @@ package ProjectStormtrooper;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Objects;
import java.util.Stack;
@ -47,6 +50,47 @@ public class FormPlaneCollection {
buttonShowRemovedPlanes.addActionListener(this::buttonShowRemovedPlanesClicked);
}
public JMenuBar getMenuBar() {
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("Файл");
JMenuItem openItem = new JMenuItem("Загрузить");
openItem.addActionListener(
e -> {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileFilter(new FileNameExtensionFilter("Текстовые файлы (*.txt)", "txt"));
fileChooser.setDialogTitle("Выберите файл для загрузки данных");
if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
if (_storage.LoadData(selectedFile.getAbsolutePath())) {
JOptionPane.showMessageDialog(null, "Загрузка прошла успешно", "Результат", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(null, "Не загрузилось", "Результат", JOptionPane.ERROR_MESSAGE);
}
}
ReloadObjects();
}
);
JMenuItem saveItem = new JMenuItem("Сохранить");
saveItem.addActionListener(
e -> {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setDialogTitle("Выберите файл для сохранения данных");
fileChooser.setFileFilter(new FileNameExtensionFilter("Текстовые файлы (*.txt)", "txt"));
if (fileChooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
if (_storage.SaveData(selectedFile.getAbsolutePath()))
JOptionPane.showMessageDialog(null, "Сохранение прошло успешно", "Результат", JOptionPane.INFORMATION_MESSAGE);
else
JOptionPane.showMessageDialog(null, "Не сохранилось", "Результат", JOptionPane.ERROR_MESSAGE);
}
}
);
fileMenu.add(openItem);
fileMenu.add(saveItem);
menuBar.add(fileMenu);
return menuBar;
}
private void ReloadObjects() {
int index = listBoxStorages.getSelectedIndex();
listBoxStorages.setListData(_storage.Keys().toArray());

View File

@ -10,6 +10,7 @@ public class FramePlaneCollection extends JFrame {
setDefaultCloseOperation(EXIT_ON_CLOSE);
_formPlaneCollection = new FormPlaneCollection();
setContentPane(_formPlaneCollection.getPanelWrapper());
this.setJMenuBar(_formPlaneCollection.getMenuBar());
setDefaultLookAndFeelDecorated(false);
setLocation(300, 100);
pack();