8
4-qadam. “New Класс Java” darchasining «Имя класса» maydoniga
“Client”, «Пакет» maydoniga “javafx” kiritilib «Готово» tugmasi bosiladi.
5-qadam. Yaratilgan javafx paketi ichidagi Client.java fayliga quyidagi Java
kodi kiritiladi.
package javafx;
import javafx.application.Application;
import javafx.event.*;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Text;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import java.io.*;
import java.net.*;
import static javafx.application.Application.launch;
public class Client extends Application {
@Override
public void start(Stage stage) {
Text text = new Text("Matnni kiriting");
TextField textField = new TextField();
Button button = new Button("Jo‘natish");
button.setOnAction(new EventHandler() {
@Override
public void handle(ActionEvent event) {
try (Socket clientSocket = new Socket("localhost", 7777)) {
DataOutputStream
outToServer
=
new
DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));
String a = textField.getText();
outToServer.writeBytes(a + '\n');
String b = inFromServer.readLine();
textField.setText(b); }
catch (IOException e){} } });
GridPane gridPane = new GridPane();
gridPane.setMinSize(400, 200);
gridPane.setVgap(5);
gridPane.setAlignment(Pos.CENTER);
gridPane.add(text, 0, 1);
gridPane.add(textField, 0, 2);
gridPane.add(button, 0, 3);