-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkmToMilesfx.java
More file actions
80 lines (66 loc) · 2.37 KB
/
kmToMilesfx.java
File metadata and controls
80 lines (66 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.geometry.Pos;
import javafx.geometry.Insets;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.Button;
import javafx.event.EventHandler;
import javafx.event.ActionEvent;
/** * Kilometer Converter application that uses an * anonymous inner class to create an event handler. */
public class kmToMilesfx extends Application
{
// Fields
private TextField kiloTextField;
private Label resultLabel;
public static void main(String[] args)
{
// Launch the application.
launch(args);
}
@Override
public void start(Stage primaryStage)
{
// Create a Label to display a prompt.
Label promptLabel = new Label("Enter a distance in kilometers:");
// Create a TextField for input.
kiloTextField = new TextField();
// Create a Button to perform the conversion.
Button calcButton = new Button("Convert");
// Create an event handler.
calcButton.setOnAction(new EventHandler<ActionEvent>()
{
@Override
public void handle(ActionEvent event)
{
// Get the kilometers.
Double kilometers = Double.parseDouble(kiloTextField.getText());
// Convert the kilometers to miles.
Double miles = kilometers * 0.6214;
// Display the results.
resultLabel.setText(String.format("%,.2f miles", miles));
}
});
// Create an empty Label to display the result.
resultLabel = new Label();
// Put the promptLabel and the kiloTextField in an HBox.
HBox hbox = new HBox(10, promptLabel, kiloTextField);
// Put the HBox, calcButton, and resultLabel in a VBox.
VBox vbox = new VBox(10, hbox, calcButton, resultLabel);
// Set the VBox's alignment to center.
vbox.setAlignment(Pos.CENTER);
// Set the VBox's padding to 10 pixels.
vbox.setPadding(new Insets(10));
// Create a Scene.
Scene scene = new Scene(vbox);
// Add the Scene to the Stage.
primaryStage.setScene(scene);
// Set the stage title.
primaryStage.setTitle("Kilometer Converter");
// Show the window.
primaryStage.show();
}
}