0

我正在开发应用程序,本地客户端向服务器发送信息,服务器具有 GUI 字段,例如姓名、电子邮件和电话号码,代码显示文本已更新为文本字段,但未出现在 GUI 中。这是代码

public class evensoft extends javax.swing.JFrame {


        private static final int port=8008;
        private static ServerSocket serversocket;
        private static Socket clientsocket;
        private static InputStreamReader reader;
        private static BufferedReader breader;
        private static InputStreamReader InputStreamReader;
        private static String message;
        String msg;

        public evensoft() 
        {   
             initComponents();

        }


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



        public static void main(String args[]) 
        {



            java.awt.EventQueue.invokeLater(new Runnable()
            {
                public void run() {
                    new evensoft().setVisible(true);


                }
            });
            evensoft n1=new evensoft();
            try
            {
            serversocket=new ServerSocket(port,0,InetAddress.getLocalHost());
            label.setText("Server IP:  "+serversocket.getInetAddress()+  " port " +serversocket.getLocalPort());
            }
            catch(IOException e)
            {
                e.printStackTrace();
            }
            while(true)
            {
                try
                {
                    clientsocket=serversocket.accept();
                    reader=new InputStreamReader(clientsocket.getInputStream());  
                    breader=new BufferedReader(reader);
                    message=breader.readLine();
                    char a[]=message.toCharArray();
                    n1.formatmessage(a);



                }
                catch(IOException e1)
                {
                    e1.printStackTrace();
                }

            }


        }
         public void formatmessage(char[] a)
         {

                char[] Fname=new char[10];
                char[] lname=new char[50];
                char[] email=new char[50];
                char[] phone=new char[15];
               //Formatting meg string 

               String n1=new String(Fname);
               String n2=new String(lname);
               String n3=new String(email);
               String n6=new String(phone);

               System.out.println(n1);
               System.out.println(n2);
               System.out.println(n3);
               System.out.println(n5);
               System.out.println(n6);

               Name.setText(n1.trim()+" "+n2.trim());
               Email.setText(n3.trim());
               Contact.setText(n6.trim());


               System.out.println(Name.getText());
               System.out.println(Email.getText());
               System.out.println(Contact.getText());



         }
   public javax.swing.JTextField Contact;
    public javax.swing.JTextField Email;
    public javax.swing.JTextField Name;

 }
4

1 回答 1

2

您显示的表单与您正在更新的表单不同。

您创建要在此处显示的表单:

java.awt.EventQueue.invokeLater(new Runnable()
{
    public void run() {
        new evensoft().setVisible(true);
    }
});

以及稍后更新一行的表格

evensoft n1=new evensoft();

为了解决这个问题,您应该只实例化一个表单并在两个地方使用它:

public static void main(String args[]) {
    final evensoft f = new evensoft();

    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            f.setVisible(true);
        }
    });

    // ...

    while(true) {
        try {
            clientsocket=serversocket.accept();
            reader=new InputStreamReader(clientsocket.getInputStream());  
            breader=new BufferedReader(reader);
            message=breader.readLine();
            final char a[]=message.toCharArray();

            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    f.formatmessage(a);
                }
            });
        }
        // ...
    }
}
于 2013-09-30T13:35:52.240 回答