2016-04-23 129 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 
} 

一行代码,如果从服务器端你送点东西在服务器端类似。