我想要做的是通过套接字发送一个 JFrame。问题是在我发送表单并按下按钮查看它之后,我得到了以下异常。
package ds3;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
public class SerializationApp extends JFrame {
private DataForm dataForm = new DataForm();
private int serverPort = 15000;
private ServerSocket serverSocket;
public SerializationApp() {
try {
serverSocket = new ServerSocket(serverPort);
initComponents();
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
try {
Socket socket = serverSocket.accept();
ObjectInputStream objectinputstream = new ObjectInputStream(socket.getInputStream());
DataForm dataform = (DataForm) objectinputstream.readObject();
dataform.setTitle(socket.getInetAddress().getHostAddress());
dataform.setVisible(true);
socket.close();
} catch (ClassNotFoundException ex) {
Logger.getLogger(SerializationApp.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(SerializationApp.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}).start();
} catch (IOException ex) {
Logger.getLogger(SerializationApp.class.getName()).log(Level.SEVERE, null, ex);
}
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
iptextfield = new javax.swing.JTextField();
jPanel2 = new javax.swing.JPanel();
viewButton = new javax.swing.JButton();
sendButton = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Receiver IP Address", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Dialog", 0, 12), new java.awt.Color(0, 0, 204))); // NOI18N
jPanel1.setLayout(new java.awt.BorderLayout());
jPanel1.add(iptextfield, java.awt.BorderLayout.CENTER);
getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);
viewButton.setText("View Form");
viewButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
viewButtonActionPerformed(evt);
}
});
jPanel2.add(viewButton);
sendButton.setText("Send Form");
sendButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
sendButtonActionPerformed(evt);
}
});
jPanel2.add(sendButton);
getContentPane().add(jPanel2, java.awt.BorderLayout.SOUTH);
pack();
}// </editor-fold>
private void viewButtonActionPerformed(java.awt.event.ActionEvent evt) {
dataForm.setVisible(!dataForm.isVisible());
}
private void sendButtonActionPerformed(java.awt.event.ActionEvent evt) {
try {
Socket clientsocket = new Socket(iptextfield.getText(), 15000);
ObjectOutputStream oout = new ObjectOutputStream(clientsocket.getOutputStream());
oout.writeObject(dataForm);
oout.reset();
oout.flush();
clientsocket.close();
} catch (IOException ex) {
Logger.getLogger(SerializationApp.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void main(String args[]) {
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/*
* If Nimbus (introduced in Java SE 6) is not available, stay with the
* default look and feel. For details see
* http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(SerializationApp.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(SerializationApp.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(SerializationApp.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(SerializationApp.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new SerializationApp().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JTextField iptextfield;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JButton sendButton;
private javax.swing.JButton viewButton;
// End of variables declaration
}
序列化类
package ds3;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.Serializable;
public class DataForm extends javax.swing.JFrame implements Serializable {
public DataForm() {
initComponents();
jButton1.addActionListener(new ClearFormActionListener());
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
buttonGroup1 = new javax.swing.ButtonGroup();
jLabel1 = new javax.swing.JLabel();
jPanel1 = new javax.swing.JPanel();
jLabel2 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
jTextField2 = new javax.swing.JTextField();
jLabel4 = new javax.swing.JLabel();
jPanel2 = new javax.swing.JPanel();
jRadioButton1 = new javax.swing.JRadioButton();
jRadioButton2 = new javax.swing.JRadioButton();
jLabel5 = new javax.swing.JLabel();
jTextField4 = new javax.swing.JTextField();
jPanel3 = new javax.swing.JPanel();
jButton1 = new javax.swing.JButton();
setTitle("Personal Data Form");
jLabel1.setFont(new java.awt.Font("Tahoma", 3, 11)); // NOI18N
jLabel1.setForeground(new java.awt.Color(0, 0, 204));
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel1.setText("Please fill you data below");
getContentPane().add(jLabel1, java.awt.BorderLayout.NORTH);
jPanel1.setLayout(new java.awt.GridLayout(4, 2, 0, 2));
jLabel2.setText("Firstname:");
jPanel1.add(jLabel2);
jPanel1.add(jTextField1);
jLabel3.setText("Lastname:");
jPanel1.add(jLabel3);
jPanel1.add(jTextField2);
jLabel4.setText("Gender:");
jPanel1.add(jLabel4);
jPanel2.setLayout(new java.awt.GridLayout(1, 2));
buttonGroup1.add(jRadioButton1);
jRadioButton1.setText("Male");
jPanel2.add(jRadioButton1);
buttonGroup1.add(jRadioButton2);
jRadioButton2.setText("Female");
jPanel2.add(jRadioButton2);
jPanel1.add(jPanel2);
jLabel5.setText("Email:");
jPanel1.add(jLabel5);
jPanel1.add(jTextField4);
getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);
jButton1.setText("Clear");
jPanel3.add(jButton1);
getContentPane().add(jPanel3, java.awt.BorderLayout.SOUTH);
pack();
}// </editor-fold>
// Variables declaration - do not modify
private javax.swing.ButtonGroup buttonGroup1;
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JRadioButton jRadioButton1;
private javax.swing.JRadioButton jRadioButton2;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
private javax.swing.JTextField jTextField4;
// End of variables declaration
class ClearFormActionListener implements ActionListener , Serializable {
@Override
public void actionPerformed(ActionEvent e) {
jTextField1.setText("");
jTextField2.setText("");
jTextField4.setText("");
buttonGroup1.clearSelection();
}
}
}
例外。
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.plaf.synth.SynthLookAndFeel.paintRegion(SynthLookAndFeel.java:395)
at javax.swing.plaf.synth.SynthLookAndFeel.update(SynthLookAndFeel.java:359)
at javax.swing.plaf.synth.SynthRootPaneUI.update(SynthRootPaneUI.java:94)
at javax.swing.JComponent.paintComponent(JComponent.java:765)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5142)
at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:302)
at javax.swing.RepaintManager.paint(RepaintManager.java:1188)
at javax.swing.JComponent.paint(JComponent.java:1015)
at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:39)
at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:78)
at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:115)
at java.awt.Container.paint(Container.java:1784)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:794)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:735)
at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:677)
at javax.swing.RepaintManager.access$700(RepaintManager.java:58)
at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1593)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:226)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:647)
at java.awt.EventQueue.access$000(EventQueue.java:96)
at java.awt.EventQueue$1.run(EventQueue.java:608)
at java.awt.EventQueue$1.run(EventQueue.java:606)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:105)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:617)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:275)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:200)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:185)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:177)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:138)
档案_
更新
更具体。为什么当我发送表单时,表单正常显示给接收者,没有任何异常,之后我看不到来自发件人的表单,没有任何异常。我的意思是,如果这是一个序列化问题,为什么发送者可以毫无问题地将表单发送给接收者,然后View Form
按钮导致异常?