1

我想要做的是通过套接字发送一个 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按钮导致异常?

4

4 回答 4

2

看起来 L&F 没有很好地序列化,但无论如何,我的建议再次是序列化 GUI 的模型并发送其状态。然后,您可以轻松地使用此状态重新创建视图,并且发送的数据量将减少几个数量级。

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

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

于 2012-05-12T20:24:27.363 回答
2

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

为什么?来自JFrame 的 Javadoc

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

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

于 2012-05-13T10:01:48.597 回答
1

几年前我做了类似的事情,最终我发送了 JPanel一个框架本身。

我认为在这种情况下的问题在于,框架在某种程度上附加到了一个Graphics,当然,它没有被序列化并通过网络发送,因为它在实现中非常靠近主机操作系统(在 AWTWindow级别)

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

于 2012-05-12T20:23:17.813 回答
0

JPanel执行

遵循@Jack 的建议发送 aJPanel而不是 aJFrame似乎可行。下面是实现。

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                   
}

JPanel

在收到JPanel只有第二个ActionListener作品。

package ds3;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.Serializable;

public class DataPanel extends javax.swing.JPanel implements Serializable,ActionListener {

    public DataPanel() {
        initComponents();
        jButton1.addActionListener(this);
    }

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

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jTextField3 = new javax.swing.JTextField();
        jTextField4 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();

        jLabel1.setText("jLabel1");
        add(jLabel1);

        jLabel2.setText("jLabel1");
        add(jLabel2);

        jLabel3.setText("jLabel1");
        add(jLabel3);

        jLabel4.setText("jLabel1");
        add(jLabel4);

        jTextField1.setText("jTextField1");
        add(jTextField1);

        jTextField2.setText("jTextField1");
        add(jTextField2);

        jTextField3.setText("jTextField1");
        add(jTextField3);

        jTextField4.setText("jTextField1");
        add(jTextField4);

        jButton1.setText("jButton1");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        add(jButton1);
    }// </editor-fold>                        

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

    // Variables declaration - do not modify                     
    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.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    // End of variables declaration                   

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("Print");
    }
}

XMLEncoder执行

至于XMLEncoder如果我对整个班级进行编码,则不会传输字段上的输入。

主要班

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                   
}
于 2012-05-15T11:32:15.753 回答