Serializing a JFrame and sending via network(序列化 JFrame 并通过网络发送)
问题描述
我想要做的是通过套接字发送一个 JFrame.问题是在我发送表单并按下按钮查看它之后我得到以下异常.
What I'm trying to do is to send a JFrame through sockets. The problem is after I send the form and press the button to view it I get the below exception.
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
按钮导致异常?
To be more specific.
Why when I send the form ,the form appears to the receiver normally without any exception and after that I can't see the form from the sender without get any exception.
I mean if it's a serialize problem why the sender can send to the receiver the form without
any problem and after that the View Form
button cause the exception?
推荐答案
几年前我做过类似的事情,最后我发送了一个 JPanel
而不是一个框架本身.
I did it something similar years ago and I ended up sending a JPanel
instead that a frame itself.
我认为这种情况下的问题在于框架在某种程度上附加到 Graphics
上,当然,它没有被序列化并通过网络发送,因为它确实靠近主机操作系统在实现中(在 AWT Window
级别)
I think that the problem in this situation is the fact that the frame is somewhat attached to a Graphics
which is not, of course, serialized and sent over network becuse it's really near to the host OS in the implementation (at AWT Window
level)
我建议您尝试仅发送一个面板(最终只是您尝试发送的框架的内容窗格)再试一次.如果您确实需要附加框架属性,您可以将它们封装在自定义对象中并通过序列化(例如标题或其他)附加它们.
I suggest you to try sending just a panel (which in the end is just the content pane of the frame that you are trying to send) a try again. If you really need to attach frame properties you could encapsulate them in a custom object and attach them by serialization (eg title or whatever).
这篇关于序列化 JFrame 并通过网络发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:序列化 JFrame 并通过网络发送
- 获取数字的最后一位 2022-01-01
- 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
- java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
- Eclipse 的最佳 XML 编辑器 2022-01-01
- 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
- 未找到/usr/local/lib 中的库 2022-01-01
- 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01
- 如何指定 CORS 的响应标头? 2022-01-01
- GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
- 转换 ldap 日期 2022-01-01