JAVA类之间的双向通信

JAVA类之间的双向通信,java,swing,class,parameter-passing,this,Java,Swing,Class,Parameter Passing,This,我希望能够在GUI中创建客户机类的实例,然后将GUI.this作为客户机构造函数的参数传递给客户机构造函数,但我不确定具体如何做。我知道我错过了一些明显的东西!我希望能够从任何一个类调用另一个类上的方法。显然,我已经注释掉了代码,因为我不知道在不合并类的情况下如何或应该如何实现这种双向通信!我想能够做的事情,如通过发送一个命令,通过客户端从GUI类时,“向上”按钮是在GUI上点击等。感谢事先的帮助 客户端类: public class Client{ //static playe

我希望能够在GUI中创建客户机类的实例,然后将GUI.this作为客户机构造函数的参数传递给客户机构造函数,但我不确定具体如何做。我知道我错过了一些明显的东西!我希望能够从任何一个类调用另一个类上的方法。显然,我已经注释掉了代码,因为我不知道在不合并类的情况下如何或应该如何实现这种双向通信!我想能够做的事情,如通过发送一个命令,通过客户端从GUI类时,“向上”按钮是在GUI上点击等。感谢事先的帮助

客户端类:

    public class Client{
    //static playerGUI GUI;
    public static void main(String[] args) {
        //GUI = new playerGUI();
        //GUI.getFrame().setVisible(true);
        //Client cli = new Client("localhost",4444);
    }

    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){
                                //GUI.processgrid(response);
                                //send to GUI to process output!
                                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){
                        //inputbuttons!
                        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类:

public class playerGUI {
    private JFrame Frame = new JFrame();
    private JPanel displayPanel;
    private JTextPane hostTextPane;
    private JTextPane portTextPane;
    private playerGUI GUI;

    public static void main(String[] args) {

        playerGUI GUI = new playerGUI();
        GUI.Frame.setVisible(true);
        Client newclient = new Client(this.playerGUI);
    }

    /**
     * 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, 600);
        humanGameWindow.setVisible(false);

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

        JButton up = new JButton("Up");
        up.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // ######################
            }
        });
        up.setBackground(new Color(100, 149, 237));
        up.setBounds(274, 514, 100, 40);

        JButton down = new JButton("Down");
        down.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // ######################
            }
        });
        down.setBackground(new Color(100, 149, 237));
        down.setBounds(274, 555, 100, 40);

        JButton left = new JButton("Left");
        left.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // ######################
            }
        });
        left.setBackground(new Color(100, 149, 237));
        left.setBounds(173, 535, 100, 40);

        JButton right = new JButton("Right");
        right.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // ######################
            }
        });
        right.setBackground(new Color(100, 149, 237));
        right.setBounds(375, 535, 100, 40);

        JButton pickup = new JButton("Pickup");
        pickup.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // ######################
            }
        });
        pickup.setBackground(new Color(100, 149, 237));
        pickup.setBounds(40, 555, 100, 40);

        JButton Exit = new JButton("Exit");
        Exit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        Exit.setBackground(new Color(100, 149, 237));
        Exit.setBounds(427, 17, 70, 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));
        }

        humanGameWindow.add(gameTitle);
        humanGameWindow.add(up);
        humanGameWindow.add(down);
        humanGameWindow.add(left);
        humanGameWindow.add(right);
        humanGameWindow.add(pickup);
        humanGameWindow.add(Exit);
        humanGameWindow.add(displayPanel);

        final JPanel mainMenu = new JPanel();
        mainMenu.setLayout(null);
        mainMenu.setBackground(Color.LIGHT_GRAY);
        mainMenu.setBounds(0, 0, 500, 600);
        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);

        hostTextPane = new JTextPane();
        hostTextPane.setToolTipText("Enter the host name");
        hostTextPane.setBackground(new Color(192, 192, 192));
        hostTextPane.setBounds(50, 100, 234, 30);
        hostTextPane.setFont(new Font("Moire", Font.BOLD, 19));

        portTextPane = new JTextPane();
        portTextPane.setToolTipText("Enter the port");
        portTextPane.setBackground(new Color(192, 192, 192));
        portTextPane.setBounds(50, 175, 234, 30);
        portTextPane.setFont(new Font("Moire", Font.BOLD, 19));

        JButton playGameHuman = new JButton("Play Game Human");
        playGameHuman.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                mainMenu.setVisible(false);
                humanGameWindow.setVisible(true);
                                //not sure if i need to be creating the Client here?
                /*
                 * if (!(hostTextPane.getText().equals("")) &&
                 * !(portTextPane.getText().equals(""))) { try { Client cli =
                 * new Client(hostTextPane.getText(), Integer
                 * .parseInt(portTextPane.getText())); } catch (Exception e1) {
                 * System.out.println("Error."); } } else {
                 * JOptionPane.showMessageDialog(null,
                 * "Do not leave hostname or port no. blank."); }
                 */
            }
        });
        playGameHuman.setBackground(new Color(100, 149, 237));
        playGameHuman.setBounds(50, 345, 150, 55);

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

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

    }
}
公共类playerGUI{
私有JFrame=新JFrame();
专用JPanel显示面板;
私有JTextPane主机文本窗格;
私有JTextPane portTextPane;
私人玩家界面;
公共静态void main(字符串[]args){
playerGUI GUI=新的playerGUI();
GUI.Frame.setVisible(true);
Client newclient=newclient(this.playerGUI);
}
/**
*创建应用程序。
*/
公共播放器gui(){
Frame.getContentPane().setBackground(新颜色(255、255、255));
Frame.getContentPane().setLayout(null);
机架立根(100100500630);
Frame.setUndecorated(false);//删除菜单栏
Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// ############################################################################################################################################################################
最终JPanel humanGameWindow=新JPanel();
humanGameWindow.setLayout(空);
人形游戏窗口。背景(颜色。浅灰色);
humanGameWindow.setBounds(0,0500600);
humanGameWindow.setVisible(false);
JLabel gameTitle=新JLabel(“末日之城!!”);
设置前景(新颜色(100149237));
gameTitle.setFont(新字体(“Moire”,Font.BOLD,28));
游戏名称.挫折(92,5,380,50);
JButton up=新JButton(“up”);
addActionListener(新的ActionListener(){
已执行的公共无效操作(操作事件e){
// ######################
}
});
向上。挫折背景(新颜色(100149237));
上升.后退(274514100,40);
JButton down=新JButton(“down”);
down.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
// ######################
}
});
下降。倒退(新颜色(100149237));
下降。后退(27455510040);
JButton left=新JButton(“左”);
addActionListener(新ActionListener()){
已执行的公共无效操作(操作事件e){
// ######################
}
});
左。挫折背景(新颜色(100149237));
左图:后退(1735351000,40);
JButton right=新JButton(“right”);
right.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
// ######################
}
});
右。挫折背景(新颜色(100149237));
右.立根(375,535,100,40);
JButton拾取=新JButton(“拾取”);
pickup.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
// ######################
}
});
拾音.收音背景(新颜色(100149237));
皮卡.立根(40,555,100,40);
JButton Exit=新JButton(“Exit”);
Exit.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
系统出口(0);
}
});
退场退场(新颜色(100149237));
出口.后退(427,17,70,40);
displayPanel=newjpanel();
显示面板.立根(48,89,400,400);
设置布局(新的网格布局(5,5));
displayPanel.setPreferredSize(新尺寸((int)(400),(int)(400));
对于(int i=1;i<26;i++){
添加(新的JLabel(“标签”+i));
}
humanGameWindow.add(游戏标题);
humanGameWindow.add(向上);
humanGameWindow.add(向下);
humanGameWindow.add(左);
humanGameWindow.add(右);
人形游戏窗口。添加(拾取);
humanGameWindow.add(退出);
humanGameWindow.add(显示面板);
最终JPanel主菜单=新JPanel();
mainMenu.setLayout(空);
主菜单.背景(颜色.浅灰色);
主菜单.设置边界(0,0,500,600);
mainMenu.setVisible(真);
JLabel mainMenuTitle=新JLabel(“末日之城!!”);
设置前景(新颜色(100149237));
main menutitle.setFont(新字体(“莫尔”,Font.BOLD,28));
主立根(50,13380,50);
hostTextPane=新的JTextPane();
setToolTipText(“输入主机名”);
setBackground(新颜色(192192192));
hostTextPane.setBounds(50、100、234、30);
setFont(新字体(“莫尔”,Font.BOLD,19));
portTextPane=新的JTextPane();
setToolTipText(“输入端口”);
setBackground(新颜色(192192192));
portTextPane.setBounds(50175234,30);
setFont(新字体(“莫尔”,Font.BOLD,19));
JButton playGameHuman=新JButton(“Play Game Human”);
playGameHuman.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
main menu.setVisible(假);
humanGameWindow.setVisible(true);
//不确定是否需要在此处创建客户端?
/*
 public static void main(String[] args) {
        playerGUI GUI = new playerGUI();
        GUI.Frame.setVisible(true);
        Client newclient = new Client(GUI);
    }
Client newclient = new Client(this.playerGUI);
Client newclient = new Client(GUI);