java—将相同的按钮文本从客户端传递到服务器按钮文本的相同位置(或将服务器按钮文本传递到客户端按钮文本)

f2uvfpb9  于 2021-06-27  发布在  Java
关注(0)|答案(0)|浏览(193)

下面的代码显示在当前窗口上按下按钮,但不会转移到另一个窗口,即客户机或服务器。我不是在寻找如何执行规则的条件。但只需将按钮文本传输到另一端的按钮文本(相同位置)。无论这是客户端还是服务器。要运行程序,首先运行服务器应用程序,然后运行客户端应用程序。localhost只是“localhost”,端口号是8901。名字是你选择的名字。任何帮助都将不胜感激。
服务器应用程序:

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Optional;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.control.TextInputDialog;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class ChatServer extends Application
{ 
    // declare and initialise the text display area
    private TextArea textWindow = new TextArea(); private OutputStream outStream; // for low level output
    private DataOutputStream outDataStream; // for high level output
 private ListenerTask listener; // required for the server thread

    private final int port = 8901;
    private String name;
    private recordButtonPressedIndex obj = new recordButtonPressedIndex();
    Button[][] buttonDP = new Button[3][3];
    @Override    
    public void start(Stage stage)
    {  

        startServerThread();  

        HBox root1 = new HBox(5);
        VBox box = new VBox(5);

        for (int j = 1; j <= 3; j++) 
        {
            Button b = new Button();
            b.setText("");
            b.setMinWidth(50);
            b.setMaxWidth(50);
            root1.getChildren().add(b);
            root1.setAlignment(Pos.CENTER);
            buttonDP[0][j-1] = b;

        }

        HBox root21 = new HBox(5);
        VBox box2 = new VBox(5);

        for (int j = 1; j <= 3; j++) 
        {

            Button b = new Button();
            b.setText("");
            b.setMinWidth(50);
            b.setMaxWidth(50);
            root21.getChildren().add(b);
            root21.setAlignment(Pos.CENTER);
            buttonDP[1][j-1] = b;

        }

        HBox root31 = new HBox(5);

        for (int j = 1; j <= 3; j++) 
        {

            Button b = new Button();
            b.setText("");
            b.setMinWidth(50);
            b.setMaxWidth(50);
            root31.getChildren().add(b);
            root31.setAlignment(Pos.CENTER);
            buttonDP[2][j-1] = b;

        }

        Socket connection; // declare a "general" socket
        ServerSocket listenSocket; // declare a server socket

            box.getChildren().addAll(root1,root21,root31);
            box.setAlignment(Pos.CENTER);

            buttonDP[0][0].setOnAction(e->
                  { 
                    buttonDP[0][0].setText("O");
                    try
                    {
                      outDataStream.writeInt(0); outDataStream.writeInt(0);
                    }
                    catch(IOException ie)
                    {
                    }
                });
            buttonDP[0][1].setOnAction(e->
                  { 
                    buttonDP[0][1].setText("O");
                    try
                    {
                      outDataStream.writeInt(0); outDataStream.writeInt(1);
                    }
                    catch(IOException ie)
                    {
                    }
                });
            buttonDP[0][2].setOnAction(e->
                  { 
                    buttonDP[0][2].setText("O");
                    try
                    {
                      outDataStream.writeInt(0); outDataStream.writeInt(2);
                    }
                    catch(IOException ie)
                    {
                    }
                });
            buttonDP[1][0].setOnAction(e->
                  { 
                    buttonDP[1][0].setText("O");
                    try
                    {
                      outDataStream.writeInt(1); outDataStream.writeInt(0);
                    }
                    catch(IOException ie)
                    {
                    }
                });
            buttonDP[1][1].setOnAction(e->
                  { 
                    buttonDP[1][1].setText("O");
                    try
                    {
                      outDataStream.writeInt(1); outDataStream.writeInt(1);
                    }
                    catch(IOException ie)
                    {
                    }
                });
            buttonDP[1][2].setOnAction(e->
                  { 
                    buttonDP[1][2].setText("O");
                    try
                    {
                      outDataStream.writeInt(1); outDataStream.writeInt(2);
                    }
                    catch(IOException ie)
                    {
                    }
                });
            buttonDP[2][0].setOnAction(e->
                  { 
                    buttonDP[2][0].setText("O");
                    try
                    {
                      outDataStream.writeInt(2); outDataStream.writeInt(0);
                    }
                    catch(IOException ie)
                    {
                    }
                });
            buttonDP[2][1].setOnAction(e->
                  { 
                    buttonDP[2][1].setText("O");
                    try
                    {
                      outDataStream.writeInt(2); outDataStream.writeInt(1);
                    }
                    catch(IOException ie)
                    {
                    }
                });
            buttonDP[2][2].setOnAction(e->
                  { 
                    buttonDP[2][2].setText("O");
                    try
                    {
                      outDataStream.writeInt(2); outDataStream.writeInt(2);
                    }
                    catch(IOException ie)
                    {
                    }
                });

        Scene scene = new Scene(box, 1000, 450);

        stage.setTitle("Noughts and crosses Server");
        stage.setScene(scene);
        stage.show();
   }

   private void startServerThread()  
   {
        Socket connection; // declare a "general" socket
        ServerSocket listenSocket; // declare a server socket

        try  
        {
            // create a server socket
       listenSocket = new ServerSocket(port);       

            // listen for a connection from the client
           connection = listenSocket.accept();             

            // create an output stream to the connection   
       outStream = connection.getOutputStream ();
       outDataStream = new DataOutputStream(outStream);

            // create a thread to listen for messages
           listener = new ListenerTask(obj,buttonDP, connection);

           Thread thread = new Thread(listener);
           thread.start(); // start the thread  
       }  
        catch (IOException e)  
        {        
            textWindow.setText("An error has occured");           
        }        
   }  
   @Override   
   public void stop()
   {
       System.exit(0); // terminate application when the window is closed
   }

   public static void main(String[] args)
   {
       launch(args);
   }
}

服务器listenertask类:

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.concurrent.Task;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;

public class ListenerTask extends Task
{   
    private InputStream inputStream; // for low level input
    private DataInputStream dataInputStream; // for high level input
    private Button button[][]; // a reference to the text area where the message will be displayed
    private Socket connection; // a reference to the connection
    private recordButtonPressedIndex obj;
    private OutputStream outStream;
    private DataOutputStream outDataStream;
    // constructor receives references to the text area and the connection
    public ListenerTask(recordButtonPressedIndex objIn,Button[][]buttonIn, Socket connectionIn)
    {
    button = buttonIn;
    connection = connectionIn;
    obj=objIn;
    try
    {
            // create an input stream from the remote machine
            inputStream = connection.getInputStream(); 
            dataInputStream = new DataInputStream(inputStream);
            outStream = connection.getOutputStream();
        outDataStream = new DataOutputStream (outStream);  
    }

        catch(IOException e)
    {
    }
    }

    @Override
    public Void call()
    { 
    int in0,in1;
    while(true)
    {

        try
        {
                Thread.sleep(1000);
        in0 = dataInputStream.readInt(); // read the incoming message
                in1 = dataInputStream.readInt();
            button[in0][in1].setText("O"); // display the message

            }

            catch(IOException e)
            {
            } 
            catch (InterruptedException ex) {

            }
        }    
    }   
}

客户端应用程序:

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Optional;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextInputDialog;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class ChatClient extends Application
{
    // declare and initialize the text display area
    private TextArea textWindow = new TextArea();

    private OutputStream outStream; // for low level output
    private DataOutputStream outDataStream; // for high level output

    private ListenerTask listener; // required for the cleint thread

    private int port; // to hold the port number of the server
    private  String remoteMachine; // to hold the name chosen by the user

    private String name;
    private Button[][] buttonDP = new Button[3][3];
    private recordButtonPressedIndex obj = new recordButtonPressedIndex();

    private    InputStream inStream;
    private    DataInputStream inDataStream;

    @Override
    public void start(Stage stage)   
    {

        getInfo(); // call method that gets user name and server details
        startClientThread(); // start the client thread

        HBox root1 = new HBox(5);
        VBox box = new VBox(5);

        for (int j = 1; j <= 3; j++) 
        {
            Button b = new Button();
            b.setText("");
            b.setMinWidth(50);
            b.setMaxWidth(50);
            root1.getChildren().add(b);
            root1.setAlignment(Pos.CENTER);
            buttonDP[0][j-1] = b;

        }

        HBox root21 = new HBox(5);
        VBox box2 = new VBox(5);

        for (int j = 1; j <= 3; j++) 
        {

            Button b = new Button();
            b.setText("");
            b.setMinWidth(50);
            b.setMaxWidth(50);
            root21.getChildren().add(b);
            root21.setAlignment(Pos.CENTER);
            buttonDP[1][j-1] = b;

        }
        HBox root31 = new HBox(5);
        for (int j = 1; j <= 3; j++) 
        {

            Button b = new Button();
            b.setText("");
            b.setMinWidth(50);
            b.setMaxWidth(50);
            root31.getChildren().add(b);
            root31.setAlignment(Pos.CENTER);
            buttonDP[2][j-1] = b;

        }
        box.getChildren().addAll(root1,root21,root31);
        box.setAlignment(Pos.CENTER);
        buttonDP[0][0].setOnAction(e->
                  { 
                    buttonDP[0][0].setText("X");
                    try
                    {
                      outDataStream.writeInt(0); outDataStream.writeInt(0);
                    }
                    catch(IOException ie)
                    {
                    }
                });
            buttonDP[0][1].setOnAction(e->
                  { 
                    buttonDP[0][1].setText("X");
                    try
                    {
                      outDataStream.writeInt(0); outDataStream.writeInt(1);
                    }
                    catch(IOException ie)
                    {
                    }
                });
            buttonDP[0][2].setOnAction(e->
                  { 
                    buttonDP[0][2].setText("X");
                    try
                    {
                      outDataStream.writeInt(0); outDataStream.writeInt(2);
                    }
                    catch(IOException ie)
                    {
                    }
                });
            buttonDP[1][0].setOnAction(e->
                  { 
                    buttonDP[1][0].setText("X");
                    try
                    {
                      outDataStream.writeInt(1); outDataStream.writeInt(0);
                    }
                    catch(IOException ie)
                    {
                    }
                });
            buttonDP[1][1].setOnAction(e->
                  { 
                    buttonDP[1][1].setText("X");
                    try
                    {
                      outDataStream.writeInt(1); outDataStream.writeInt(1);
                    }
                    catch(IOException ie)
                    {
                    }
                });
            buttonDP[1][2].setOnAction(e->
                  { 
                    buttonDP[1][2].setText("X");
                    try
                    {
                      outDataStream.writeInt(1); outDataStream.writeInt(2);
                    }
                    catch(IOException ie)
                    {
                    }
                });
            buttonDP[2][0].setOnAction(e->
                  { 
                    buttonDP[2][0].setText("X");
                    try
                    {
                      outDataStream.writeInt(2); outDataStream.writeInt(0);
                    }
                    catch(IOException ie)
                    {
                    }
                });
            buttonDP[2][1].setOnAction(e->
                  { 
                    buttonDP[2][1].setText("X");
                    try
                    {
                      outDataStream.writeInt(2); outDataStream.writeInt(1);
                    }
                    catch(IOException ie)
                    {
                    }
                });
            buttonDP[2][2].setOnAction(e->
                  { 
                    buttonDP[2][2].setText("X");
                    try
                    {
                      outDataStream.writeInt(2); outDataStream.writeInt(2);

                    }
                    catch(IOException ie)
                    {
                    }
                });

        Scene scene = new Scene(box, 1000, 450);

        stage.setTitle("Noughts and crosses client");
        stage.setScene(scene);
        stage.show();

    }

    private void startClientThread()  
    {   
        Socket connection;  // declare a "general" socket

        try
        {
            // create a connection to the server
            connection = new Socket (remoteMachine, port); 
            // create output stream to the connection   
        outStream = connection.getOutputStream();
        outDataStream = new DataOutputStream (outStream);       

            listener = new ListenerTask(obj,buttonDP,connection);
            Thread thread = new Thread(listener);
            thread.start(); // start the thread
    }  

        catch(UnknownHostException e)
        {   
            textWindow.setText("Unknown host");
        } 

        catch (IOException e)  
        {
        textWindow.setText("An error has occured");
    }  
  }  

// method to get information from user 
private void getInfo()
{
    Optional<String> response;

    // get address of host 
    TextInputDialog textDialog1 = new TextInputDialog();
    textDialog1.setHeaderText("Enter remote host");
    textDialog1.setTitle("Chat Client");
    response = textDialog1.showAndWait();
    remoteMachine = response.get(); 

    // get port number            
    TextInputDialog textDialog2 = new TextInputDialog();
    textDialog2.setHeaderText("Enter port number");
    textDialog2.setTitle("Chat Client");
    response = textDialog2.showAndWait(); 
    port = Integer.valueOf(response.get());

    // get user name
    TextInputDialog textDialog3 = new TextInputDialog();
    textDialog3.setHeaderText("Enter user name");
    textDialog3.setTitle("Chat Client");
    response = textDialog3.showAndWait();
    name =  response.get();

}
  @Override
  public void stop()
  {
      System.exit(0); // terminate application when the window is closed
  }

    public static void main(String[] args)
    {
        launch(args);
    }
}

客户端listenertask类:

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import javafx.concurrent.Task;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;

public class ListenerTask extends Task
{   
    private InputStream inputStream; // for low level input
    private DataInputStream dataInputStream; // for high level input
    private Button button[][]; // a reference to the text area where the message will be displayed
    private Socket connection; // a reference to the connection
    private recordButtonPressedIndex obj;
    private OutputStream outStream;
    private DataOutputStream outDataStream;
    // constructor receives references to the text area and the connection
    public ListenerTask(recordButtonPressedIndex objIn,Button[][]buttonIn, Socket connectionIn)
    {
    button = buttonIn;
    connection = connectionIn;
    obj=objIn;
    try
    {
            // create an input stream from the remote machine
            inputStream = connection.getInputStream(); 
            dataInputStream = new DataInputStream(inputStream);
            outStream = connection.getOutputStream();
        outDataStream = new DataOutputStream (outStream);  
    }

        catch(IOException e)
    {
    }
    }

    @Override
    public Void call()
    { 
    int in0,in1;
    while(true)
    {
        try
        {
        in0 = dataInputStream.readInt(); // read the incoming message
                in1 = dataInputStream.readInt();
            button[in0][in1].setText("X"); // display the message
                Thread.sleep(1000);

            }

            catch(IOException e)
            {
            }
            catch (InterruptedException ex) {

            }
        }    
    }   
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题