JTextfield未在GUI中动态更新

时间:2013-09-30 13:30:27

标签: java swing concurrency serversocket event-dispatch-thread

我正在开发本地客户端向服务器发送信息的应用程序i,并且服务器具有GUI字段,例如名称,电子邮件和电话号码,代码显示文本已更新为textfield,但它没有出现在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;

 }

1 个答案:

答案 0 :(得分: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);
                }
            });
        }
        // ...
    }
}