import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
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.stage.Stage;
public class Calculator extends Application
{
// the calculator dimensions
public static int CALC_WIDTH = 400;
public static int CALC_HEIGHT = 300;
// the calculator screen
private TextField screen;
// the calculator buttons
private Button button1;
private Button button2;
private Button button3;
private Button button4;
public static void main(String[] args)
{
launch(args);
}
@Override
public void start(Stage primaryStage)
{
// create the calculator screen
screen = new TextField();
// create the buttons
button1 = new Button("1");
button2 = new Button("2");
button3 = new Button("3");
button4 = new Button("4");
// attach a handler to process button clicks
ButtonHandler handler = new ButtonHandler();
button1.setOnAction(handler);
button2.setOnAction(handler);
button3.setOnAction(handler);
button4.setOnAction(handler);
// setup a grid panel for the keypad
GridPane keypad = new GridPane();
keypad.setMinSize(CALC_WIDTH, CALC_HEIGHT);
// keypad.setPadding(new Insets(10, 10, 10, 10));
// keypad.setVgap(5);
// keypad.setHgap(5);
keypad.setAlignment(Pos.CENTER);
// attach the buttons to the keypad grid
keypad.add(button1, 0, 0);
keypad.add(button2, 1, 0);
keypad.add(button3, 0, 1);
keypad.add(button4, 1, 1);
// put screen and keypad together
BorderPane gui = new BorderPane();
gui.setTop(screen);
gui.setCenter(keypad);
// set up the scene
Scene scene = new Scene(gui);
primaryStage.setTitle("Calculator");
primaryStage.setScene(scene);
primaryStage.show();
}
// Handler for processing the button clicks
private class ButtonHandler implements EventHandler<ActionEvent>
{
@Override
public void handle(ActionEvent e)
{
if (e.getSource() == button1) {
System.out.println("Button 1 Pressed");
}
else if (e.getSource() == button2) {
System.out.println("Button 2 Pressed");
}
else if (e.getSource() == button3) {
System.out.println("Button 3 Pressed");
}
else if (e.getSource() == button4) {
System.out.println("Button 4 Pressed");
}
}
}
}