Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 序列化JFrame并通过网络发送_Java_Sockets_Exception_Serialization_Jframe - Fatal编程技术网

Java 序列化JFrame并通过网络发送

Java 序列化JFrame并通过网络发送,java,sockets,exception,serialization,jframe,Java,Sockets,Exception,Serialization,Jframe,我要做的是通过套接字发送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; impor

我要做的是通过套接字发送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                   
}
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)

更新

更具体地说。 为什么当我发送表单时,表单会正常地出现在接收者面前,没有任何例外,之后我就无法从发送者那里看到表单,没有任何例外。 我的意思是,如果这是一个序列化问题,为什么发送方可以向接收方发送表单而无需
有什么问题吗?之后,查看表单按钮会导致异常?

我在几年前做了类似的事情,结果发送了一个
JPanel
,而不是一个帧本身

我认为这种情况下的问题在于,框架在某种程度上连接到了一个
图形
,而这个图形当然不是通过网络序列化和发送的,因为它在实现中非常接近主机操作系统(在AWT
窗口
级别)


我建议您尝试只发送一个面板(最终只是您尝试发送的框架的内容窗格)再试一次。如果您真的需要附加框架属性,您可以将它们封装在自定义对象中,并通过序列化(例如标题或其他方式)附加它们。

我在几年前做了类似的事情,结果发送了一个
JPanel
,而不是框架本身

我认为这种情况下的问题在于,框架在某种程度上连接到了一个
图形
,而这个图形当然不是通过网络序列化和发送的,因为它在实现中非常接近主机操作系统(在AWT
窗口
级别)


我建议您尝试只发送一个面板(最终只是您尝试发送的框架的内容窗格)再试一次。如果您真的需要附加框架属性,您可以将它们封装在自定义对象中,并通过序列化(例如标题或其他任何内容)来附加它们。

看起来L&F序列化得不好,但不管怎样,我的建议还是将GUI的模型序列化并通过发送状态。然后,您可以使用此状态轻松地重新创建视图,发送的数据量将减少几个数量级

编辑1:例如,在您发布的代码中,您将创建一个PersonalData类作为您的模型,该类有三个用于名字、姓氏和电子邮件的字符串字段,以及一个用于性别的布尔(或更好的枚举)字段


然后,通过套接字发送的信息将是这个“模型”类的对象,而不是整个Swing GUI。您可以从传递的对象中复制填充数据的GUI。

看起来L&F序列化得不好,但不管怎样,我的建议还是序列化GUI的模型并发送其状态。然后,您可以使用此状态轻松地重新创建视图,发送的数据量将减少几个数量级

编辑1:例如,在您发布的代码中,您将创建一个PersonalData类作为您的模型,该类有三个用于名字、姓氏和电子邮件的字符串字段,以及一个用于性别的布尔(或更好的枚举)字段

然后,通过套接字发送的信息将是这个“模型”类的对象,而不是整个Swing GUI。您可以从传递的对象中复制填充数据的GUI

我要做的是通过套接字发送JFrame

为什么??从:

警告:此类的序列化对象将与不兼容 未来的Swing版本。当前的序列化支持是 适用于运行的应用程序之间的短期存储或RMI 同样版本的Swing。从1.4开始,支持长期储存 所有JavaBeansTM都已添加到java.beans包中。请看 XMLEncoder

不要忽视这些警告。他们在那里是有原因的

我要做的是通过套接字发送JFrame

为什么??从:

警告:此类的序列化对象将与不兼容 未来的Swing版本。当前的序列化支持是 适用于运行的应用程序之间的短期存储或RMI 同样版本的Swing。从1.4开始,支持长期储存 所有JavaBeansTM都已添加到java.beans包中。请看 XMLEncoder


不要忽视这些警告。它们的存在是有原因的。

JPanel
implementation

跟随@Jack advices发送
JPanel
而不是
JFrame
似乎是可行的。 下面是实现

package ds3;

import java.awt.Container;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class SerializationApp extends JFrame {

    private DataPanel dataPanel = new DataPanel();
    private JFrame frPanel = new JFrame();
    private int serverPort = 15000;
    private ServerSocket serverSocket;

    public SerializationApp() {
        try {
            serverSocket = new ServerSocket(serverPort);
            initComponents();
            setLocationRelativeTo(null);
            frPanel.add(dataPanel);
            frPanel.pack();
            frPanel.setLocationRelativeTo(this);
            frPanel.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
            new Thread(new Runnable() {

                @Override
                public void run() {
                    while (true) {
                        try {
                            Socket socket = serverSocket.accept();
                            ObjectInputStream objectinputstream = new ObjectInputStream(socket.getInputStream());
                            JPanel container = (JPanel) objectinputstream.readObject();
                            JFrame frame = new JFrame(socket.getInetAddress().getHostAddress());
                            frame.add(container);
                            frame.pack();
                            frame.setLocationRelativeTo(null);
                            setVibility(frame);


                            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 setVibility(final JFrame dataform) {

        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                dataform.setVisible(!dataform.isVisible());
            }
        });
    }

private void viewButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
    setVibility(frPanel);
}                                          

private void sendButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
    try {
        Socket clientsocket = new Socket(iptextfield.getText(), 15000);
        ObjectOutputStream oout = new ObjectOutputStream(clientsocket.getOutputStream());
        oout.writeObject(dataPanel);
        oout.reset();
        oout.flush();
        clientsocket.close();
    } catch (UnknownHostException ex) {
        Logger.getLogger(SerializationApp.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(SerializationApp.class.getName()).log(Level.SEVERE, null, ex);
    }
}                                          

    public static void main(String args[]) {

        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception ignored) {
            ignored.printStackTrace();
        }

        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                   
}
XMLEncoder
实现

至于
xmlcoder
,如果我对整个类进行编码,字段上的输入不会被传输

主课

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.XMLEncoder;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.UIManager;

public final class MainClass extends JFrame {

    private TheFRameForm x;

    public MainClass() {
        x = new TheFRameForm();
        initComponents();
        TheServer theServer = new TheServer();
        theServer.start();

    }

    void initComponents() {
        setTitle("Look At Me Now");
        setLayout(new FlowLayout());
        JButton b = new JButton("Show Form");
        b.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                x.setVisible(true);
            }
        });
        add(b);
        JButton s = new JButton("Send Form");
        s.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                sendForm();
            }
        });
        add(s);
        pack();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    private void sendForm() {
        try {
            Socket clientsocket = new Socket("localhost", 15000);
            XMLEncoder encoder = new XMLEncoder(clientsocket.getOutputStream());
            encoder.writeObject(x); //.getContentPane()
            encoder.close();

            OutputStream memStream = new ByteArrayOutputStream();
            XMLEncoder mencoder = new XMLEncoder(memStream);
            mencoder.writeObject(x);//.getContentPane()
            mencoder.close();
            String xmlString = memStream.toString();
            System.out.println(xmlString);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String args[]) {

        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception ignored) {
            ignored.printStackTrace();
        }

        MainClass theMainclass = new MainClass();
    }
}
服务器类

import java.beans.XMLDecoder;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;

public class TheServer extends Thread {

    private static final int serverPort = 15000;
    private ServerSocket serverSocket;

    public TheServer() {
        try {
            serverSocket = new ServerSocket(serverPort);
        } catch (IOException ex) {
            Logger.getLogger(TheServer.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void run() {
        try {
            while (true) {
                Socket socket = serverSocket.accept();
                System.out.println("Server is running");
                XMLDecoder decoder = new XMLDecoder(socket.getInputStream());
                JFrame frame = (JFrame) decoder.readObject();
                frame.setVisible(true);
                socket.close();
            }
        } catch (IOException ex) {
            Logger.getLogger(TheServer.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
JFrame

public class TheFRameForm extends javax.swing.JFrame {

    public TheFRameForm() {
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jButton1.setText("jButton1");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel1.setText("jLabel1");

        jTextField1.setText("jTextField1");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(123, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addGap(54, 54, 54)
                .addComponent(jLabel1)
                .addGap(102, 102, 102))
            .addGroup(layout.createSequentialGroup()
                .addGap(149, 149, 149)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(120, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jButton1))
                .addGap(57, 57, 57)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(65, 65, 65))
        );

        pack();
    }// </editor-fold>                        

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        System.out.println("Print");      
    }                                        

    public static void main(String args[]) {

        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(TheFRameForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(TheFRameForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(TheFRameForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TheFRameForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TheFRameForm().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration                   
}
frameform公共类扩展了javax.swing.JFrame{
公共框架(){
初始化组件();
}
@抑制警告(“未选中”)
//                           
私有组件(){
jButton1=newjavax.swing.JButton();
jLabel1=newjavax.swing.JLabel();
jTextField1=newjavax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE\u ON\u CLOSE);
setText(“jButton1”);
jButton1.addActionListener(新java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent evt){
jButton1ActionPerformed(evt);
}
});
jLabel1.setText(“jLabel1”);
jTextField1.setText(“jTextField1”);
javax.swing.GroupLayout=newjavax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(布局);
layout.setHorizontalGroup(
createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
公元
import java.beans.XMLDecoder;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;

public class TheServer extends Thread {

    private static final int serverPort = 15000;
    private ServerSocket serverSocket;

    public TheServer() {
        try {
            serverSocket = new ServerSocket(serverPort);
        } catch (IOException ex) {
            Logger.getLogger(TheServer.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void run() {
        try {
            while (true) {
                Socket socket = serverSocket.accept();
                System.out.println("Server is running");
                XMLDecoder decoder = new XMLDecoder(socket.getInputStream());
                JFrame frame = (JFrame) decoder.readObject();
                frame.setVisible(true);
                socket.close();
            }
        } catch (IOException ex) {
            Logger.getLogger(TheServer.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
public class TheFRameForm extends javax.swing.JFrame {

    public TheFRameForm() {
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jButton1.setText("jButton1");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel1.setText("jLabel1");

        jTextField1.setText("jTextField1");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(123, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addGap(54, 54, 54)
                .addComponent(jLabel1)
                .addGap(102, 102, 102))
            .addGroup(layout.createSequentialGroup()
                .addGap(149, 149, 149)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(120, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jButton1))
                .addGap(57, 57, 57)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(65, 65, 65))
        );

        pack();
    }// </editor-fold>                        

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        System.out.println("Print");      
    }                                        

    public static void main(String args[]) {

        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(TheFRameForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(TheFRameForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(TheFRameForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TheFRameForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TheFRameForm().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration                   
}