Created
May 4, 2018 16:48
-
-
Save sharifulislam52/a252263fb1ec97ba1c8faade795976bc to your computer and use it in GitHub Desktop.
javafx : Calculator example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javafx.application.Application; | |
import javafx.fxml.FXMLLoader; | |
import javafx.stage.Stage; | |
import javafx.scene.Parent; | |
import javafx.scene.Scene; | |
public class Main extends Application { | |
@Override | |
public void start(Stage primaryStage) { | |
try { | |
Parent root = FXMLLoader.load(getClass().getResource("Main.fxml")); | |
Scene scene = new Scene(root,300,300); | |
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm()); | |
primaryStage.setScene(scene); | |
primaryStage.setResizable(false); | |
primaryStage.show(); | |
} catch(Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
public static void main(String[] args) { | |
launch(args); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<?import javafx.scene.control.Button?> | |
<?import javafx.scene.control.Label?> | |
<?import javafx.scene.layout.HBox?> | |
<?import javafx.scene.layout.StackPane?> | |
<?import javafx.scene.layout.VBox?> | |
<?import javafx.scene.text.Font?> | |
<VBox prefHeight="300.0" prefWidth="300.0" spacing="10.0" xmlns="http://javafx.com/javafx/9.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MainController"> | |
<children> | |
<StackPane prefHeight="50.0" prefWidth="200.0"> | |
<children> | |
<Label fx:id="result" prefHeight="17.0" prefWidth="300.0"> | |
<font> | |
<Font name="System Bold" size="18.0" /> | |
</font> | |
</Label> | |
</children></StackPane> | |
<HBox alignment="CENTER" prefHeight="50.0" prefWidth="300.0" spacing="10.0"> | |
<children> | |
<Button mnemonicParsing="false" onAction="#processNumber" prefWidth="50.0" text="7"> | |
<font> | |
<Font size="18.0" /> | |
</font> | |
</Button> | |
<Button mnemonicParsing="false" onAction="#processNumber" prefWidth="50.0" text="8"> | |
<font> | |
<Font size="18.0" /> | |
</font> | |
</Button> | |
<Button mnemonicParsing="false" onAction="#processNumber" prefWidth="50.0" text="9"> | |
<font> | |
<Font size="18.0" /> | |
</font> | |
</Button> | |
<Button mnemonicParsing="false" onAction="#processOperator" prefWidth="50.0" text="/"> | |
<font> | |
<Font size="18.0" /> | |
</font> | |
</Button> | |
</children> | |
</HBox> | |
<HBox alignment="CENTER" prefHeight="50.0" prefWidth="300.0" spacing="10.0"> | |
<children> | |
<Button mnemonicParsing="false" onAction="#processNumber" prefWidth="50.0" text="4"> | |
<font> | |
<Font size="18.0" /> | |
</font> | |
</Button> | |
<Button mnemonicParsing="false" onAction="#processNumber" prefWidth="50.0" text="5"> | |
<font> | |
<Font size="18.0" /> | |
</font> | |
</Button> | |
<Button mnemonicParsing="false" onAction="#processNumber" prefWidth="50.0" text="6"> | |
<font> | |
<Font size="18.0" /> | |
</font> | |
</Button> | |
<Button mnemonicParsing="false" onAction="#processOperator" prefWidth="50.0" text="*"> | |
<font> | |
<Font size="18.0" /> | |
</font> | |
</Button> | |
</children> | |
</HBox> | |
<HBox alignment="CENTER" prefHeight="50.0" prefWidth="300.0" spacing="10.0"> | |
<children> | |
<Button mnemonicParsing="false" onAction="#processNumber" prefWidth="50.0" text="1"> | |
<font> | |
<Font size="18.0" /> | |
</font> | |
</Button> | |
<Button mnemonicParsing="false" onAction="#processNumber" prefWidth="50.0" text="2"> | |
<font> | |
<Font size="18.0" /> | |
</font> | |
</Button> | |
<Button mnemonicParsing="false" onAction="#processNumber" prefWidth="50.0" text="3"> | |
<font> | |
<Font size="18.0" /> | |
</font> | |
</Button> | |
<Button mnemonicParsing="false" onAction="#processOperator" prefWidth="50.0" text="-"> | |
<font> | |
<Font size="18.0" /> | |
</font> | |
</Button> | |
</children> | |
</HBox> | |
<HBox alignment="CENTER" prefHeight="50.0" prefWidth="300.0" spacing="10.0"> | |
<children> | |
<Button mnemonicParsing="false" onAction="#processNumber" prefWidth="110.0" text="0"> | |
<font> | |
<Font size="18.0" /> | |
</font> | |
</Button> | |
<Button mnemonicParsing="false" onAction="#processOperator" prefWidth="50.0" text="="> | |
<font> | |
<Font size="18.0" /> | |
</font> | |
</Button> | |
<Button mnemonicParsing="false" onAction="#processOperator" prefWidth="50.0" text="+"> | |
<font> | |
<Font size="18.0" /> | |
</font> | |
</Button> | |
</children> | |
</HBox> | |
</children> | |
</VBox> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Model { | |
public float calculate(long number1, long number2, String operator) { | |
switch(operator) { | |
case "+": | |
return number1+number2; | |
case "-": | |
return number1-number2; | |
case "*": | |
return number1*number2; | |
case "/": | |
if(number2 == 0) {return 0;} | |
else {return number1/number2;} | |
default: | |
return 0; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javafx.event.ActionEvent; | |
import javafx.fxml.FXML; | |
import javafx.scene.control.Label; | |
import javafx.scene.control.Button; | |
public class MainController { | |
@FXML | |
private Label result; | |
private long number1 = 0; | |
private String operator = ""; | |
private boolean start = true; | |
private Model model = new Model(); | |
@FXML | |
public void processNumber(ActionEvent event) { | |
if(start) { | |
result.setText(""); | |
start = false; | |
} | |
String value = ((Button)event.getSource()).getText(); | |
result.setText(result.getText() + value); | |
} | |
@FXML | |
public void processOperator(ActionEvent event) { | |
String value = ((Button)event.getSource()).getText(); | |
if(!value.equals("=")) { | |
if(!operator.isEmpty()) { | |
return; | |
} | |
else { | |
operator = value; | |
number1 = Long.parseLong(result.getText()); | |
result.setText(""); | |
} | |
} | |
else { | |
if(operator.isEmpty()) { | |
return; | |
} | |
else { | |
long number2 = Long.parseLong(result.getText()); | |
float output = model.calculate(number2, number2, operator); | |
result.setText(String.valueOf(output)); | |
operator = ""; | |
start = true; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment