0

好的,所以我有这个扩展 JFrame 的类,我基本上是在尝试创建这个 JPanel,它接收信息并在按下提交按钮时发送一个“事件”对象。一切似乎都在工作,除了当我按下提交时,它告诉我我的客户端需要可序列化......(客户端类是一个基本类,它打开到特定端口的连接。它有效,我已经测试过了这不是问题)。我已经序列化了我要发送的所有对象。我不明白为什么我会收到 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

如果您需要我的任何其他课程,请告诉我。

谢谢!

4

3 回答 3

4

异常消息清楚地表明 WindowClient 不可序列化。修复它的最合理方法是使 Client(WindowClient 扩展)可序列化

于 2013-05-21T04:40:37.647 回答
3

您的类Event是 的内部类WindowGameActual,因此每当您尝试序列化 Event 的实例时,WindowGameActual 的封闭实例及其所有字段也将被序列化。

解决方案:除非真的合适,否则不要使用内部类,将 Event 类提升到自己的文件中。

于 2013-05-21T04:36:46.447 回答
-1

看看 XStream http://x-stream.github.io/它具有与序列化/反序列化相同的功能,除了您处理的是 XML。不需要一个类来实现 Serializable。这将解决您的所有问题,无需任何解决方法。

于 2013-05-21T05:28:07.000 回答