Created
November 22, 2015 10:03
-
-
Save dheshanm/b9a79159552c36538f41 to your computer and use it in GitHub Desktop.
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
| package Practice; | |
| import javafx.application.Application; | |
| import javafx.event.ActionEvent; | |
| import javafx.geometry.Pos; | |
| import javafx.scene.Scene; | |
| import javafx.scene.control.Button; | |
| import javafx.scene.control.Label; | |
| import javafx.scene.control.TextField; | |
| import javafx.scene.layout.GridPane; | |
| import javafx.stage.Stage; | |
| public class CalculatorGUI extends Application{ | |
| TextField num1,num2; | |
| Button add,sub,mul,div,clr; | |
| Label ans; | |
| @Override | |
| public void start(Stage primaryStage){ | |
| //Making Controls | |
| num1=new TextField(); | |
| num2=new TextField(); | |
| add=new Button("+"); | |
| sub=new Button("-"); | |
| mul=new Button("x"); | |
| div=new Button("/"); | |
| clr=new Button("Clear"); | |
| ans=new Label("?"); | |
| ans.setAlignment(Pos.CENTER); | |
| ans.setStyle("-fx-border-color: #000; -fx-padding: 5px;"); | |
| GridPane root=new GridPane(); | |
| root.setAlignment(Pos.CENTER); | |
| //Set Spacing between Controls in Grid | |
| root.setHgap(10); | |
| root.setVgap(10); | |
| //adding to Grid | |
| root.add(add,0,0); | |
| root.add(sub, 1, 0); | |
| root.add(mul,0,1); | |
| root.add(div,1,1); | |
| root.add(num1,0,2); | |
| root.add(num2,1,2); | |
| // 2 Column Buttons | |
| root.add(ans,0,3,2,1); | |
| root.add(clr,0,4,2,1); | |
| setWidths(); | |
| //attach buttons to code in separate method | |
| attachCode(); | |
| //usual stuff | |
| Scene scene = new Scene(root, 300, 250); | |
| primaryStage.setTitle("Calculator"); | |
| primaryStage.setScene(scene); | |
| primaryStage.show(); | |
| } | |
| public void setWidths(){ | |
| //set widths of all controls | |
| num1.setPrefWidth(70); | |
| num2.setPrefWidth(70); | |
| add.setPrefWidth(70); | |
| sub.setPrefWidth(70); | |
| mul.setPrefWidth(70); | |
| div.setPrefWidth(70); | |
| clr.setPrefWidth(150); | |
| ans.setPrefWidth(150); | |
| } | |
| public void attachCode() | |
| { | |
| //have each button run BTNCODE when clicked | |
| add.setOnAction(e -> btncode(e)); | |
| sub.setOnAction(e -> btncode(e)); | |
| mul.setOnAction(e -> btncode(e)); | |
| div.setOnAction(e -> btncode(e)); | |
| clr.setOnAction(e -> btncode(e)); | |
| } | |
| public void btncode(ActionEvent e) | |
| { | |
| int anum1, anum2, answer; | |
| char symbol; | |
| //e tells us which button was clicked | |
| if(e.getSource()==clr) | |
| { | |
| num1.setText(""); | |
| num2.setText(""); | |
| ans.setText("?"); | |
| num1.requestFocus(); | |
| return; | |
| } | |
| //read numbers in from textfields | |
| anum1=Integer.parseInt(num1.getText()); | |
| anum2=Integer.parseInt(num2.getText()); | |
| if(e.getSource()==add) | |
| { | |
| symbol='+'; | |
| answer=anum1+anum2; | |
| } | |
| else if(e.getSource()==sub) | |
| { | |
| symbol='-'; | |
| answer=anum1-anum2; | |
| } | |
| else if(e.getSource()==mul) | |
| { | |
| symbol='x'; | |
| answer=anum1*anum2; | |
| } | |
| else | |
| { | |
| symbol='/'; | |
| answer=anum1/anum2; | |
| } | |
| //display answer | |
| ans.setText("" + anum1 + symbol + anum2 + "=" + answer); | |
| } | |
| public static void main(String[] args) { | |
| launch(args); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment