Witam!
Piszę sobie chat, ale niestety nie wstaje wgl GUI w JavieFX, zrobiłem je przez SceneBuildera.
Serwer:
Server.java
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Server extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("server.fxml"));
primaryStage.setTitle("Chat - Serwer");
primaryStage.setScene(new Scene(root, 600, 400));
primaryStage.setResizable(false);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
ServerController.java
package sample;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerController {
@FXML
Label conversationLabel;
@FXML
TextField messageTextField;
@FXML
Button sendButton;
//socket config
private ServerSocket serverSocket = null;
private Socket clientSocket = null;
private int port = 4447;
//input & output
private DataInputStream inputStream;
private DataOutputStream outputStream;
public void initialize() throws IOException {
serverSocket = new ServerSocket(port);
clientSocket = serverSocket.accept();
inputStream = new DataInputStream(clientSocket.getInputStream());
outputStream = new DataOutputStream(clientSocket.getOutputStream());
String messageIn="";
while (true){
messageIn=inputStream.readUTF();
System.out.println("Klient: " + messageIn);
}
}
public void onAction(){
String messageOut=messageTextField.getText();
try {
outputStream.writeUTF(messageOut);
outputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
messageTextField.setText("");
System.out.println("Serwer: "+ messageOut);
}
}
Server.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.ServerController">
<children>
<Label fx:id="conversationLabel" alignment="TOP_CENTER" prefHeight="300.0" prefWidth="601.0" style="-fx-background-color: white;" />
<HBox prefHeight="100.0" prefWidth="200.0">
<children>
<TextField fx:id="messageTextField" prefHeight="26.0" prefWidth="540.0" promptText="Napisz coś!">
<opaqueInsets>
<Insets />
</opaqueInsets></TextField>
<Button fx:id="sendButton" mnemonicParsing="false" onAction="#onAction" text="Wyślij" />
</children>
</HBox>
<Button mnemonicParsing="false" text="Button" />
</children>
</VBox>
Client.java
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Client extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("client.fxml"));
primaryStage.setTitle("Chat - Klient");
primaryStage.setScene(new Scene(root, 600, 400));
primaryStage.setResizable(false);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
ClientController.java
package sample;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import java.io.*;
import java.net.Socket;
public class ClientController {
@FXML
Label conversationLabel;
@FXML
TextField messageTextField;
@FXML
Button sendButton;
//socket config
private int port = 4447;
private String hostName="127.0.0.1";
private Socket serverSocket = null;
//input & ouput
private DataInputStream inputStream;
private DataOutputStream outputStream;
public void initialize() throws IOException {
serverSocket = new Socket(hostName, port);
inputStream = new DataInputStream(serverSocket.getInputStream());
outputStream = new DataOutputStream(serverSocket.getOutputStream());
String messageIn="";
while (true){
messageIn=inputStream.readUTF();
System.out.println("Serwer: " + messageIn);
}
}
public void onAction(){
String messageOut=messageTextField.getText();
try {
outputStream.writeUTF(messageOut);
outputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
messageTextField.setText("");
System.out.println("Serwer: "+ messageOut);
}
}
Client.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.ClientController">
<children>
<Label fx:id="conversationLabel" alignment="TOP_CENTER" prefHeight="300.0" prefWidth="601.0" style="-fx-background-color: white;" />
<HBox prefHeight="100.0" prefWidth="200.0">
<children>
<TextField fx:id="messageTextField" prefHeight="26.0" prefWidth="540.0" promptText="Napisz coś!">
<opaqueInsets>
<Insets />
</opaqueInsets></TextField>
<Button fx:id="sendButton" mnemonicParsing="false" onAction="#onAction" text="Wyślij" />
</children>
</HBox>
</children>
</VBox>
Proces startuje, nie ma żadnych komunikatów. Możecie podpowiedzieć co tutaj jest źle?