Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
32 views2 pages

JavaFX Calculator Code Overview

This document summarizes the key aspects of a JavaFX application code for building a simple calculator application, including: 1) Importing necessary JavaFX and Java classes; 2) Defining a Calc class that extends Application for creating the JavaFX application; 3) Using properties, enums, and arrays to store and represent calculation values, operations, and button layouts; 4) Implementing methods for setting up the user interface, handling keyboard input, and performing calculations.

Uploaded by

Ayano Boresa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views2 pages

JavaFX Calculator Code Overview

This document summarizes the key aspects of a JavaFX application code for building a simple calculator application, including: 1) Importing necessary JavaFX and Java classes; 2) Defining a Calc class that extends Application for creating the JavaFX application; 3) Using properties, enums, and arrays to store and represent calculation values, operations, and button layouts; 4) Implementing methods for setting up the user interface, handling keyboard input, and performing calculations.

Uploaded by

Ayano Boresa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

1.

Package and Imports:


o The code begins with the package declaration package application;, indicating that the
class Calc belongs to the application package.
o The following import statements bring in necessary classes from the JavaFX library and other
standard Java libraries:
 import javafx.application.Application;: Imports
the Application class from the JavaFX library.
 import javafx.beans.binding.Bindings;: Imports the Bindings class
for creating bindings between properties.
 import javafx.beans.property.*;: Imports various property classes
(e.g., DoubleProperty, SimpleDoubleProperty) for managing property
values.
 import javafx.geometry.Pos;: Imports the Pos enum for positioning
elements.
 import javafx.scene.Scene;: Imports the Scene class for defining the
visual content of a JavaFX application.
 import javafx.scene.control.*;: Imports various UI control classes
(e.g., Button, TextField).
 import javafx.scene.input.KeyEvent;: Imports the KeyEvent class for
handling keyboard events.
 import javafx.scene.layout.*;: Imports layout-related classes
(e.g., VBox, TilePane) for arranging UI elements.
 import javafx.stage.*;: Imports classes related to application stages
(e.g., Stage, StageStyle).
2. Class Definition:
o The class Calc extends the Application class, which is a requirement for creating a
JavaFX application.
o The class contains various fields and methods for building a simple calculator application.
3. Template Array:
o The template array is a 2D array of strings. It represents the layout of calculator buttons,
where each string corresponds to a button label.
o For example, "7" represents the button with the label “7,” and "+" represents the addition
button.
4. Accelerators Map:
o The accelerators map is used to associate keyboard input (e.g., key presses) with specific
buttons. It maps key strings to Button objects.
5. Double Properties:
o stackValue and value are DoubleProperty objects. These properties are used to store
numeric values during calculations.
o stackValue represents a value stored in the calculator’s memory (e.g., for memory
operations).
o value represents the current input value.
6. Enum for Operations:
o The Op enum defines different calculator operations: NOOP (no
operation), ADD, SUBTRACT, MULTIPLY, and DIVIDE.
o curOp and stackOp are variables of type Op used to track the current operation and the
operation stored in the stack.
7. main Method:
o The main method is the entry point of the Java application. It launches the JavaFX
application by calling launch(args).
8. start Method:
o The start method is overridden from the Application class.
o It sets up the calculator’s user interface (UI) components, including the screen (TextField)
and buttons (TilePane).
o The UI is displayed in a non-resizable window with the title “Calculator.”
9. createLayout Method:
o This method creates the layout for the calculator UI by arranging the screen and buttons in a
vertical box (VBox).
o The background color is set to gold, and padding and font size are adjusted.
10. handleAccelerators Method:
o This method handles keyboard accelerators (shortcut keys) for calculator buttons.
o When a key is pressed, it looks up the corresponding button in the accelerators map.

You might also like