Я создал программу JavaFX, которая использует два основных класса: клиентский и серверный класс. Я создал класс Values, который используется для отправки значений туда и обратно. Однако, когда я использую objectinputstream на стороне клиента для чтения значений, в то время как код выполняется без ошибок, в текстовой области значения не отображаются. Кто-нибудь может помочь?
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import static javafx.application.Application.launch;
public class TASK4FINAL extends Application {
ObjectOutputStream ClientData;
ObjectInputStream ServerData;
public void start(Stage primaryStage) throws IOException {
primaryStage.setTitle("ALSET's Calculator");
Label label1 = new Label("Distance in Miles :");
Label label2 = new Label("Fuel Efficiency in MPG :");
Label label3 = new Label("Cost per litre :");
Label label4 = new Label("Calculated Values: ");
Label label5 = new Label("");
RadioButton rb1 = new RadioButton("98 Octane - Cost: £1.03");
RadioButton rb2 = new RadioButton("Diesel - Cost: £1.05");
ToggleGroup togglegroup = new ToggleGroup();
rb1.setToggleGroup(togglegroup);
rb2.setToggleGroup(togglegroup);
TextField text1 = new TextField();
TextField text2 = new TextField();
Button bt1 = new Button("Enter");
Button bt2 = new Button("Clear");
TextArea textarea = new TextArea();
textarea.setPrefRowCount(19);
textarea.setPrefColumnCount(19);
try{
Socket socket = new Socket("localhost", 5023);
ServerData = new ObjectInputStream(socket.getInputStream());
ClientData = new ObjectOutputStream(socket.getOutputStream());
} catch (Exception e) {
e.printStackTrace();
}
bt1.setOnAction(event -> {
try{
double num1 = Double.parseDouble(text1.getText());
double num2 = Double.parseDouble(text2.getText());
double rb_choice;
if (rb1.isSelected()){
rb_choice = 1;
}
if (rb2.isSelected()){
rb_choice = 2;
}
togglegroup.getUserData();
Values value = new Values(num1, num2, rb_choice, 0.1);
ClientData.writeObject(value);
Values server_object = (Values) ServerData.readObject();
textarea.setText("Trip Distance: " + text1.getText() + " miles" + "\n\nFuel Efficiency: " + text2.getText() + " MPG" + "\n\nTotal Cost: " + server_object.get_output() + "GBP");
ClientData.flush();
} catch (IOException e) {
e.printStackTrace();
} catch (NumberFormatException e) {
label5.setText("Error Message: ");
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
});
bt2.setOnAction(event -> {
text1.clear();
text2.clear();
togglegroup.selectToggle(null);
textarea.clear();
});
GridPane gridpane = new GridPane();
VBox vbox = new VBox();
gridpane.setMinSize(500, 400);
gridpane.setHgap(10);
gridpane.setVgap(10);
gridpane.add(label1,0,1);
gridpane.add(text1, 1, 1);
gridpane.add(label2, 0, 2);
gridpane.add(text2,1,2);
gridpane.add(label3,0,3);
gridpane.add(label4,1,3);
gridpane.add(rb1,0,4);
gridpane.add(rb2,0,5);
gridpane.add(bt1,0,6);
gridpane.add(bt2,1,6);
gridpane.add(textarea,1,4);
gridpane.setAlignment(Pos.CENTER);
gridpane.setPadding(new Insets(10, 10, 10, 10));
StackPane root = new StackPane();
root.getChildren().add(gridpane);
Scene scene = new Scene(root, 600, 600);
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
class Values implements Serializable {
double distance;
double efficiency;
double CPL;
double gallon_number;
double output;
Values(double distance, double efficiency, double CPL, double output){
this.distance = distance;
this.efficiency = efficiency;
this.CPL = CPL;
this.output = output;
}
public void set_distance(double distance){
this.distance = distance;
}
public void set_efficiency(double efficiency){
this.efficiency = efficiency;
}
public void set_CPL(double CPL){
this.CPL = CPL;
}
public void set_output(double output){
this.output = output;
}
public double get_distance(){
return this.distance;
}
public double get_efficiency(){
return this.efficiency;
}
public double get_CPL(){
return this.CPL;
}
public double get_output(){
this.distance = distance;
this.efficiency = efficiency;
this.gallon_number = distance/efficiency;
this.CPL = CPL;
this.output = CPL*gallon_number;
return this.output;
}
}
class Values_server{
double output;
Values_server(double output){
this.output = output;
}
public void set_output(double output){
this.output = output;
}
public double get_output(){
return this.output;
}
}
class Server {
public static void main(String[] args) throws ClassNotFoundException{
try {
System.out.println("Server Ready");
ServerSocket ss = new ServerSocket(5023);
System.out.println("Server is waiting for client request");
Socket socket = ss.accept();
System.out.println("Client connected: ");
ObjectInputStream ClientData = new ObjectInputStream(socket.getInputStream());
ObjectOutputStream ServerData = new ObjectOutputStream(socket.getOutputStream());
Values client_values = (Values) ClientData.readObject();
double output;
output = (client_values.get_CPL() * (client_values.get_distance()/client_values.get_efficiency()));
double CPL;
double efficiency;
double distance;
CPL = client_values.get_CPL();
efficiency = client_values.get_efficiency();
distance = client_values.get_distance();
Values server_values = new Values(distance, efficiency, CPL, output);
ServerData.writeObject(server_values);
while(true) {
double number = client_values.get_CPL();
double index_array;
if (number == 1){
index_array = 0;
}
else if (number == 2){
index_array = 1;
}
ArrayList<String> array = new ArrayList<String>();
try {
client_values.get_CPL();
FileWriter csvWriter = new FileWriter("output.csv");
csvWriter.append("Distance");
csvWriter.append(",");
csvWriter.append(Double.toString(client_values.get_distance()));
csvWriter.append(",\n");
csvWriter.append("Efficiency");
csvWriter.append(",");
csvWriter.append(Double.toString(client_values.get_efficiency()));
csvWriter.append(",\n");
csvWriter.append("CPL");
csvWriter.append(",");
csvWriter.append(Double.toString(client_values.get_CPL()));
csvWriter.append(",\n");
csvWriter.append("Output");
csvWriter.append(",");
csvWriter.append(Double.toString(output));
csvWriter.flush();
csvWriter.close();
} catch(IOException e) {
e.printStackTrace();
}
ss.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}