Java Reference
In-Depth Information
L
ISTING
31.2
Client.java
1
import
java.io.*;
2
import
java.net.*;
3
import
javafx.application.Application;
4
import
javafx.geometry.Insets;
5
import
javafx.geometry.Pos;
6
import
javafx.scene.Scene;
7
import
javafx.scene.control.Label;
8
import
javafx.scene.control.ScrollPane;
9
import
javafx.scene.control.TextArea;
10
import
javafx.scene.control.TextField;
11
import
javafx.scene.layout.BorderPane;
12
import
javafx.stage.Stage;
13
14
public class
Client
extends
Application {
15
// IO streams
16
DataOutputStream toServer =
null
;
17
DataInputStream fromServer =
null
;
18
19 @Override
// Override the start method in the Application class
20
public void
start(Stage primaryStage) {
21
// Panel p to hold the label and text field
22 BorderPane paneForTextField =
new
BorderPane();
23 paneForTextField.setPadding(
new
Insets(
5
,
5
,
5
,
5
));
24 paneForTextField.setStyle(
"-fx-border-color: green"
);
25 paneForTextField.setLeft(
new
Label(
"Enter a radius: "
));
26
27 TextField tf =
new
TextField();
28 tf.setAlignment(Pos.BOTTOM_RIGHT);
29 paneForTextField.setCenter(tf);
30
31 BorderPane mainPane =
new
BorderPane();
32
// Text area to display contents
33 TextArea ta =
new
TextArea();
34 mainPane.setCenter(
new
ScrollPane(ta));
35 mainPane.setTop(paneForTextField);
36
37
// Create a scene and place it in the stage
38 Scene scene =
new
Scene(mainPane,
450
,
200
);
39 primaryStage.setTitle(
"Client"
);
// Set the stage title
40 primaryStage.setScene(scene);
// Place the scene in the stage
41 primaryStage.show();
// Display the stage
42
43 tf.setOnAction(e -> {
44
create UI
handle action event
try
{
45
// Get the radius from the text field
46
double
radius = Double.parseDouble(tf.getText().trim());
read radius
47
48
// Send the radius to the server
49 toServer.writeDouble(radius);
50 toServer.flush();
51
52
write radius
// Get area from the server
53
double
area = fromServer.readDouble();
read area
54
55
// Display to the text area
56 ta.appendText(
"Radius is "
+ radius +
"\n"
);
57 ta.appendText(
"Area received from the server is "
58 + area +
'\n'
);
Search WWH ::
Custom Search