In: Computer Science
Java
Please comment code
Create an Interactive JavaFX Application
Program screenshot:
Sample output screenshot:
Code to copy:
/**************** File name: Calculator.java
*****************/
package application;
// Import statements
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
// Create a class calculator
public class Calculator extends Application
{
// Required components
Label lblFNum;
TextField txtFNum;
Button btnPlus;
Button btnMinus;
Button btnMul;
Button btnDiv;
Label lblSNum;
TextField txtSNum;
Button btnEql;
Label lblResult;
double num1 = 0;
double num2 = 0;
char operation = '=';
@Override
public void start(Stage primaryStage) throws
Exception
{
lblFNum = new Label("First
Number:");
txtFNum = new TextField("");
btnPlus = new Button("+");
btnMinus = new Button("-");
btnMul = new Button("*");
btnDiv = new Button("/");
btnEql = new Button("=");
lblSNum = new Label("Second
Number:");
txtSNum = new TextField("");
lblResult = new Label("Result");
// Create a panel
GridPane root = new
GridPane();
// Align the position center
root.setAlignment(Pos.CENTER);
root.setHgap(10);
root.setVgap(10);
// (columnNumberStart, Row
Number, ColumnsToMerge, NumComponents)
// Add the components to the panel
and sets
// the position of the components
in the panel
root.add(lblFNum, 0, 0, 2,
1);
root.add(txtFNum, 2, 0, 2, 1);
root.add(btnPlus, 0, 1);
root.add(btnMinus, 1, 1);
root.add(btnMul, 2, 1);
root.add(btnDiv, 3, 1);
root.add(lblSNum, 0, 3, 2,
1);
root.add(txtSNum, 2, 3, 2, 1);
root.add(btnEql, 0, 4, 4, 1);
lblResult.setAlignment(Pos.CENTER);
root.add(lblResult, 0, 5, 4, 1);
setWidths();
// Add the panel to the
screen
Scene scene = new Scene(root, 300,
250);
// Set title
primaryStage.setTitle("Calculator");
// Add screen to the stage
primaryStage.setScene(scene);
// Display stage
primaryStage.show();
// Create the button
events
btnPlus.setOnAction(new
EventHandler<ActionEvent>()
{
@Override
public void
handle(ActionEvent e)
{
operation = '+';
}
});
btnMinus.setOnAction(new
EventHandler<ActionEvent>()
{
@Override
public void
handle(ActionEvent e)
{
operation = '-';
}
});
btnMul.setOnAction(new
EventHandler<ActionEvent>()
{
@Override
public void
handle(ActionEvent e)
{
operation = '*';
}
});
btnDiv.setOnAction(new
EventHandler<ActionEvent>()
{
@Override
public void
handle(ActionEvent e)
{
operation = '/';
}
});
// This button event is helps to
perform the math calcuation.
btnEql.setOnAction(new
EventHandler<ActionEvent>()
{
@Override
public void
handle(ActionEvent e)
{
num1 =
Double.parseDouble(txtFNum.getText());
num2 =
Double.parseDouble(txtSNum.getText());
double result = 0.0;
switch (operation)
{
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
if (num2 == 0)
{
lblResult.setText("Inf");
break;
}
else
{
result =
num1 / num2;
break;
}
default:
lblResult.setText("Please
select the math operator.");
break;
}
if(num2 != 0 || operation != '/')
{
lblResult.setText(String.valueOf(result));
}
}
});
}
// Create a method named setWidths.
// This method helps to set the size of the each
component
private void setWidths()
{
lblFNum.setPrefWidth(100);
txtFNum.setPrefWidth(50);
btnPlus.setPrefWidth(38);
btnMinus.setPrefWidth(38);
btnMul.setPrefWidth(38);
btnDiv.setPrefWidth(38);
lblSNum.setPrefWidth(100);
txtSNum.setPrefWidth(50);
btnEql.setPrefWidth(200);
lblResult.setPrefWidth(200);
}
// Create a main method to run the program.
public static void main(String args[])
{
// Launch the application
launch(args);
}
}