import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.Pos; import javafx.scene.control.Button; import javafx.scene.control.TextField; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; import javafx.stage.Stage; import javafx.scene.Scene; public class Kalkulator extends Application{ TextField res; TextField t1, t2; @Override public void start(Stage stage) { VBox rootPane = new VBox(); HBox textPane = new HBox(); textPane.setAlignment(Pos.CENTER); t1 = new TextField(); t2 = new TextField(); textPane.getChildren().addAll(t1, t2); HBox buttonPane = new HBox(); buttonPane.setAlignment(Pos.CENTER); Button plus = new Button("+"); Button min = new Button("-"); Button mult = new Button("X"); Button div = new Button("/"); ButtonHandler h = new ButtonHandler(); plus.setOnAction(h); min.setOnAction(h); mult.setOnAction(h); div.setOnAction(h); res = new TextField("Result"); buttonPane.getChildren().addAll(plus, min, mult, div, res); rootPane.getChildren().addAll(textPane, buttonPane); stage.setScene(new Scene(rootPane, 200, 100)); stage.show(); } private class ButtonHandler implements EventHandler { @Override public void handle(ActionEvent e) { Button tmp = (Button) e.getSource(); Double num1=0.0, num2=0.0; try { num1 = Double.parseDouble(t1.getText()); num2 = Double.parseDouble(t2.getText()); } catch (NumberFormatException exe) { System.out.println("Dette er ikke tall!"); return; } String op = tmp.getText(); double sum = 0; switch (op) { case "+": sum = num1+num2; break; case "-": sum=num1-num2; break; case "X": sum=num1*num2; break; case "/": sum=num1/num2; break; } res.setText(""+sum); } } }