簡體   English   中英

GUI和服務器客戶端之間的雙向通信

[英]Two way communication between a GUI and a Server Client

當我在GUI中按下按鈕時,我試圖發送某個字符串。 我的Client類當前正在運行,以繼續從命令行獲取字符串命令,並將其發送到服務器,在此將對其進行處理並返回響應。

現在如何通過GUI發送數據並將結果移回GUI?

例如,我有一個名為“ pickup”的按鈕,當單擊該按鈕時,它將通過Client類將字符串“ PICKUP”發送到服務器。

同樣,來自服務器的響應將是“ SUCCESS”或“ FAIL”,這將通過我的Client類中的線程“ serverResponse”打印出來,並且需要以某種方式將其發送到playerGUI類中的任意方法作為參數。

感謝您的幫助,對於未使用常規的類/方法/字段命名樣式感到抱歉!

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class playerGUI {
    private JFrame frame = new JFrame();
    private JPanel displayPanel;
    private JTextPane hostTextPane;
    private JTextPane portTextPane;
    private static Client newclient;

    public static void main(String[] args) {
        playerGUI GUI = new playerGUI();
        GUI.frame.setVisible(true);
        newclient = new Client(GUI);
    }

    /**
     * Create the application.
     */
    public playerGUI() {
        frame.getContentPane().setBackground(new Color(255, 255, 255));
        frame.getContentPane().setLayout(null);
        frame.setBounds(100, 100, 500, 630);
        frame.setUndecorated(false); // REMOVES MENU BAR
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        final JPanel humanGameWindow = new JPanel();
        humanGameWindow.setLayout(null);
        humanGameWindow.setBackground(Color.LIGHT_GRAY);
        humanGameWindow.setBounds(0, 0, 500, 630);
        humanGameWindow.setVisible(false);

        JButton pickup = new JButton("Pickup");
        pickup.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //I WANT TO SEND THE STRING "PICKUP" TO WHERE THE STARS ARE IN Client.class
            }
        });
        pickup.setBackground(new Color(100, 149, 237));
        pickup.setBounds(40, 555, 100, 40);

        displayPanel = new JPanel();
        displayPanel.setBounds(48, 89, 400, 400);
        displayPanel.setLayout(new GridLayout(5, 5));
        displayPanel.setPreferredSize(new Dimension((int) (400), (int) (400)));
        for (int i = 1; i < 26; i++) {
            displayPanel.add(new JLabel("Label " + i));
        }

        JButton Look = new JButton("Look");
        Look.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

            }
        });
        Look.setBackground(new Color(100, 149, 237));
        Look.setBounds(40, 514, 100, 40);

        humanGameWindow.add(Look);
        humanGameWindow.add(pickup);
        humanGameWindow.add(displayPanel);

        final JPanel mainMenu = new JPanel();
        mainMenu.setLayout(null);
        mainMenu.setBackground(Color.DARK_GRAY);
        mainMenu.setBounds(0, 0, 500, 630);
        mainMenu.setVisible(true);

        JLabel mainMenuTitle = new JLabel("DUNGEON OF DOOM!!");
        mainMenuTitle.setForeground(new Color(100, 149, 237));
        mainMenuTitle.setFont(new Font("Moire", Font.BOLD, 28));
        mainMenuTitle.setBounds(50, 13, 380, 50);

        JButton mainMenuQuit = new JButton("Quit");
        mainMenuQuit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        mainMenuQuit.setBackground(new Color(100, 149, 237));
        mainMenuQuit.setBounds(220, 345, 70, 55);

        JButton playGameHuman = new JButton("Play Game Human");
        playGameHuman.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                mainMenu.setVisible(false);
                humanGameWindow.setVisible(true);
            }
        });
        playGameHuman.setBackground(new Color(100, 149, 237));
        playGameHuman.setBounds(50, 345, 150, 55);

        mainMenu.add(mainMenuTitle);
        mainMenu.add(mainMenuQuit);
        mainMenu.add(playGameHuman);

        frame.getContentPane().add(humanGameWindow);
        frame.getContentPane().add(mainMenu);
        frame.setVisible(true);

    }
}

這是Client類,線程是我想要將響應發送到GUI類以處理和顯示特定輸出的地方。 星號是我要從GUI類中的按鈕發送文本的地方(還有其他按鈕,我已刪除了代碼,以方便閱讀!)。

import java.net.*;
import java.io.*;

public class Client{
    public Client(playerGUI GUI){   
        try{
            final Socket sock = new Socket("localhost",4444);
            final DataInputStream in = new DataInputStream(sock.getInputStream());
            final PrintStream out = new PrintStream(sock.getOutputStream());
            DataInputStream inputLine = new DataInputStream(new BufferedInputStream(System.in));

            final Thread serverResponse = new Thread(){
                public void run(){
                    System.out.println("DUNGEON OF DOOM HAS STARTED");
                    if(sock != null){
                        if(in != null){
                            try{
                                String response;
                                while((response = in.readLine()) != null){
                                    //I WANT TO SEND "response" TO THE GUI CLASS
                                    System.out.println(response);
                                }
                            }catch(UnknownHostException uhe){
                                System.err.println("Unknown host1: " + uhe);
                            }catch(IOException ioe){
                                System.err.println("IOException1: " + ioe);
                            }catch(NullPointerException npe){
                                System.err.println("Null Pointer1: " + npe);
                            }
                        }
                    }
                }
            };
            serverResponse.start();
            if(sock != null){
                if(out != null){
                    try{
                        while(true){
                            String sending = *************************
                            //String sending = inputLine.readLine(); 
                            out.println(sending);
                            if(sending.equals("QUIT")) break;
                        }
                    }catch(UnknownHostException uhe2){
                        System.err.println("Unknown host2: " + uhe2);
                    }catch(IOException ioe2){
                        System.err.println("IOException2: " + ioe2);
                    }catch(NullPointerException npe2){
                        System.err.println("Null Pointer2: " + npe2);
                    }
                }
            }
            out.close();
            in.close();
            sock.close();
        }catch(UnknownHostException uhe3){
            System.err.println("Unknown host3: " + uhe3);
        }catch(IOException ioe3){
            System.err.println("IOException3: " + ioe3);
        }catch(NullPointerException npe3){
            System.err.println("Null Pointer3: " + npe3);
        }   
    }
}

您可能應該閱讀該內容,然后提出問題: 教程

要將數據從GUI傳遞到客戶端線程,請使用LinkedBlockingQueue 要將響應發送到GUI,請使用SwingUtilities.invokeLater

您正在混合服務器和客戶端代碼,如下所示。 客戶端-服務器應用程序無法以這種方式工作。

public static void main(String[] args) {
    playerGUI GUI = new playerGUI();
    GUI.frame.setVisible(true);
    newclient = new Client(GUI);
}

最后,這兩個類都將駐留在單獨計算機上的單獨JVM中。

它可能在單台機器上工作,但是如果服務器在一個系統上運行並且多個客戶端試圖從不同的系統進行通信,將會發生什么?

我已經發布了一些示例,請查看客戶端-服務器通信 我已經一步一步地描述了它,只是按照線程來查找其他樣本。

如果您還有任何問題,請告訴我!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM