好吧,我有这个类扩展了JFrame,我基本上试图创建这个JPanel,它接收信息并发送一个“Event”对象按下Submit按钮。一切似乎都在工作,除了当我按下提交时,它告诉我我的客户端需要可序列化......(客户端类是一个基本的类,它打开一个连接到特定的端口。它的工作原理,我已经测试了它这不是问题)。我已将所有要发送的对象序列化。我不明白为什么我得到一个NotSerializableException。一直试图找出现在几个小时。任何有识之士将不胜感激。即使我发送的所有对象都是可序列化的,但得到一个NotSerializableException异常
这里是我的代码:
public class WindowGameActual extends JFrame implements ActionListener
{
private final static int PORT = 11114;
private GameState game;
public WindowClient connection;
JPanel container;
JTextArea description;
JTextField difficulty;
JCheckBox check4;
JCheckBox check6;
JCheckBox check8;
JCheckBox check12;
JCheckBox check20;
JCheckBox agl;
JCheckBox str;
JCheckBox mana;
public class WindowClient extends Client
{
public WindowClient(String host) throws IOException
{
super(host, PORT);
}
protected void messageReceived(Object message)
{
if(message instanceof GameState)
{
game = (GameState) message;
container.repaint();
}
}
} //end WindowClient
public WindowGameActual(String host)
{
setDefaultCloseOperation(EXIT_ON_CLOSE);
game = new GameState();
try {
connection = new WindowClient(host);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
container = new JPanel();
GridLayout gridC = new GridLayout(3, 1);
container.setLayout(gridC);
CurrentEventPanel curEvt = new CurrentEventPanel(game);
curEvt.updateEvent(game);
container.add(curEvt);
// ControlPanel eventControl = new ControlPanel(host);
// container.add(eventControl);
GridLayout outerPanel = new GridLayout(4, 1);
JPanel control = new JPanel();
control.setLayout(outerPanel);
JPanel layer1 = new JPanel();
JLabel eDescription = new JLabel("Describe Event:");
layer1.add(eDescription);
description = new JTextArea("");
description.setEditable(true);
description.setColumns(17);
layer1.add(description);
control.add(layer1);
JPanel layer2 = new JPanel();
JLabel eDifficulty = new JLabel("Event Difficulty (integers only):");
layer2.add(eDifficulty);
difficulty = new JTextField("");
difficulty.setEditable(true);
difficulty.setColumns(2);
layer2.add(difficulty);
JButton submit = new JButton("Submit");
submit.addActionListener(this);
submit.setPreferredSize(new Dimension(74, 22));
layer2.add(submit);
control.add(layer2);
JPanel layer3 = new JPanel();
JLabel dice = new JLabel("dice: ");
layer3.add(dice);
JLabel d4 = new JLabel("d4");
check4 = new JCheckBox();
layer3.add(d4);
layer3.add(check4);
JLabel d6 = new JLabel("d6");
check6 = new JCheckBox();
layer3.add(d6);
layer3.add(check6);
JLabel d8 = new JLabel("d8");
check8 = new JCheckBox();
layer3.add(d8);
layer3.add(check8);
JLabel d12 = new JLabel("d12");
check12 = new JCheckBox();
layer3.add(d12);
layer3.add(check12);
JLabel d20 = new JLabel("d20");
check20 = new JCheckBox();
layer3.add(d20);
layer3.add(check20);
control.add(layer3);
JPanel layer4 = new JPanel();
JLabel skills = new JLabel("Skills Required: ");
layer4.add(skills);
JLabel strLabel = new JLabel("Str");
str = new JCheckBox();
layer4.add(strLabel);
layer4.add(str);
JLabel aglLabel = new JLabel("Agl");
agl = new JCheckBox();
layer4.add(aglLabel);
layer4.add(agl);
JLabel manaLabel = new JLabel("Mana");
mana = new JCheckBox();
layer4.add(manaLabel);
layer4.add(mana);
control.add(layer4);
container.add(control);
// setPreferredSize(new Dimension(200, 20));
UserStatPanel stats = new UserStatPanel(game);
stats.updateStatPanel(game);
container.add(stats);
add(container);
setVisible(true);
}
public class Event implements Serializable
{
public String eventDescription ;
public String diff ;
public boolean strChecked ;
public boolean aglChecked ;
public boolean manaChecked ;
public boolean d4checked ;
public boolean d6checked ;
public boolean d8checked ;
public boolean d12checked ;
public boolean d20checked;
public Event()
{
eventDescription = description.getText();
diff = difficulty.getText();
strChecked = str.isSelected();
aglChecked = agl.isSelected();
manaChecked = mana.isSelected();
d4checked = check4.isSelected();
d6checked = check6.isSelected();
d8checked = check8.isSelected();
d12checked = check12.isSelected();
d20checked = check20.isSelected();
}
}
Event nEvent;
public void actionPerformed(ActionEvent evt)
{
String cmd = evt.getActionCommand();
if(cmd.equals("Submit"))
{
nEvent = new Event();
connection.send(nEvent);
}
}
}
这是我的错误信息(这是异常短):
Client send thread terminated by IOException: java.io.NotSerializableException: testGame.WindowGameActual$WindowClient
Client send thread terminated.
Client receive thread terminated.
Hub receive thread terminated by IOException: java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: testGame.WindowGameActual$WindowClient
如果你需要我的任何其他类的让我知道。
谢谢!
异常说'testGame.WindowGameActual $ WindowClient'不可序列化。请检查'Client'类是否可序列化。如果可能的话,发布'Client'类也是.. –
此外,序列化扩展摆动组件或包含摆动组件的对象不是一个好习惯。 –
将所有这些摆动标记标记为“transient”,并且您将被解除此问题。 –