2017-04-22 23 views
0

需要這個幻想足球計劃的一些幫助,努力想辦法如何實現我的writetofile方法到添加按鈕,然後當我退出程序時保存它關閉。寫入文件,然後用WindowListener保存它

我已經告訴朋友使用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(); 
    } 


} 
+0

雖然重複的問題是處理「幀位置」,但您可以在此處按照相同的方法來序列化和恢復用戶輸入的數據。 –

回答

1

有人告訴我的朋友使用WindowListener的,但不知道如何使用此。

閱讀How to Write a WindowListener上的Swing教程部分。您可以實施windowClosing(...)方法並執行邏輯以將數據保存到文件。

相關問題