Java 远程桌面管理

Java Remote Desktop Administration

我正在研究 Java 远程桌面管理。当我 运行 服务器启动器主 class 单独时,它工作正常。但是当我从按钮的动作事件中调用 class 时,框架只是冻结并显示黑屏..这是代码。有帮助吗?

import java.awt.BorderLayout;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JOptionPane;


public class ServerInitiator {
    //Main server frame
    private JFrame frame = new JFrame();
    //JDesktopPane represents the main container that will contain all
    //connected clients' screens
    private JDesktopPane desktop = new JDesktopPane();

    public static void main(String args[]){
        String port = JOptionPane.showInputDialog("Please enter listening port");
       new ServerInitiator().initialize(Integer.parseInt(port));
    }

    public void initialize(int port){

        try {
            ServerSocket sc = new ServerSocket(port);
            //Show Server GUI
            drawGUI();
            //Listen to server port and accept clients connections
            while(true){
                Socket client = sc.accept();
                System.out.println("New client Connected to the server");
                //Per each client create a ClientHandler
                new ClientHandler(client,desktop);
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    /*
     * Draws the main server GUI
     */
    public void drawGUI(){
            frame.add(desktop,BorderLayout.CENTER);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            //Show the frame in a maximized state
            frame.setExtendedState(frame.getExtendedState()|JFrame.MAXIMIZED_BOTH);
            frame.setVisible(true);
    }
}

您的 while 循环可能 运行 来自事件调度线程的上下文,阻止它处理任何新事件(包括重绘事件)

public void initialize(int port){

    try {
        ServerSocket sc = new ServerSocket(port);
        //Show Server GUI
        drawGUI();
        //Listen to server port and accept clients connections
        while(true){
            Socket client = sc.accept();
            System.out.println("New client Connected to the server");
            //Per each client create a ClientHandler
            new ClientHandler(client,desktop);
        }
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

有关详细信息,请参阅 Concurrency in Swing

相反,如果您希望能够更新 UI(简单且安全),您应该在不同的 Thread 中启动您的服务器或使用 SwingWorker。有关详细信息,请参阅 Worker Threads and SwingWorker