Question

In: Computer Science

Java Please comment code Create an Interactive JavaFX Application Create an application with the following controls:...

Java

Please comment code

Create an Interactive JavaFX Application

  • Create an application with the following controls:
    • A Label control with the following text displayed: "First Number:"
    • A Label control with the following text displayed: "Second Number:"
    • An empty TextField control beside the First Number label.
    • An empty TextField control beside the Second Number label.
    • Five buttons:
      • Button 1 labeled +
      • Button 2 labeled -
      • Button 3 labeled *
      • Button 4 labeled /
      • Button 5 labeled =
    • An empty Label control that will display the result of a mathematical operation.
  • Make sure there is spacing between each control.
  • Align the controls:
    • The Label control with the text First Number on the first line with a TextField beside it.
    • The buttons labeled +, -, *, / on the second line. Make sure there is space between the buttons.
    • The Label control with the text Second Number on the third line with a TextField beside it.
    • The button labeled = on the fourth line.
    • The label that will contain the result of the operation on the fifth line.
    • Make sure the window is large enough to display all controls.
  • Create an event for each button:
    • Capture the text entered in the first TextField control. Note that values in a TextField are strings. You will need to convert it to a numeric value.
    • Capture the text entered in the second TextField control. Note that values in a TextField are strings. You will need to convert it to a numeric value.
    • Perform the mathematical operation indicated by the button being pressed.
    • When the = button is pressed display the result of the calculation in the results label.
  • The order of execution for the user is as follows. Your program must respond in this order:
    • Enter a value in the first TextField.
    • Click on an operation button.
    • Enter a value in the second TextField.
    • Click on the equals button.
    • View the result in the Label control.
  • Display the window.

Solutions

Expert Solution

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);
   }

}


Related Solutions

Create in java an interactive GUI application program that will prompt the user to use one...
Create in java an interactive GUI application program that will prompt the user to use one of three calculators, label project name MEDCALC. You can use any color you want for your background other than grey. Be sure to label main java box with “MEDCALC” and include text of “For use only by students” On all three calculators create an alert and signature area for each user. The alert should be something like “All calculations must be confirmed by user...
Topic is relates to Java programming! Code using java GUI 2. Create the following controls: a)...
Topic is relates to Java programming! Code using java GUI 2. Create the following controls: a) a TextField b) a Button c) a Text d) a Label 3. create the following container and put them the above controls: a) a VBox controller b) a BorderPane controller c) a GridPane
JAVAFX Create a JavaFx application that is an MP3 player. The size of the media player...
JAVAFX Create a JavaFx application that is an MP3 player. The size of the media player should be 800 pixels in width and 650 pixels in height. The media player should have a TextField that allows the user to type the full path of the .mp3 file to be played. The application should also use 2 Checkbox controls to show/hide the total playing time and the status (methods of MediaPlayer class). Use 2 RadioButton controls to change background colors (red...
Create a JavaFX program in java that does the following items: Display a drawing area of...
Create a JavaFX program in java that does the following items: Display a drawing area of dimension 500 x 400, with a black background. Provides a radio button group to allow the user to select one of the following three colors: red, green, and blue. Upon startup, the red radio button should be selected. Each time the user clicks in the drawing area, a circle of size 10 of the color selected by the radio button group in item 2...
Please write code in java and comment . thanksItem classA constructor, with a String...
Please write code in java and comment . thanksItem classA constructor, with a String parameter representing the name of the item.A name() method and a toString() method, both of which are identical and which return the name of the item.BadAmountException ClassIt must be a RuntimeException. A RuntimeException is a subclass of Exception which has the special property that we wouldn't need to declare it if we need to use it.It must have a default constructor.It must have a constructor which...
Java In a JavaFX application there is a TextField control(tfRadius) for taking input of a circle’s...
Java In a JavaFX application there is a TextField control(tfRadius) for taking input of a circle’s radius. A respective square whose diagonal is the circle's diameter can then be created. There are three other controls in this application: a button(btCalculate), two textareas(trCircleArea, trSquareArea). When the button is clicked, the circle's area is calculated and displayed on trCircleArea, furthermore, the square's area is also calculated and displayed on trSquareArea. Please write the portions of codes which calculates the circle’s area and...
Create a JavaFX application that lets the user enter the food charge for a meal at...
Create a JavaFX application that lets the user enter the food charge for a meal at a restaurant. For example, if $20 is entered as a food charge for a meal then $3.6 should be displayed for the tip, $1.4 should be displayed for sales tax, and $25 should be displayed as a total of all three amounts. Modification 1: create a text box (not a pop up) for the user to enter a percent tip (don't just hardcode it...
Please write code in java and comment . thanks Item class A constructor, with a String...
Please write code in java and comment . thanks Item class A constructor, with a String parameter representing the name of the item. A name() method and a toString() method, both of which are identical and which return the name of the item. BadAmountException Class It must be a RuntimeException. A RuntimeException is a subclass of Exception which has the special property that we wouldn't need to declare it if we need to use it. It must have a default...
This question is about java program. Please show the output and the detail code and comment...
This question is about java program. Please show the output and the detail code and comment of the each question and each Class and interface. And the output (the test mthod )must be all the true! Thank you! Question1 Create a class Animal with the following UML specification: +-----------------------+ | Animal | +-----------------------+ | - name: String | +-----------------------+ | + Animal(String name) | | + getName(): String | | + getLegs(): int | | + canFly(): boolean | |...
In Java and using JavaFX, write a client/server application with two parts, a server and a...
In Java and using JavaFX, write a client/server application with two parts, a server and a client. Have the client send the server a request to compute whether a number that the user provided is prime. The server responds with yes or no, then the client displays the answer.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT