0

的方法serverinititaor()初始化(5555)在serverinitiator類是沒有得到所謂的在下面的代碼。方法不會被調用

此方法將啓動一個新框架客戶端上的端口5555像發送郵件或聊天宗旨一些功能。

public static void main(String[] args) { 

    new Thread(new Runnable() { 

     public void run() { 
      frame = new JFrame("client list"); 
     panel = new JPanel(); 
     JButton button = new JButton("Refresh Client list"); 
     button.addActionListener(new ActionListener(){ 
      public void actionPerformed(ActionEvent e) 
      { 
       if(success){ 
       JButton jb = new JButton("A new Client on port 5555"); 
       jb.addActionListener(new ActionListener() { 


        public void actionPerformed(ActionEvent e) { 

         new ServerInitiator().initialize(5555); 
        } 
       }); 
       panel.add(jb); 
       frame.revalidate(); 
       } 

    }).start(); 

}

ServerInitiator.java

synchronized public void initialize(int port){ 

    try {    
     final ServerSocket sc = new ServerSocket(port); 
     System.out.println(SwingUtilities.isEventDispatchThread());    
     drawGUI(); 


     new Thread(new Runnable(){ 
      public void run() 
      { 
       try { 
        Socket client = sc.accept(); 
       System.out.println("New client Connected to the server"); 

       new ClientHandler(client,desktop); 
       } catch (IOException ex) { 

       } 
      } 
     }).start(); 

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

請回復。

謝謝。

+1

其中'serverinititaor()。initialize(5555)'。 – Braj

+0

你甚至設法得到這個編譯? – ejk314

回答

1

我試圖申請是必要的,使其可編譯的最小的變化。結果是這個班。出於一些神祕的原因,我把這個類叫做ItDoesGetCalled

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

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 

public class ItDoesGetCalled 
{ 
    public static void main(String[] args) { 

     new Thread(new Runnable() { 

      public void run() { 
       final JFrame frame = new JFrame("client list"); 
       final JPanel panel = new JPanel(); 
       JButton button = new JButton("Refresh Client list"); 
       button.addActionListener(new ActionListener(){ 
        public void actionPerformed(ActionEvent e) 
        { 
         boolean success = true; 
         if(success){ 
          JButton jb = new JButton("A new Client on port 5555"); 
          jb.addActionListener(new ActionListener() { 


           public void actionPerformed(ActionEvent e) { 

            new ServerInitiator().initialize(5555); 
           } 
          }); 
          panel.add(jb); 
          frame.pack(); 
         } 
        }   
       }); 
       panel.add(button); 
       frame.getContentPane().add(panel); 
       frame.pack(); 
       frame.setVisible(true); 
      } 
     }).start(); 
    } 
} 

class ServerInitiator 
{ 
    synchronized public void initialize(int port) 
    { 
     System.out.println("Here we go..."); 
    }  
} 

不過,我強烈建議您清理它。什麼是所有線程的目的?目前,這樣的事情應該是足夠了:

import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 

public class ClientServerUI 
{ 
    public static void main(String[] args) 
    { 
     SwingUtilities.invokeLater(new Runnable() 
     { 
      @Override 
      public void run() 
      { 
       createAndShowGUI(); 
      } 
     }); 
    } 


    private static void createAndShowGUI() 
    { 
     JFrame f = new JFrame("Client list"); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     JPanel clientListPanel = new ClientListPanel(); 
     f.getContentPane().add(clientListPanel); 

     f.pack(); 
     f.setLocationRelativeTo(null); 
     f.setVisible(true); 
    } 
} 

class ClientListPanel extends JPanel 
{ 
    ClientListPanel() 
    { 
     setLayout(new GridLayout(0,1)); 
     JButton refreshButton = new JButton("Refresh client list"); 
     refreshButton.addActionListener(new ActionListener() 
     { 
      @Override 
      public void actionPerformed(ActionEvent e) 
      { 
       boolean success = true; 
       if(success) 
       { 
        createNewClientButton(); 
       } 
      }   
     }); 
     add(refreshButton); 
    } 

    private void createNewClientButton() 
    { 
     JButton newClientButton = new JButton("A new Client on port 5555"); 
     newClientButton.addActionListener(new ActionListener() 
     { 
      @Override 
      public void actionPerformed(ActionEvent e) { 

       new ServerInitiator().initialize(5555); 
      } 
     }); 
     add(newClientButton); 
     SwingUtilities.getWindowAncestor(this).pack(); 
    } 
} 

class ServerInitiator 
{ 
    synchronized public void initialize(int port) 
    { 
     System.out.println("Here we go..."); 
    }  
} 
1

你不應該分配Event Dispatch Thread(EDT)以外的UI。另一方面,關閉美國東部時間的網絡。請注意,Swing具有單線程繪製模型。用戶界面組件分配及其交互必須在EDT上完成。請參閱Concurrency in Swing瞭解更多詳情。

爲了獲得最佳性能,在美國東部時間所有的任務應該是短暫的。網絡應該在工作者線程上處理。幾個選項是:SwingWorkerExecutorService或您自己的輔助線程。 SwingWorker有一個內置的機制來推送EDT上的更新。在ExecutorService的情況下,您可以使用SwingUtilities.invokeLater用於此目的,與您自己的工作線程相同。