0% found this document useful (0 votes)
88 views10 pages

Java Mini Project

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

Java Mini Project

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

JAVA MINI PROJECT

CALCULATOR

DONE BY
1. PREETHI SHREE B
2. PRINCY MAGDALINE
3. PRIYA DHARSHINI R K
4. RAGAVI C

PROBLEM STATEMENT:
To build a calculator using java fx controls and layouts.

JAVAFX:
JavaFX is a Java library and a GUI toolkit designed to
develop and facilitate Rich Internet applications, web
applications, and desktop applications.
CONTROLS AND LAYOUTS:
1.TEXT:
The text node is represented by the class named
Text, which belongs to the package
javafx.scene.text.This class contains several properties to
create text in JavaFX and modify its appearance.
Example:-

Text text = new Text();


String text = "Hello how are you"
Text.setText(text);

2) TEXTFIELD:
TextField class is a part of JavaFX package. It is
a component that allows the user to enter a line of
unformatted text, it does not allow multi-line input it
only allows the user to enter a single line of text.
Constructor of the TextField class :
• TextField(): creates a new TextField with empty text
content
• TextField(String s): creates a new TextField with a
initial text .

TILEPLANE:-
• In the JavaFX application, in order to
set TilePane as a layout, the TilePane
class is used.
• The TilePane layout allows us to
arrange the components in the same
sized tiles according to specified
boundries.
Syntax:
• javafx.scene.layout.TilePane class.

3)BUTTON:-

Button class is a part of JavaFX package and it


can have a text or graphic or both.When the button is
pressed an Action Event is sent. This Action Event can
be managed by an EventHandler.
1)Button(): creates a button with an empty string for its
label.
2)Button(String t): creates a button with the specified
text as its label.

.4)SetOnAction():-

ActionEvent is an event type that is processed by


EventHandler. An EventHandler object provides the
handle method to process an action fired for a button.

5)getText():-
You can retrieve the data using the from textField
getText() method.
Data_type variable=object of TextField.gettext();

6)GRIDPANE:-

A JavaFX GridPane is a layout component which


lays out its child components in a grid. The size of the
cells in the grid depends on the components displayed
in the GridPane, but there are some rules. All cells in the
same row will have the same height, and all cells in the
same column will have the same width. Different rows
can have different heights and different columns can
have different widths.

• alignment − This property represents the alignment


of the pane and you can set value of this property using
the setAlignment() method.

• hgap − This property is of the type double and it


represents the horizontal gap between columns.
• 3)vgap − This property is of the type double and it
represents the vertical gap between rows.

7)setFont():-

When adding text, you can also set some of its


properties. To set the font, you can use an instance of the
javafx.scene.text.Font class. The Font.font() method
enables you to specify the font family name and size.

• t.setText("This is a text sample");


• t.setFont(Font.font ("Verdana", 20));

.8)STAGE AND SCENE:-


1)STAGE:-
A stage (a window) contains all the objects of a
JavaFX application. It is represented by Stage class of the
package javafx.stage. The primary stage is created by
the platform itself. The created stage object is passed as
an argument to the start() method of the Application
class.

2)SCENE:-

A scene represents the physical contents of a JavaFX


application. It contains all the contents of a scene graph.
The class Scene of the package javafx.scene represents
the scene object. At an instance, the scene object is
added to only one stage.
You can create a scene by instantiating the Scene Class.
You can opt for the size of the scene by passing its
dimensions (height and width) along with the root node
to its constructor.

9) SET PADDING
Setpading sets the margin for the child when
contained by grid pane.
EG: buttonBox.setPadding(new Insets(20,0,0,0));

SOURCE CODE:
package
intelij;
\
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.property.*;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.*;
import javafx.stage.*;

import java.util.*;

// a simple JavaFX calculator.


public class Calc extends Application {
private static final String[][] template = {
{"7", "8", "9", "/"},
{"4", "5", "6", "*"},
{"1", "2", "3", "-"},
{"0", "c", "=", "+"}
};

public Map<String, Button> accelerators = new HashMap<>();

public DoubleProperty stackValue = new SimpleDoubleProperty();


public DoubleProperty value = new SimpleDoubleProperty();

public enum Op {NOOP, ADD, SUBTRACT, MULTIPLY, DIVIDE}

public Op curOp = Op.NOOP;


public Op stackOp = Op.NOOP;

public static void main(String[] args) {


launch(args);
}

@Override
public void start(Stage stage) {
TextField screen = createScreen();
TilePane buttons = createButtons();

stage.setTitle("Calc");
stage.initStyle(StageStyle.UTILITY);
stage.setResizable(false);
stage.setScene(new Scene(createLayout(screen, buttons)));
stage.show();
}

public VBox createLayout(TextField screen, TilePane buttons) {


VBox layout = new VBox(20);
layout.setAlignment(Pos.CENTER);
layout.setStyle("-fx-background-color: chocolate; -fx-padding: 20;
-fx-font-size: 20;");
layout.getChildren().setAll(screen, buttons);
handleAccelerators(layout);
screen.prefWidthProperty().bind(buttons.widthProperty());
return layout;
}

public void handleAccelerators(VBox layout) {


layout.addEventFilter(KeyEvent.KEY_PRESSED, keyEvent -> {
Button activated = accelerators.get(keyEvent.getText());
if (activated != null) {
activated.fire();
}
});
}

public TextField createScreen() {


TextField screen = new TextField();
screen.setStyle("-fx-background-color: aquamarine;");
screen.setAlignment(Pos.CENTER_RIGHT);
screen.setEditable(false);
screen.textProperty().bind(Bindings.format("%.0f", value));
return screen;
}

public TilePane createButtons() {


TilePane buttons = new TilePane();
buttons.setVgap(7);
buttons.setHgap(7);
buttons.setPrefColumns(template[0].length);
for (String[] r : template) {
for (String s : r) {
buttons.getChildren().add(createButton(s));
}
}
return buttons;
}

public Button createButton(String s) {


Button button = makeStandardButton(s);

if (s.matches("[0-9]")) {
makeNumericButton(s, button);
} else {
final ObjectProperty<Op> triggerOp = determineOperand(s);
if (triggerOp.get() != Op.NOOP) {
makeOperandButton(button, triggerOp);
} else if ("c".equals(s)) {
makeClearButton(button);
} else if ("=".equals(s)) {
makeEqualsButton(button);
}
}

return button;
}

public ObjectProperty<Op> determineOperand(String s) {


ObjectProperty<Op> triggerOp = new SimpleObjectProperty<>(Op.NOOP);
switch (s) {
case "+" -> triggerOp.set(Op.ADD);
case "-" -> triggerOp.set(Op.SUBTRACT);
case "*" -> triggerOp.set(Op.MULTIPLY);
case "/" -> triggerOp.set(Op.DIVIDE);
}
return triggerOp;
}

public void makeOperandButton(Button button, final ObjectProperty<Op>


triggerOp) {
button.setStyle("-fx-base: lightgray;");
button.setOnAction(actionEvent -> curOp = triggerOp.get());
}

public Button makeStandardButton(String s) {


Button button = new Button(s);
button.setStyle("-fx-base: beige;");
accelerators.put(s, button);
button.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
return button;
}

public void makeNumericButton( String s, Button button) {


button.setOnAction(actionEvent -> {
if (curOp == Op.NOOP) {
value.set(value.get() * 10 + Integer.parseInt(s));
} else {
stackValue.set(value.get());
value.set(Integer.parseInt(s));
stackOp = curOp;
curOp = Op.NOOP;
}
});
}

public void makeClearButton(Button button) {


button.setStyle("-fx-base: mistyrose;");
button.setOnAction(actionEvent -> value.set(0));
}

public void makeEqualsButton(Button button) {


button.setStyle("-fx-base: ghostwhite;");
button.setOnAction(actionEvent -> {
switch (stackOp) {
case ADD -> value.set(stackValue.get() + value.get());
case SUBTRACT -> value.set(stackValue.get() - value.get());
case MULTIPLY -> value.set(stackValue.get() * value.get());
case DIVIDE -> value.set(stackValue.get() / value.get());
}
});
}
}
OUTPUT:

You might also like