0

を連載したかったのJFrameですが、残念ながらNotSerializableException. が理由だと思ったので、修飾子を JFileChoosers追加しました:transienttransient JFileChooser fc = new JFileChooser(".");

そして、それらを削除すると問題が解決するためJFileChooserですが、シリアライゼーションに対して本当に一時的にする方法を使用する必要がありますか?

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.border.BevelBorder;
import javax.swing.border.TitledBorder;


public class GlowneOkno extends JFrame{
    private static final long serialVersionUID = 1L;
    private JPanel panelPrawo , panelDol;
    private Okienko panelSr ;
    private final static String[] etykietyFigur = {"Elipsa" , "Kolo" , "Kwadrat" , "Prostokat" , "Trojkat" , "Trojkat Rownoboczny"};
    private static String[] etykietyPrzyciskowKontrolnych = {"Play", "Pause", "Stop"};
    //private JButton playButton, pauseButton, stopButton, elipsaButton, koloButton, kwadratButton, okragButton, prostokatButton, trojkatButton, trojkatRButton;
    private JButton[] przyciskiFigur, przyciskiKontrolne;
    private final static int SIZE = 550;    
    //-------
    private JTabbedPane jtp;
    private MyListModel modelListy; 
    private JList jl;
    private MyListCellRenderer cellRender;

    public GlowneOkno() {
        super("Program");
        createGUI();
    }

    private void createGUI(){
        setSize(SIZE + 120, SIZE + 100);
    //  setPreferredSize(new Dimension(800, SIZE));
        createMenus();
        modelListy = new MyListModel(new ArrayList<>());
         panelSr = new Okienko(modelListy);
         panelDol = new JPanel(new GridLayout(1,3,50,60));
         panelPrawo = new JPanel();

         ActionListener zmieniaczFigur = new ActionListener() {     
            @Override
            public void actionPerformed(ActionEvent e) {
                 JButton jb = (JButton) e.getSource();
                 System.out.println("lkiojgwkjhugfshbkjuidsafhbkfaqwsuki");
                 switch(jb.getText()){
                     case "Elipsa":  ((Okienko) panelSr).setWylosowanaFigura(Okienko.ELIPSA); break;
                     case "Kolo":  ((Okienko) panelSr).setWylosowanaFigura(Okienko.KOLO); break;
                     case "Kwadrat":  ((Okienko) panelSr).setWylosowanaFigura(Okienko.KWADRAT); break;
                     case "Prostokat":  ((Okienko) panelSr).setWylosowanaFigura(Okienko.PROSTOKAT); break;
                     case "Trojkat":  ((Okienko) panelSr).setWylosowanaFigura(Okienko.TROJKAT); break;
                     case "Trojkat /nRownoboczny":  ((Okienko) panelSr).setWylosowanaFigura(Okienko.TROJKAT_ROWNOBOCZNY); break;
                 }
            }
        };
        ActionListener ppsListener = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                 JButton jb = (JButton) e.getSource();
                 switch(jb.getText()){
                 case "Play": ((Okienko) panelSr).playThreads(); break;
                 case "Pause":((Okienko) panelSr).pauseThreads(); break;
                 case "Stop": ((Okienko) panelSr).stopThreads(); break;
                }
            }
        };

        przyciskiFigur = dodajButtony(panelPrawo, przyciskiFigur, etykietyFigur);
        panelPrawo.setLayout(new GridLayout(przyciskiFigur.length , 1));
        add(panelPrawo, BorderLayout.EAST);
        jtp = new JTabbedPane(JTabbedPane.BOTTOM);
        jtp.setPreferredSize(new Dimension(550, 120));
        przyciskiKontrolne = dodajButtony(panelDol, przyciskiKontrolne, etykietyPrzyciskowKontrolnych);
        dodajListenera(przyciskiKontrolne, ppsListener);

        for(JButton button: przyciskiKontrolne)
            button.setFont(new Font("", Font.BOLD, 24));        
        System.out.println(przyciskiFigur.length);
        dodajListenera(przyciskiFigur, zmieniaczFigur);
        jtp.addTab("Kontrola", panelDol);
        add(jtp,BorderLayout.SOUTH);
    //dodawanie drugiej zakladki---> listy

        jl = new JList(modelListy);
        cellRender = new MyListCellRenderer();
        jl.setCellRenderer(cellRender);


        JScrollPane jsp = new JScrollPane();
        jsp.getViewport().setView(jl);
        jtp.addTab("Figury", jsp);

        //


        BevelBorder b = new BevelBorder(BevelBorder.LOWERED);
        panelSr.setBorder(new TitledBorder(b, "Latajace figury"));
        add(panelSr, BorderLayout.CENTER);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    private void createMenus() {
        JMenuBar jmb = new JMenuBar();
        JMenu jm = new JMenu("File");
        JMenuItem save = new JMenuItem("save");
        JMenuItem load = new JMenuItem("load");

        save.addActionListener(new ActionListener() {
            transient JFileChooser fc = new JFileChooser(".");
            @Override
            public void actionPerformed(ActionEvent e) {

                int returnVal = fc.showSaveDialog(GlowneOkno.this);      
                   if (returnVal == JFileChooser.APPROVE_OPTION) {
                        File file = fc.getSelectedFile();
                        save(file);
                   }
            }

            private void save(File file) {
                 FileOutputStream fos = null;
                 ObjectOutputStream oos = null;
                 /*
                  * Zapis do strumienia (plikowego, ale może być dowolne)
                  */
                 try {
                   fos= new FileOutputStream(file); //utworzenie strumienia wyjściowego
                   oos = new ObjectOutputStream(fos);  //utworzenie obiektu zapisującego do strumienia

                   oos.writeObject(GlowneOkno.this); //serializacja obiektu

                 } catch (FileNotFoundException e) {
                   e.printStackTrace();
                 } catch (IOException e) {
                   e.printStackTrace();

                 } finally {
                   // zamykamy strumienie w finally
                   try {
                     if (oos != null) oos.close();
                   } catch (IOException e) {}
                   try {
                     if (fos != null) fos.close();
                   } catch (IOException e) {}
                 }

            }
        });
        load.addActionListener(new ActionListener() {
            transient JFileChooser fc = new JFileChooser(".");
            @Override
            public void actionPerformed(ActionEvent e) {

                int returnVal = fc.showOpenDialog(GlowneOkno.this);
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                        File file = fc.getSelectedFile();
                        load(file);
                  }
            }

            private void load(File file) {
                 FileInputStream fis = null;
                 ObjectInputStream ois = null;
                 try {
                   fis = new FileInputStream(file); //utworzenie strumienia wejściowego  
                   ois = new ObjectInputStream(fis); //utworzenie obiektu odczytującego obiekty ze strumienia
                   GlowneOkno o = (GlowneOkno) ois.readObject();

            /*     ArrayList<Przesuwacz> wczytanaListaPrzesuwaczy = (ArrayList<Przesuwacz>) ois.readObject();
                   panelSr.setListaPrzesuwaczy(wczytanaListaPrzesuwaczy); //deserializacja obiektu
                   ArrayList<Wyrysowywalny> w = new ArrayList<Wyrysowywalny>();
                   for(Przesuwacz p: wczytanaListaPrzesuwaczy){
                       w.add(p.getF());
                   }
                   panelSr.l = w;
                   System.out.println(w.size() +  " -----------------------------------------");*/
                 } catch (FileNotFoundException e) {
                   e.printStackTrace();
                 } catch (IOException e) {
                   e.printStackTrace();
                 } catch (ClassNotFoundException e) {
                   e.printStackTrace();
                 } finally {
                   // zasoby zwalniamy w finally
                   try {
                     if (ois != null) ois.close();
                   } catch (IOException e) {}
                   try {
                     if (fis != null) fis.close();
                   } catch (IOException e) {}
                 }

            }
        });

        jm.add(save); jm.add(load); jmb.add(jm);
        setJMenuBar(jmb);
    }

    private JButton[] dodajButtony(JPanel jp , JButton[] bTab , String[] etykiety ){
        bTab = new JButton[etykiety.length];
        for(int i = 0; i < bTab.length; i++){
            bTab[i] = new JButton(etykiety[i]);
            jp.add(bTab[i]);
        }
        return bTab;
    }

    private void dodajListenera(JButton[] bTab , ActionListener al){
        for(JButton jb : bTab)
            jb.addActionListener(al);   
    }


    public static void main(String[] args) {
        new GlowneOkno();
    }

}

エラー

java.io.NotSerializableException: javax.swing.plaf.metal.MetalFileChooserUI
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
    at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
    at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.writeObject(Unknown Source)
    at javax.swing.event.EventListenerList.writeObject(Unknown Source)
    at sun.reflect.GeneratedMethodAccessor6.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at java.io.ObjectStreamClass.invokeWriteObject(Unknown Source)
    at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
    at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
    at java.io.ObjectOutputStream.defaultWriteObject(Unknown Source)
    at javax.swing.JComponent.writeObject(Unknown Source)
    at sun.reflect.GeneratedMethodAccessor10.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at java.io.ObjectStreamClass.invokeWriteObject(Unknown Source)
    at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
    at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
    at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
    at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
    at java.io.ObjectOutputStream.defaultWriteObject(Unknown Source)
    at java.awt.Window.writeObject(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at java.io.ObjectStreamClass.invokeWriteObject(Unknown Source)
    at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
    at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.writeObject(Unknown Source)
    at java.awt.Window.writeObject(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at java.io.ObjectStreamClass.invokeWriteObject(Unknown Source)
    at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
    at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.writeObject(Unknown Source)
    at GlowneOkno$3.save(GlowneOkno.java:152)
    at GlowneOkno$3.actionPerformed(GlowneOkno.java:138)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.AbstractButton.doClick(Unknown Source)
    at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
    at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
4

1 に答える 1