public class AdderApplication extends Application
2: import javafx.application.Application;
3: import javafx.application.Platform;
4: import javafx.event.ActionEvent;
5: import javafx.geometry.Insets;
6: import javafx.geometry.Pos;
7: import javafx.scene.Scene;
8: import javafx.scene.control.Button;
9: import javafx.scene.control.Label;
10: import javafx.scene.control.TextField;
11: import javafx.scene.layout.GridPane;
12: import javafx.scene.text.Font;
13: import javafx.stage.Stage;
15: /**
16: * A JavaFX Application that allows the user to enter two numbers and find their
17: * sum. (OK, it's not very impressive, but we need to start somewhere!)
18: *
19: * @author Mark Young (A00000000)
20: */
21: public class AdderApplication extends Application {
23: /**
24: * The start method is where we build and activate the GUI. THIS is the
25: * method that needs to be revised.
26: *
27: * @param primaryStage the main (and only) window for this application
28: */
29: @Override
30: public void start(Stage primaryStage) {
31: // create the controls
32: Label num1Label = makeLabel("First Number:");
33: Label num2Label = makeLabel("Second Number:");
34: Label resultLabel = makeLabel("Result:");
35: Label instructions = makeLabel("Enter two numbers to add up:");
36: TextField num1Field = makeTextField("0");
37: TextField num2Field = makeTextField("0");
38: TextField resultField = makeTextField("0");
39: resultField.setEditable(false);
40: Button addButton = makeButton("Add");
41: Button doneButton = makeButton("Done");
43: // activate the buttons
44: doneButton.setOnAction((ActionEvent event) -> {
45: Platform.exit();
46: });
47: addButton.setOnAction((ActionEvent e) -> {
48: addFields(num1Field, num2Field, resultField);
49: });
51: // create and add spacing to the Grid
52: GridPane root = new GridPane();
53: root.setPadding(new Insets(15, 25, 15, 25));
54: root.setHgap(15);
55: root.setVgap(10);
57: // add the controls to the grid
58: root.add(instructions, 0, 0, 2, 1);
59: root.add(num1Label, 0, 1);
60: root.add(num1Field, 1, 1);
61: root.add(num2Label, 0, 2);
62: root.add(num2Field, 1, 2);
63: root.add(resultLabel, 0, 3);
64: root.add(resultField, 1, 3);
65: root.add(addButton, 0, 4);
66: root.add(doneButton, 1, 4);
68: // create the scene
69: Scene scene = new Scene(root);
71: // set and show the stage
72: primaryStage.setTitle("Adder Window");
73: primaryStage.setScene(scene);
74: primaryStage.show();
75: }
77: // IGNORE THIS METHOD
78: // THIS IS NOT THE METHOD YOU'RE LOOKING FOR
79: public static void main(String[] args) {
80: launch(args);
81: }
83: // ---------- HELPER METHODS --------------------------------------- //
84:
85: // The size of the font to use in this Application
86: private static final int FONT_SIZE = 18;
88: /**
89: * Create a label in the appropriate font size
90: *
91: * @param text the text of the new label
92: * @return a new Label with the given text in large font size
93: */
94: private Label makeLabel(String text) {
95: Label result = new Label(text);
96: result.setFont(new Font(FONT_SIZE));
97: return result;
98: }
100: /**
101: * Create a right-justified text field in the appropriate font size
102: *
103: * @param text the text of the new text field
104: * @return a new TextField, right-justified and with the given text in
105: * large font size
106: */
107: private TextField makeTextField(String text) {
108: TextField result = new TextField(text);
109: result.setFont(new Font(FONT_SIZE));
110: result.setAlignment(Pos.CENTER_RIGHT);
111: return result;
112: }
114: /**
115: * Create a button with the appropriate font size
116: *
117: * @param text the text on the new button
118: * @return a new Button with the given text in large font size
119: */
120: private Button makeButton(String text) {
121: Button result = new Button(text);
122: result.setFont(new Font(FONT_SIZE));
123: return result;
124: }
126: /**
127: * Sum two text fields into a third.
128: *
129: * @param num1Field TextField containing one of the two addends
130: * @param num2Field TextField containing the other of the two addends
131: * @param resultField TextField where the result is to be placed
132: */
133: private void addFields(
134: TextField num1Field,
135: TextField num2Field,
136: TextField resultField
137: ) {
138: // get number from first text field
139: int num1 = Integer.parseInt(num1Field.getText());
140:
141: // get number from second text field
142: int num2 = Integer.parseInt(num2Field.getText());
143:
144: // calculate sum
145: int sum = num1 + num2;
146:
147: // store result
148: resultField.setText(Integer.toString(sum));
149: }
151: }