https://pastein.ru/t/M-
скопируйте уникальную ссылку для отправки
Загрузка данных
public static void main(String[] args) throws IOException, ClassNotFoundException {
Interface inter = new Interface();
inter.go();
try{
ObjectInputStream input = new ObjectInputStream(new FileInputStream(inter.fileName));
int size = input.readInt();
for (int i=0;i<size;i++){
Tabs t = (Tabs) input.readObject();
t.getPanelBoxes().removeAll();
for (Boxes box:t.getListBoxes()){
t.getPanelBoxes().add(box.getBox());
}
inter.tab.add(t.getPublicPanel());
inter.listTabs.add(t);
}
input.close();}
catch (FileNotFoundException e1){System.out.println("go");}
}
public void go(){
this.frame = new JFrame("Interface");
this.panel1 = new JPanel();
this.south = new JPanel();
this.menuBar=new JMenuBar();
this.tab = new JTabbedPane();
this.deleteTab= new JButton("Удалить текущую вкладку");
menuBar.add(file =new JMenu("File"));
menuBar.add(view =new JMenu("view"));
file.add(addDevice=new JMenuItem("Добавить технику"));
file.add(exit=new JMenuItem("Удалить вкладку по имени"));
panel1.add(menuBar);
south.add(deleteTab);
frame.add(BorderLayout.NORTH,menuBar);
frame.add(BorderLayout.CENTER,tab);
frame.add(BorderLayout.SOUTH,south);
addDevice.addActionListener(new AddDevice());
deleteTab.addActionListener(new DeleteTab());
frame.addComponentListener(new ChangeSizeFrame());
frame.addWindowListener(new CloseFrame());
exit.addActionListener(new Item2());
frame.setBounds(400, 100, 1000, 700);
frame.setLocationRelativeTo(null); //вызывает окно по центру экрана
//frame.setResizable(false);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
}
private final class AddDevice implements ActionListener{
@Override
public void actionPerformed(ActionEvent arg0) {
String[] options = {"Добавить"};
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
JLabel lbl = new JLabel("Введите наименованиие техники: ");
JTextField txt = new JTextField(20);
panel.add(lbl);
panel.add(txt);
int selectedOption = JOptionPane.showOptionDialog(null, panel, "Добавление", JOptionPane.OK_OPTION, JOptionPane.QUESTION_MESSAGE, null, options , options[0]);
if(selectedOption == 0)
{
String text = txt.getText();
Tabs t = new Tabs(text);
listTabs.add(t);
tab.add(t.getPublicPanel());
tab.revalidate();
}
}
}
private final class Item2 implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
String nameTab = JOptionPane.showInputDialog("Введите наименование техники,\n которую надо удалить:");
Component[] tabs=tab.getComponents();
int count=0;
for(int i=0;i<tabs.length;i++){
String s=tabs[i].getName();
if((s.toLowerCase()).contains(nameTab.toLowerCase())){
System.out.println(tabs[i].getName());
tab.remove(i-count);
count++;
}}
}
}
private final class DeleteTab implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
int index = tab.getSelectedIndex();
Component[] arr = tab.getComponents();
for (Tabs t:listTabs){
if ((arr[index].getName()).equals(t.getPublicPanel().getName())){
listTabs.remove(t);
}
}
tab.remove(index);
}
}
private final class CloseFrame extends WindowAdapter{
public void windowClosing(WindowEvent event){
File file = new File(fileName);
try {
FileOutputStream writer =new FileOutputStream(file);
String s = "";
writer.write(s.getBytes());
writer.close();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream(file,true));
output.writeInt(listTabs.size());
for (Tabs t:listTabs){
output.writeObject(t);
}
output.close();
} catch (IOException e) {}
System.exit(0);
}
}
private final class ChangeSizeFrame extends ComponentAdapter{
public void componentResized(ComponentEvent evt){
int width = frame.getWidth();
int height = frame.getHeight();
for (Tabs tab:listTabs){
JPanel panelBoxes = tab.getPanelBoxes();
panelBoxes.setSize(width-217, height-138);
}
}
}
}