源代码如下,各位大虾麻烦你们给出原因:
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
import java.util.concurrent.*;
import java.awt.*;
public class MutilThreadServer extends JFrame{
   private JTextArea jta=new JTextArea();
   public MutilThreadServer(){
     add(new JScrollPane(jta));
     /*setTitle("MutilThreadServer");
     setSize(500,300);
     setLocationRelativeTo(null);
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     setVisible(true);*/
     ExecutorService executor=Executors.newCachedThreadPool();
     try{
         ServerSocket serverSocket=new ServerSocket(8000);
         jta.append("MutilThreadServer start at "+new Date()+'\n');
         int clientNo=1;
         while(true){
              Socket socket=serverSocket.accept();
              jta.append("Starting thread for client "+clientNo+"at "+new Date()+'\n');
              InetAddress address=socket.getInetAddress();
              jta.append("Client "+clientNo+"'s host name is "+address.getHostName()+'\n');
              jta.append("Client "+clientNo+"'s IP Address is"+address.getHostAddress()+'\n');
              executor.execute(new handleClient(socket));
              clientNo++;
         }
     }
     catch(IOException ex){
        System.err.println(ex);
     }
     finally{
        executor.shutdown();
     }
   }
   public static void main(String[] args){
     MutilThreadServer frame=new MutilThreadServer();
     frame.setTitle("MutilThreadServer");
     frame.setSize(500,300);
     frame.setLocationRelativeTo(null);
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     frame.setVisible(true);
   }
   private class handleClient implements Runnable{
     private Socket socket;
     DataInputStream in;
     DataOutputStream out;
     public handleClient(Socket socket){
       this.socket=socket;
     }
     public void run(){
       try{
           in=new DataInputStream(socket.getInputStream());
           out=new DataOutputStream(socket.getOutputStream());
           double radius=in.readDouble();
           double area=radius*radius*Math.PI;
           out.writeDouble(area);
           jta.append("Radius recevie from client: "+radius+'\n');
           jta.append("Area found: "+area+'\n');
       }
       catch(IOException ex){
           System.out.println(ex);
       }
    }
   }
}
如果把上面代码里面的注释语句的注释符号/*    */删除,然后将main函数里面的函数体中语句全部替换成new MutilThreadServer(),然后GUI界面却显示出来了,各位大虾告诉我这是怎么回事?

解决方案 »

  1.   

    try{...}catch..finally...
    => SwingUtilites.invokeLater(new Runnable(){public void run(){ try...catch...finally...}});
    可以显示出GUI,
    不过好像还是不正常
      

  2.   

    搞不清楚你要怎么样哇这样也可以用啊package test11;
    import java.io.*;
    import java.net.*;
    import java.util.*;
    import javax.swing.*;
    import java.util.concurrent.*;
    import java.awt.*;
    public class MutilThreadServer extends JFrame{
       private JTextArea jta=new JTextArea();
       public MutilThreadServer(){
         add(new JScrollPane(jta));
         setTitle("MutilThreadServer");
         setSize(500,300);
         setLocationRelativeTo(null);
         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         setVisible(true);
         ExecutorService executor=Executors.newCachedThreadPool();
         try{
             ServerSocket serverSocket=new ServerSocket(8000);
             jta.append("MutilThreadServer start at "+new Date()+'\n');
             int clientNo=1;
             while(true){
                  Socket socket=serverSocket.accept();
                  jta.append("Starting thread for client "+clientNo+"at "+new Date()+'\n');
                  InetAddress address=socket.getInetAddress();
                  jta.append("Client "+clientNo+"'s host name is "+address.getHostName()+'\n');
                  jta.append("Client "+clientNo+"'s IP Address is"+address.getHostAddress()+'\n');
                  executor.execute(new handleClient(socket));
                  clientNo++;
             }
         }
         catch(IOException ex){
            System.err.println(ex);
         }
         finally{
            executor.shutdown();
         }
       }
       public static void main(String[] args){
         MutilThreadServer frame=new MutilThreadServer();
    //     frame.setTitle("MutilThreadServer");
    //     frame.setSize(500,300);
    //     frame.setLocationRelativeTo(null);
    //     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //     frame.setVisible(true);
       }
       private class handleClient implements Runnable{
         private Socket socket;
         DataInputStream in;
         DataOutputStream out;
         public handleClient(Socket socket){
           this.socket=socket;
         }
         public void run(){
           try{
               in=new DataInputStream(socket.getInputStream());
               out=new DataOutputStream(socket.getOutputStream());
               double radius=in.readDouble();
               double area=radius*radius*Math.PI;
               out.writeDouble(area);
               jta.append("Radius recevie from client: "+radius+'\n');
               jta.append("Area found: "+area+'\n');
           }
           catch(IOException ex){
               System.out.println(ex);
           }
        }
       }
    }
      

  3.   

    frame.setVisible(true);
    问题出在这句话上  默认构造函数里面 创建的frame 是不可见的
    你先创建一个不可见的  后来设置属性可见 但是 界面不会因为你修改属性就刷新了
    所以设置可见属性要在构造函数里面
      

  4.   

    几处地方我给你修改了一下
    public MutilThreadServer() {
    super();
    setContentPane(new JScrollPane(jta));

    // add(new JScrollPane(jta));
    /*
     * setTitle("MutilThreadServer"); setSize(500,300);
     * setLocationRelativeTo(null); setDefaultCloseOperation
     * (JFrame.EXIT_ON_CLOSE); setVisible(true);
     */
    // while (true) {
    // Socket socket = serverSocket.accept();
    // jta.append("Starting thread for client " + clientNo + "at "
    // + new Date() + '\n');
    // InetAddress address = socket.getInetAddress();
    // jta.append("Client " + clientNo + "'s host name is "
    // + address.getHostName() + '\n');
    // jta.append("Client " + clientNo + "'s IP Address is"
    // + address.getHostAddress() + '\n');
    // executor.execute(new handleClient(socket));
    // clientNo++;
    // }

    你这个循环一直为真吧,都跳不出循环,走不到frame.setVisible(true); 这一句,你怎么看到frame啊?
      

  5.   

    一般在调用frame.setVisible(true);前加一句frame.pack();