Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将鼠标位置从一台计算机传输到另一台计算机_Java_Sockets_Events_Tcp - Fatal编程技术网

Java 将鼠标位置从一台计算机传输到另一台计算机

Java 将鼠标位置从一台计算机传输到另一台计算机,java,sockets,events,tcp,Java,Sockets,Events,Tcp,我想在java上以VNC的形式编写一个应用程序 一开始我试着用流媒体显示我的鼠标位置,但我在学校里刚刚学了一些Java 我的代码: private Maus mouse; static int cX; static int cY; public TestMaus() { mouse = new Maus(); } public void main() throws Exception { String sentence; String modifiedS

我想在java上以VNC的形式编写一个应用程序

一开始我试着用流媒体显示我的鼠标位置,但我在学校里刚刚学了一些Java

我的代码:

private Maus mouse;

static int cX;
static int cY;

public TestMaus() {
    mouse = new Maus();
     }

public void main() throws Exception {

    String sentence;
    String modifiedSentence;
    BufferedReader inFromUser = new BufferedReader( new InputStreamReader(System.in));
    Socket clientSocket = new Socket("", 6789);

    while(true)
    {
        DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
        BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        sentence = String.valueOf(mouse.posX + "I" + mouse.posY);
        outToServer.writeBytes(sentence + '\n');
        modifiedSentence = inFromServer.readLine();
        System.out.println("FROM SERVER: " + modifiedSentence);
        clientSocket.close();
    }        
}

public int posX, posY;

public Maus(){     

        addMouseMotionListener(new MouseMotionAdapter() {
        public void mouseMoved(final MouseEvent e) {
            posX = e.getX();
            posY = e.getY();
        }
    });
}

您必须将鼠标运动侦听器添加到某个gui窗口,以便它接收鼠标事件。目前,您正试图将其添加到“Maus”实例中,该实例不是gui控件。您的问题标题和注释之间的关系不太清楚。此外,还有“while(true)”…这是一个无限循环。您的代码肯定会在某个时候崩溃……或者更糟的是,它根本不会崩溃,而只是失去响应。