写入文件,然后使用窗口列表保存



在这个幻想足球计划上需要一些帮助,努力弄清楚如何将我的 writetofile方法实现到添加按钮中,然后在退出程序时将其保存在关闭中。

我被一个朋友告诉我使用WindowListener,但不知道如何使用。

import java.awt.GridLayout; 
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.SwingUtilities;
import javax.swing.ListSelectionModel; 
import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.*;
import java.util.*;
public class JListFBExample extends JFrame 
{
   private JList<Footballer> rhsList;
   private DefaultListModel<Footballer> rhsListModel; 
   private JButton btnIn; 
   private JButton btnOut;
   private JButton btnAdd;
   private JButton btnDelete;
   private JButton acceptPlayer;
   private JTextField playerFName;
   private JTextField playerLName;
   private JComboBox comboBox;
   private JList<Footballer> fbList;
   private ArrayList<Footballer> fbArrayList =  new ArrayList<Footballer();

 private DefaultListModel<Footballer> fbListModel; 

   public JListFBExample() throws IOException, ClassNotFoundException 
   {
      readRecordsFromFile();
      //create the model and add elements
      fbListModel = new DefaultListModel<>();

      for(Footballer f : fbArrayList)
         fbListModel.addElement(f); 

      fbList = new JList<>(fbListModel);
      //create the model and add elements
      rhsListModel = new DefaultListModel<>();
      rhsList = new JList<>(rhsListModel);
      setLayout(new GridLayout(1, 3));
      add(new JScrollPane(fbList));   //add the list to scrollpane and to the frame
      add(createButtonPanel());
      add(new JScrollPane(rhsList));    
   }  
   public JPanel createButtonPanel()
   {
      JPanel panel = new JPanel();

      ActionListener listener = new ButtonListener();
      ActionListener delListener = new DeleteListener();
      btnIn = new JButton(">>"); 
      btnOut = new JButton("<<");
      btnAdd = new JButton("Add");
      btnDelete = new JButton("Delete");
      btnIn.addActionListener(listener); 
      btnOut.addActionListener(listener);
      btnDelete.addActionListener(delListener);
      btnAdd.addActionListener(
         new ActionListener(){
            public void actionPerformed(ActionEvent e)
            {
               int i = JOptionPane.showConfirmDialog(null, addPlayerPanel(), "Add player", JOptionPane.OK_CANCEL_OPTION);
               if(i== JOptionPane.OK_OPTION)
               {
                  Footballer f = new Footballer(playerFName.getText(), playerLName.getText(), (String)comboBox.getSelectedItem());
                  fbListModel.addElement(f);
               }
           }
        });
      panel.add(btnIn); 
      panel.add(btnOut);
      panel.add(btnAdd);
      panel.add(btnDelete);

      return panel;
   } 
   public JPanel addPlayerPanel()
   {
      JPanel playerPanel = new JPanel(new GridLayout(3,1)); 
      playerFName = new JTextField(15);
      playerLName = new JTextField(15);
      String[] options = { "Goalkeeper", "Defender", "Midfielder", "Forward"};
      comboBox = new JComboBox(options);
      playerPanel.add(playerFName);
      playerPanel.add(playerLName);
      playerPanel.add(comboBox);
      return playerPanel;
   }   


   public class ButtonListener implements ActionListener
   {
      public void actionPerformed(ActionEvent e)
      {
         if(e.getSource().equals(btnIn))
         { 
            int[] fromIndex = fbList.getSelectedIndices(); 
            Object[] from = fbList.getSelectedValues();  
            for(Object s : from)  //for each object in from -- in the selected list 
               //add to rhslist
               rhsListModel.addElement((Footballer)s); 
            for(Object s : from)   
               fbListModel.removeElement((Footballer)s);
         }
         else if(e.getSource().equals(btnOut))
         {
            int[] fromIndex = rhsList.getSelectedIndices();
            Object[] from = rhsList.getSelectedValues();
            for(Object s : from)
               fbListModel.addElement((Footballer)s);
            for(Object s : from)   
               rhsListModel.removeElement((Footballer)s);
         }   
      }
   }
   public class DeleteListener implements ActionListener
   {
      public void actionPerformed(ActionEvent d)
      {
         if(d.getSource().equals(btnDelete))
         {
            int[] fromIndex = fbList.getSelectedIndices(); 
            Object[] from = fbList.getSelectedValues();  
            for(Object s : from)   
               fbListModel.removeElement((Footballer)s);
         }
      }
   }            

    // writeRecordsToFile()
   public void writeRecordsToFile() throws IOException,FileNotFoundException
   {        
      ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("players.bin"));   
      os.writeObject(fbArrayList);  
      os.close();  
   }  

    //readRecordsFromFile()
   public void readRecordsFromFile() throws IOException, ClassNotFoundException
   {
      ObjectInputStream is = new ObjectInputStream (new FileInputStream("players.bin"));
      fbArrayList = (ArrayList<Footballer>)is.readObject();  //note use of cast
      is.close();
   }

}

我被一个朋友告诉我使用WindowListener,但不知道如何使用此操作。

阅读有关如何编写WindowListener的Swing教程中的部分。您将实现windowClosing(...)方法并执行逻辑以将数据保存到文件中。

最新更新