java - 写入文件,然后使用 WindowListener 保存它

标签 java swing file-io jframe windowlistener

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

一位 friend 告诉我使用 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();
   }


}

最佳答案

I've been told by a friend to use a WindowListener but have no idea how to use this.

阅读 Swing 教程中关于 How to Write a WindowListener 的部分。您将实现 windowClosing(...) 方法并执行逻辑以将数据保存到文件中。

关于java - 写入文件,然后使用 WindowListener 保存它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43560232/

相关文章:

java - 如何在 java 中为 iphone 创建一个 "Hello World"应用程序?

java - rJava::.jnew ("edu.stanford.nlp.pipeline.StanfordCoreNLP"中的错误,基本名称(路径))

java - JTable 的页脚

java - 为什么我的移动 Sprite 动画尝试会起作用?

java - 设置按钮可见

c - 从 C 文件中读取整数

java - 我的边缘碰撞算法无法正常工作

java - 如何将非英语单词作为文本部分添加到volley库中的多部分请求中?

c# - 是否可以创建一个内存文件并使用 FileInfo 访问它?

Java - 读取文本文件,将内容存储在ArrayList中,打印ArrayList