FXMLDocumentController.java package calculator; import java.net.URL; import java.util.ResourceBundle; import javafx.event.ActionEv...
FXMLDocumentController.java
package calculator;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
/**
*
* @author amir
*/
public class FXMLDocumentController implements Initializable {
@FXML
private TextField txt_result;
String op ="";
long number1;
long number2;
// click CTRL
// First Method
public void Number (ActionEvent ae){
String no = ((Button)ae.getSource()).getText();
txt_result.setText(txt_result.getText()+no);
}
// Second Method
public void Operation (ActionEvent ae){
String operation = ((Button)ae.getSource()).getText();
if (!operation.equals("=")){
if(!op.equals("")){
return;
}
op = operation;
number1 = Long.parseLong(txt_result.getText());
txt_result.setText("");
}else {
if(op.equals("")){
return;
}
number2 = Long.parseLong(txt_result.getText());
calculate(number1, number2, op);
op="";
}
}
// 3 Methode
public void calculate (long n1, long n2, String op){
switch (op){
case "+" : txt_result.setText(n1 + n2 + "");break;
case "-" : txt_result.setText(n1 - n2 + "");break;
case "*" : txt_result.setText(n1 * n2 + "");break;
case "/" :
if (n2 == 0){
txt_result.setText("0");break;
}
txt_result.setText(n1/n2+ "");break;
}
}
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
Class Calculator.java
package calculator;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
*
* @author amir
*/
public class Calculator extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
FXMLDocument.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="300.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="calculator.FXMLDocumentController">
<children>
<TextField fx:id="txt_result" prefHeight="65.0" prefWidth="300.0">
<VBox.margin>
<Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
</VBox.margin>
<font>
<Font size="25.0" />
</font>
</TextField>
<HBox prefHeight="40.0" prefWidth="300.0">
<children>
<Button mnemonicParsing="false" onAction="#Number" prefHeight="0.0" prefWidth="60.0" text="7">
<HBox.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</HBox.margin>
<font>
<Font name="System Bold" size="22.0" />
</font>
</Button>
<Button mnemonicParsing="false" onAction="#Number" prefHeight="0.0" prefWidth="60.0" text="8">
<HBox.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</HBox.margin>
<font>
<Font name="System Bold" size="22.0" />
</font>
</Button>
<Button mnemonicParsing="false" onAction="#Number" prefHeight="0.0" prefWidth="60.0" text="9">
<HBox.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</HBox.margin>
<font>
<Font name="System Bold" size="22.0" />
</font>
</Button>
<Button mnemonicParsing="false" onAction="#Operation" prefHeight="0.0" prefWidth="60.0" style="-fx-background-color: yellow;" text="/">
<HBox.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</HBox.margin>
<font>
<Font name="System Bold" size="22.0" />
</font>
</Button>
</children>
</HBox>
<HBox prefHeight="40.0" prefWidth="300.0">
<children>
<Button mnemonicParsing="false" onAction="#Number" prefHeight="45.0" prefWidth="60.0" text="4">
<HBox.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</HBox.margin>
<font>
<Font name="System Bold" size="22.0" />
</font>
</Button>
<Button mnemonicParsing="false" onAction="#Number" prefHeight="0.0" prefWidth="60.0" text="5">
<HBox.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</HBox.margin>
<font>
<Font name="System Bold" size="22.0" />
</font>
</Button>
<Button mnemonicParsing="false" onAction="#Number" prefHeight="0.0" prefWidth="60.0" text="6">
<HBox.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</HBox.margin>
<font>
<Font name="System Bold" size="22.0" />
</font>
</Button>
<Button mnemonicParsing="false" onAction="#Operation" prefHeight="0.0" prefWidth="60.0" style="-fx-background-color: red;" text="-">
<HBox.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</HBox.margin>
<font>
<Font name="System Bold" size="22.0" />
</font>
</Button>
</children>
</HBox>
<HBox prefHeight="40.0" prefWidth="300.0">
<children>
<Button mnemonicParsing="false" onAction="#Number" prefHeight="0.0" prefWidth="60.0" text="1">
<HBox.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</HBox.margin>
<font>
<Font name="System Bold" size="22.0" />
</font>
</Button>
<Button mnemonicParsing="false" onAction="#Number" prefHeight="0.0" prefWidth="60.0" text="2">
<HBox.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</HBox.margin>
<font>
<Font name="System Bold" size="22.0" />
</font>
</Button>
<Button mnemonicParsing="false" onAction="#Number" prefHeight="0.0" prefWidth="60.0" text="3">
<HBox.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</HBox.margin>
<font>
<Font name="System Bold" size="22.0" />
</font>
</Button>
<Button mnemonicParsing="false" onAction="#Operation" prefHeight="0.0" prefWidth="60.0" style="-fx-background-color: pink;" text="*">
<HBox.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</HBox.margin>
<font>
<Font name="System Bold" size="22.0" />
</font>
</Button>
</children>
</HBox>
<HBox prefHeight="40.0" prefWidth="300.0">
<children>
<Button mnemonicParsing="false" onAction="#Number" prefHeight="48.0" prefWidth="138.0" text="0">
<HBox.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</HBox.margin>
<font>
<Font name="System Bold" size="22.0" />
</font>
</Button>
<Button mnemonicParsing="false" onAction="#Operation" prefHeight="0.0" prefWidth="60.0" style="-fx-background-color: Cyan;" text="=">
<HBox.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</HBox.margin>
<font>
<Font name="System Bold" size="22.0" />
</font>
</Button>
<Button mnemonicParsing="false" onAction="#Operation" prefHeight="0.0" prefWidth="60.0" style="-fx-background-color: green;" text="+">
<HBox.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</HBox.margin>
<font>
<Font name="System Bold" size="22.0" />
</font>
</Button>
</children>
</HBox>
</children>
</VBox>
COMMENTS