Gui和Serializable错误

时间:2013-12-30 21:15:21

标签: java swing serialization serializable

好的,所以我需要编写一个程序来创建配置文件(简单的只有名称,照片,状态等)并添加存在的朋友。它使用ObjectOutputStream将配置文件保存在文件中。现在我面临的问题是:

  • 我收到序列化错误,我不知道它们来自哪里。 项目提供的错误
  • java.io.NotSerializableException:SocialNetwork at java.io.ObjectOutputStream.writeFields(Unknown Source)at java.io.ObjectOutputStream.writeFields(Unknown Source)at java.io.ObjectOutputStream.writeSerialData(Unknown Source)at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)

代码大约是250行,所以我要添加3条评论,并在错误描述中添加它们。 Ctrl + F (这里是错误的人)是单词

    import javax.swing.*;

    import java.awt.*;
    import java.awt.event.*;

    import javax.swing.border.LineBorder;

    import java.io.*;
    import java.util.ArrayList;
    public class SocialNetwork implements Serializable {
        /**
         * 
         */
        private static final long serialVersionUID = 904698037930887085L;
        RandomAccessFile raf ;
        JTextField setStatus ;
        JTextField addFriend ;
        JTextField emriProfilit ;
        JButton changestatus ;
        JButton setfriend ;
        JButton changepicture ;
        JButton add ;
        JButton delete ;
        JButton lookup ;
        JLabel name ;
        JLabel password ;
        JPasswordField passwordfield ;
        JLabel Banner ;
        SocialProfile profile ;
        SocialProfile temp ;
        JLabel profilepicture ;
        FileOutputStream fileoutput;
        FileInputStream fileinput ;
        ObjectOutputStream output;
        ObjectInputStream input;

        public SocialNetwork() throws FileNotFoundException{

        fileoutput = new FileOutputStream("database.dat",true);
        fileinput = new FileInputStream("database.dat");
        emriProfilit = new JTextField(15);
        name = new JLabel("Name");
        password = new JLabel("Password");
        passwordfield = new JPasswordField (7);
        lookup = new JButton("Lookup");
        add = new JButton("Add");
        delete = new JButton("Delete");
        JPanel p1 = new JPanel() ;
        p1.setLayout(new FlowLayout());
        p1.add(name);
        p1.add(emriProfilit);
        p1.add(password);
        p1.add(passwordfield);
        p1.add(add);
        p1.add(delete);
        p1.add(lookup);
        JFrame frame = new JFrame ("Social Network");
        frame.setSize(1050,500);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        frame.setLayout(new BorderLayout());
        frame.add(p1,BorderLayout.NORTH);
        changestatus = new JButton ("Change Status");
        setfriend = new JButton ("Add Friend");
        changepicture = new JButton ("Change Picture");
        setStatus = new JTextField (20);
        addFriend = new JTextField (15) ;
        ImageIcon sfond= new ImageIcon("images/socialnetwork.png");
        Banner = new JLabel (sfond);
        JPanel p2 = new JPanel() ;
        JPanel p3 = new JPanel() ;
        JPanel p4 = new JPanel() ;
        p4.setLayout(new GridLayout(2,1,10,10));
        p2.setLayout(new GridLayout(5,1,10,10));

        p3.add(Banner);
        p2.add(setStatus);
        p2.add(changestatus);
        p2.add(changepicture);
        p2.add(addFriend);
        p2.add(setfriend);
        p4.add(p3);
        p4.add(p2);
        p4.setBorder(BorderFactory.createLineBorder(Color.black));
    // Error here guys (sometimes it does not show )


frame.add(p4,BorderLayout.WEST);
// Veprimi i cili do kryhet mbasi të klikohet butoni changepicture
changepicture.addActionListener(new ActionListener(){
    @Override
    public void actionPerformed(ActionEvent e){
        File file = browsefile();
        profile.setAvatar(file.getAbsolutePath());
        profilepicture.setIcon(profile.getAvatar());
    }

});
// Veprimi që do realizohet mbasi të jepet emri
    emriProfilit.addActionListener(new ActionListener(){
    @Override
    public void actionPerformed(ActionEvent e){
        profile = new SocialProfile();  
        profile.setName(emriProfilit.getText().trim());
        emriProfilit.setFocusable(false);
    }

    });
    // Veprimi që do realizohet mbasi të jepet password-i
    passwordfield.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e){

            profile.setPassword(passwordfield.getPassword());
            passwordfield.setFocusable(false);
        }

        });
    // Veprimi që do realizohet kur të klikohet butoni changestatus
    changestatus.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e){

            profile.setStatus(changestatus.getText().trim());

        }

        });
    // Veprimi që do kryhet nga butoni add
    add.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e){

            try {
                add(profile);
            } catch (IOException e1) {

                e1.printStackTrace();
            }

        }

        });
    // Veprimi që do kryhet nga butoni lookup
    lookup.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e){
            String name ;   
            name = emriProfilit.getText().trim();
            try {
                if(doesitexist(name))
                    System.out.println("Ekziston");
                else
                    System.out.println("Nuk Ekziston");
            } catch (ClassNotFoundException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            emriProfilit.setFocusable(false);
            passwordfield.setFocusable(false);
        }

        });
}
    // Error here Guys


    public void add(SocialProfile profile) throws IOException{
            output = new ObjectOutputStream(fileoutput);
            output.writeObject(profile);
        }
        //Error here Guys


    public boolean doesitexist(String name) throws IOException, ClassNotFoundException{
            input = new ObjectInputStream(fileinput);

            try{
                while(true){
            temp = (SocialProfile)(input.readObject());
                if (temp.getName().equals(name)){
                    return true ;
                }
                }
            }
            catch (EOFException ex){
                ex.printStackTrace();
                return false ;
            }
        }
        public File browsefile(){
            JFileChooser filebrowse = new JFileChooser ();
            if (filebrowse.showOpenDialog(null) == JFileChooser.APPROVE_OPTION){

            }
            File file = filebrowse.getSelectedFile();
            return file ;
        }



        public static void main (String[] args) throws FileNotFoundException{
        new SocialNetwork();
    }
        public class SocialProfile implements Serializable {
            /**
             * 
             */
            private static final long serialVersionUID = 2436758770296955312L;
            /**
             * 
             */

            private String name ;
            private ImageIcon avatar;
            private String status ;
            private ArrayList<String> friends ;
            private char[] password ;



            public SocialProfile(){
                status = new String ("Default Status");
                friends = new ArrayList<String>( );
                avatar = new ImageIcon ("images/defaultavatar.jpg");

            }
            public String getName (){
                return name ;
            }
            public void setName(String name){
                this.name = name ;
            }
            public void setPassword (char[] pass){
                password = pass ;
            }
            String getStatus (){
                return status ;
            }

            void setStatus (String status){
                this.status = status ;
            }

            ImageIcon getAvatar (){
                return avatar ;
            }
            void setAvatar (String avatarpicture){
                avatar = new ImageIcon(avatarpicture) ;
            }
            void addFriend (String name){
                friends.add(name);
            }
        }
    }

3 个答案:

答案 0 :(得分:1)

不要尝试序列化GUI组件。尝试序列化对象模型和对象模型以动态加载ui。

答案 1 :(得分:1)

您应该在使用后关闭流,您应该在写入文件后关闭文件。

关于不显示边框,你应该在添加每个组件后调用frame.setVisible(true),如果仍然没有显示你应该调用框架的repaint()或updateui()方法,如果你只使用一帧,我建议你从JFrame类扩展你的类,这是为了更容易使用。

关于序列化,我认为您的代码是正确的,您在构建项目后遇到的错误是什么?

编辑: 我发现这个page 关于这个问题,看起来你的类中有一个非序列化对象,所以首先从你的SocialNetwork类中删除implements Serializable部分,并尝试找到NotSerializeable对象在您的SocialProfile类中。我将删除代码部分并开始测试它,例如只保留原始类型,并检查是否能够以这种方式序列化。

增加文件大小意味着发生了某些事情,尝试在运行序列化代码之前删除该文件。

答案 2 :(得分:1)

序列化的类不应该被定义为内部类。我将该类声明为新的公共类,它可以正常工作。