Question

I mainly need help with the “Mouse Events” & “Command Buttons” sections

Sqrt xA2 Cir CircleButton Clas:s The graphic circular buttons are created by drawing a filled Circle on a StackPane. So, theSimpleCalc Class (extends Application) This is your main project class that contains your start0 method. This class will crea

0 0
Add a comment Improve this question Transcribed image text
Answer #1

SimpleCalc.java

import javafx.application.Application;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class SimpleCalc extends Application{
      private TextField txt;
  
      @Override
    public void start(Stage primaryStage) throws Exception {
        BorderPane borderPane = new BorderPane();
      
      
        borderPane.setTop(txtField());
        borderPane.setCenter(gridPane());
        borderPane.setBottom(hBox());
      
        Scene scene = new Scene(borderPane);
      
        primaryStage.setScene(scene);
        primaryStage.show();
    }
     
    private TextField txtField() {
      
       txt = new TextField();
       txt.setEditable(false);
       return txt;
     
    }
  
    public void toLaunch() {
        this.launch();
    }
  
    private GridPane gridPane() {
        GridPane gridPane = new GridPane();
      
        int counter = 1;
        for(int row =0; row < 3; row++) {
            for(int col = 0; col < 3; col++) {
                CircleButton circleButton = new CircleButton(counter + "");
                counter++;
              
              
              
                circleButton.setOnMousePressed(new EventHandler() {
                    @Override
                    public void handle(Event event) {
                        circleButton.setColor(Color.RED);
                        txt.appendText(circleButton.getValue());
                    }
                });
              
                 circleButton.setOnMouseReleased(new EventHandler() {
                    @Override
                    public void handle(Event event) {
                        circleButton.setColor(Color.WHITE);
                   
                    }
                });
              
                gridPane.add(circleButton, col, row);
            }
        }
      
        return gridPane;
    }
  
   public HBox hBox() {
    HBox hbox = new HBox();
     Button xsquared = new Button("x^2");
     Button sqrt = new Button("Sqrt");
     Button clear = new Button("Clear");

  
    clear.setOnAction(new EventHandler() {
        @Override
        public void handle(Event event) {
        txt.clear();
    }
    });
  
    sqrt.setOnAction(new EventHandler() {
        @Override
        public void handle(Event event) {
        txt.setText(String.valueOf(Math.sqrt(Double.parseDouble(txt.getText()))));
    }
    });
  
    xsquared.setOnAction(new EventHandler() {
        @Override
        public void handle(Event event) {
            txt.setText(String.valueOf(Math.pow(Double.parseDouble(txt.getText()),2.0)));
      
    }
    });
     hbox.getChildren().add(xsquared);
     hbox.getChildren().add(sqrt);
     hbox.getChildren().add(clear);
    return hbox;
    }

}


CircleButton.java


package calculator;

import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.shape.Circle;

public class CircleButton extends StackPane{
  
    private Label lblValue;
    private Circle circle;
  
    public CircleButton() {
        circle = new Circle();
        lblValue = new Label("");
      
        circle.setRadius(90);
        circle.setFill(Color.WHITE);
      
        this.setPrefSize(100, 100);
      
        this.getChildren().add(circle);
        this.getChildren().add(lblValue);
    }
  
    public CircleButton(String val) {
        circle = new Circle();
        lblValue = new Label(val);
      
        circle.setRadius(90);
        circle.setFill(Color.WHITE);
      
        this.setPrefSize(100, 100);
      
        this.getChildren().add(circle);
        this.getChildren().add(lblValue);
    }
  
    public void setColor(Paint color) {
        circle.setFill(color);
    }
  
    public Paint getColor() {
        return circle.getFill();
    }
  
    public String getValue() {
        return lblValue.getText();
    }

}

Add a comment
Know the answer?
Add Answer to:
I mainly need help with the “Mouse Events” & “Command Buttons” sections Sqrt xA2 Cir CircleButton...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The Gui has all the right buttons, but from there i get lost. I need to...

    The Gui has all the right buttons, but from there i get lost. I need to know whats wrong with my assignment can someone please help. The code I have so far is listed below, could you please show me the errors in my code. PYTHON Create the GUI(Graphical User Interface). Use tkinter to produce a form that looks much like the following. It should have these widgets. Temperature Converter GUI Enter a temperature (Entry box)                 Convert to Fahrenheit...

  • Java Painter Class This is the class that will contain main. Main will create a new...

    Java Painter Class This is the class that will contain main. Main will create a new Painter object - and this is the only thing it will do. Most of the work is done in Painter’s constructor. The Painter class should extend JFrame in its constructor.  Recall that you will want to set its size and the default close operation. You will also want to create an overall holder JPanel to add the various components to. It is this JPanel that...

  • could you please help me with this problem, also I need a little text so I...

    could you please help me with this problem, also I need a little text so I can understand how you solved the problem? import java.io.File; import java.util.Scanner; /** * This program lists the files in a directory specified by * the user. The user is asked to type in a directory name. * If the name entered by the user is not a directory, a * message is printed and the program ends. */ public class DirectoryList { public static...

  • please help!!!! JAVA I done the project expect one part but I still give you all...

    please help!!!! JAVA I done the project expect one part but I still give you all the detail that you needed... and I will post my code please help me fix the CreateGrid() part in main and make GUI works    List Type Data Structures Overview : You will be implementing my version of a linked list. This is a linked list which has possible sublists descending from each node. These sublists are used to group together all nodes which...

  • NEED HELP with HTML with Javascript embedding for form validation project below. I have my code...

    NEED HELP with HTML with Javascript embedding for form validation project below. I have my code below but I'm stuck with validation. If anyone can fix it, I'd really appreciate. ****************************************************************************** CODE: <!DOCTYPE html> <!-- To change this license header, choose License Headers in Project Properties. To change this template file, choose Tools | Templates and open the template in the editor. --> <html> <head> <title>Nice</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <script> var textFromTextArea; function getWords(){ var text =...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT