gpt4 book ai didi

java - Gui 和可序列化错误

转载 作者:行者123 更新时间:2023-12-01 23:18:43 25 4
gpt4 key购买 nike

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

  • 我遇到了可序列化错误,但我不知道它们来自哪里。项目给出的错误
  • java.io.NotSerializedException:社交网络在 java.io.ObjectOutputStream.writeObject0(未知来源) 在 java.io.ObjectOutputStream.defaultWriteFields(未知来源) 在 java.io.ObjectOutputStream.writeSerialData(未知来源) 在 java.io.ObjectOutputStream.writeOrdinaryObject(未知来源)<

代码大约有 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);
}
}
}

最佳答案

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

关于java - Gui 和可序列化错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20847711/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com