问题描述
下面的代码显示了当前窗口上的按钮按下,但不会转移到客户端或服务器的另一个窗口。我不是在寻找有关如何实施规则的条件。但只是简单地将按钮文本转移到另一端(相同位置)的按钮文本。无论是客户端还是服务器。要运行程序,首先运行服务器应用程序,然后运行客户端应用程序。 LocalHost 只是“localhost”,端口是数字 8901。Name 是您选择的名称。任何帮助将不胜感激谢谢。
服务器应用程序:
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);
}
}
Server 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,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,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,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,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) {
}
}
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)