Java onclick侦听器

Java onclick侦听器,java,onclick,Java,Onclick,我有一个简单的程序,允许两个客户端连接到一个服务器 一旦连接,他们可以轮流点击空白卡片图像。 一旦两个客户中的任何一个点击空白卡片图像,卡片图像将变为Ace图像俱乐部 更改将显示在客户端的两侧,单击将对两个客户端禁用 2秒钟后,它会变回空白卡图像,客户端可以再次单击 问题 在客户点击后,我可以在两侧显示Ace俱乐部图像,但当图像在2秒后变回空白卡图像时,我无法再点击它 这是我在run()方法中尝试过的(已更新) 这是我客户的完整代码。请帮忙 import java.awt.event.Acti

我有一个简单的程序,允许两个客户端连接到一个服务器

一旦连接,他们可以轮流点击空白卡片图像。 一旦两个客户中的任何一个点击空白卡片图像,卡片图像将变为Ace图像俱乐部

更改将显示在客户端的两侧,单击将对两个客户端禁用

2秒钟后,它会变回空白卡图像,客户端可以再次单击


问题

在客户点击后,我可以在两侧显示Ace俱乐部图像,但当图像在2秒后变回空白卡图像时,我无法再点击它

这是我在run()方法中尝试过的(已更新)

这是我客户的完整代码。请帮忙

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);

            }  
        }
    }
}

确保客户端和服务器都使用缓冲流。这就是套接字连接的本质。它不会逐字节发送信息。它发送缓冲区。 您的逻辑使用行操作,它等待行结束字符。它可能在缓冲区的中间,所以你的程序会等到缓冲区满了,它可以是很多行。 要消除此等待发送部分(在服务器和客户端上),应使用立即发送数据

特别是在客户端添加一行代码

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

os.flush();//注意:
while(true&&!responsed)
while(!responsed)相同
。你的
run
方法只执行一次while循环,你需要用另一个while循环来包围你的run方法中的所有内容,该循环将迭代到游戏结束。你还应该选择在
responsed=true
之后删除该
中断
,或者将while更改为
while(true)
@JonnyHenly我试着按照你的建议用另一个while循环来包围一切,但一旦我单击,它就会陷入一个无休止的循环。但是你能再次单击卡片吗?你是用
运行
方法还是只使用
while
循环来包围一切?让我们来看看。
 while (!closed) {
   os.println(s.trim());
   os.flush(); // <- send immediately right now
 }