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 が Serializable ではないことを明確に示しています。これを修正する最も合理的な方法は、クライアント (WindowClient が拡張するもの) をシリアライズ可能にすることです。

于 2013-05-21T04:40:37.647 に答える
3

クラスEventは の内部クラスでWindowGameActualあるため、Event のインスタンスをシリアル化しようとすると、WindowGameActual の外側のインスタンスとそのすべてのフィールドも同様にシリアル化されます。

解決策: 本当に適切でない限り、内部クラスを使用しないでください。Event クラスを独自のファイルに昇格させます。

于 2013-05-21T04:36:46.447 に答える