从控制台到GUI获取应用程序

时间:2012-12-24 13:49:54

标签: java user-interface client-server

我正在学习用Java编写代码。我想和gui一起写简单的聊天。到目前为止,我的应用程序通过命令行。我有兴趣为客户部分建立gui。我无法联系到它。我的问题是我是否必须为gui编写特殊类,而不是在客户端类中构造这样的对象并对其进行操作?特别是我有一个问题,通过gui建立客户端和服务器之间的通信。我的命令行应用程序代码与客户端部分如下。我对此事的任何建议表示感谢。

public class Client {
    public static void main(String[] args) {
        try {
            Socket socket = new Socket("localhost", 4444);
            System.out.println("CLIENT: Server connected on port 4444");

            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            System.out.println("CLIENT: IN and OUT streams opened. Starting sending data...");

            ClientInputThread thread = new ClientInputThread(socket);
            thread.start();

            String serverResponse;
            while ((serverResponse = in.readLine()) != null) {
                System.out.println("Server: " + serverResponse);
                if (serverResponse.equals("koniec")) {
                    break;
                }
            } 
            System.out.println("CLIENT: Ending server connection. Closing client streams and socket.");
            out.close();
            in.close();
            socket.close();
            System.exit(0);
        } 
        catch (UnknownHostException e) {
            System.err.println("CLIENT: Trying to connect to unknown host: " + e);
            System.exit(1);
        } 
        catch (Exception e) {
            System.err.println("CLIENT: Exception:  " + e);
            System.exit(1);
        }
    }
}

public class ClientInputThread extends Thread {
    private PrintWriter out;

    public ClientInputThread(Socket clientSocket) {
        try {
            out = new PrintWriter(clientSocket.getOutputStream(), true);
        } 
        catch (IOException e) {
            e.printStackTrace();
            System.exit(1);
        }
    }

    public void run() {
        try {    
            BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
            String userInput="";    
            while (userInput != null) {
                userInput = console.readLine();
                out.println(userInput);
                out.flush();
                if (userInput.equals("koniec")) {
                    break;
                }
            }
            System.exit(0);
        } 
        catch (IOException e) {
            e.printStackTrace();
            System.exit(1);
        }
    }
}

1 个答案:

答案 0 :(得分:3)

通常的做法是尽可能地将逻辑与GUI分开。我创建了一个实现发送/接收消息部分的类(或许多类)(看起来你已经完成了)。

这些类应提供发送/接收消息的公共方法,并且可能能够注册侦听器并通知它们传入的消息。

然后编写GUI类,将其注册为服务器类的侦听器,并在MessageReceived事件发生后更新文本。可以找到事件处理基础知识herehere是创建和处理自定义事件的示例。

示例

//an interface that will let your server work with its listeners
interface MessageListener {
    public void messageSent();
}


class Server {
    List<MessageListener> listeners = new ArrayList<MessageListener>();

    //method to register listeners to be notified of incoming messages
    public void addListener(MessageListener toAdd) {
        listeners.add(toAdd);
    }

    public void sendMessage() {
        //code your logic here
        System.out.println("Message sent");

        // Notify everybody that may be interested.
        for (MessageListener hl : listeners)
            hl.messageSent();
    }

}

class GuiImplementation implements MessageListener {
    @Override
    public void messageSent() {
         System.out.println(message);
    }
}

和主要课程:

class Test {
    public static void main(String[] args) {
        Server server = new Initiater();
        GuiImplementation gui = new Responder();

        //register gui as a listener for incoming/outgoing messages
        server.addListener(gui);

        //this will trigger the gui method to process incoming message
        server.sendMessage(); 
    }
}