2016-04-23 209 views
3

我有一個簡單的程序,允許2個客戶端連接到服務器。Java onclick聽衆

連接後,他們可以輪流點擊空白卡片圖像。

一旦2位客戶端中的任何一位點擊空白卡片圖像,卡片圖像將更改爲Ace圖像俱樂部。

這些更改將顯示在客戶端的兩側,並且對於兩個客戶端都將禁用點擊。

2秒後,它變回空白卡圖像,客戶端可以再次點擊。


問題

我能夠顯示在雙方客戶在點擊后王牌俱樂部的形象,但是當圖像2秒後變回空白卡的形象,我不能再次點擊它。

這是我在運行中都試過()方法(更新)

public void run() { 

    while(true) { 
     try { 
      while (true) { 
       response = is.readLine(); 
       if(!response.equals("")) { 
        System.out.println(response); 
        responded = true; 
       } 
       break; 
      } 
     } catch (IOException e) { 
      System.err.println("IOException: " + e); 
     } 
     if(responded) { 
      timer = new Timer(1000,new TimerC()); 
      timer.start(); 
      responded = false; 
     } 
    } 
} 

這是我爲我的客戶全部代碼。請幫助

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 
import java.io.DataInputStream; 
import java.io.IOException; 
import java.io.ObjectInputStream; 
import java.io.PrintStream; 
import java.net.Socket; 
import java.net.UnknownHostException; 
import javax.swing.ImageIcon; 
import javax.swing.Timer; 

public class Client implements Runnable { 

    static Socket clientSocket = null; 
    ObjectInputStream in = null; 
    String serverMsg; 
    static PrintStream os = null; 
    static DataInputStream is = null; 
    static boolean closed = false; 
    static String s = ""; 
    static String cardType = ""; 
    static GameUI gameUI; 
    int countdown = 3; 
    Timer timer = new Timer(1000,null); 
    String response = null; 
    boolean responded = false; 

    public static void main(String args[]) { 
     try { 
      clientSocket = new Socket("localhost", 5550); 
      os = new PrintStream(clientSocket.getOutputStream()); 
      is = new DataInputStream(clientSocket.getInputStream()); 
     } catch (UnknownHostException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     gameUI = new GameUI(); 

     gameUI.cardOne.addMouseListener(new MouseAdapter() 
     { 
      public void mouseClicked(MouseEvent e) 
      { 
       s = gameUI.cardOne.getText(); 

      } 
     }); 

     if (clientSocket != null && os != null && is != null) { 
      try { 
       new Thread(new Client()).start(); 
       while (!closed) { 
        os.println(s.trim()); 
       } 

       os.close(); 
       is.close(); 
       clientSocket.close(); 
      } catch (IOException e) { 
       System.err.println("IOException: " + e); 
      } 
     } 

    } 

    public void run() { 
     try { 
      while (true && !responded) { 
       response = is.readLine(); 
       if(!response.equals("")) { 
        System.out.println(response); 
        responded = true; 
        break; 
       } 
      } 

     } catch (IOException e) { 
      System.err.println("IOException: " + e); 
     } 

     if(responded) { 
      timer = new Timer(1000,new TimerC()); 
      timer.start(); 
      responded = false; 
     } 
    } 

    class TimerC implements ActionListener { 
     @Override 
     public void actionPerformed(ActionEvent event) { 
      if(countdown == 0) { 
       timer.stop(); 
       gameUI.cardOne.setIcon(new ImageIcon(GameUI.revealCard("blank.png"))); 
       countdown = 3; 
      } 

      else { 
       gameUI.cardOne.setIcon(new ImageIcon(GameUI.revealCard(response))); 
       countdown--; 
       System.out.println(countdown); 

      } 
     } 
    } 
} 
+1

注意:'while(true &&!respond)'和'while(!respond)'是一樣的。 –

+0

@JonnyHenly注意 – user2935569

+0

你的'run'方法只執行while循環一次,你需要用另一個循環遍歷你的run方法中的所有內容,直到*遊戲結束*爲止。你也應該選擇在'respond = true'後刪除'break',或者把while改爲'while(true)'。 –

回答

0

肯定客戶端和服務器都使用緩衝流。這是套接字連接的本質。它不會逐字節地發送信息。它發送緩衝區。 您的邏輯使用行操作,它等待行尾字符。它可能在緩衝區的中間,所以你的程序會一直等到緩衝區滿了,它可能會有很多行。

爲了消除這個等待發送部分(在服務器和客戶端)應該使用flush method立即發送數據。

特別是增加在客戶端

while (!closed) { 
    os.println(s.trim()); 
    os.flush(); // <- send immediately right now 
} 

一行代碼,如果從服務器端你送點東西在服務器端類似。