import javafx.application.Application; import javafx.stage.Stage; import javafx.scene.Scene; import javafx.scene.layout.VBox; import javafx.scene.layout.HBox; import javafx.scene.control.Button; import javafx.scene.control.TextField; import javafx.event.EventHandler; import javafx.event.ActionEvent; import javafx.geometry.Pos; import javafx.scene.layout.GridPane; import javafx.scene.text.Text; public class Kalkulator extends Application { TextField x; TextField y; Text output = new Text("Utregning"); /*Tips: Noen elementer b?r kunne hentes ut av en hendelsesbehandler!*/ @Override public void start (Stage stage) { x = new TextField("Skriv inn tall 1"); y = new TextField("Skriv inn tall 2"); VBox rootPane = new VBox(); HBox hbTekst = new HBox(); GridPane rutenett = new GridPane(); Button pluss = new Button("+"); Button minus = new Button("-"); Button multiplikasjon = new Button("x"); Button divisjon = new Button("/"); pluss.setOnAction(new EventHandler() { @Override public void handle(ActionEvent event) { int minSum = (Integer.parseInt(x.getText())) + Integer.parseInt(y.getText()); String tekst = "" + minSum; output.setText(tekst); } }); minus.setOnAction(new EventHandler() { @Override public void handle(ActionEvent event) { int minSum = (Integer.parseInt(x.getText())) - Integer.parseInt(y.getText()); String tekst = "" + minSum; output.setText(tekst); } }); multiplikasjon.setOnAction(new EventHandler() { @Override public void handle(ActionEvent event) { int minSum = (Integer.parseInt(x.getText())) * Integer.parseInt(y.getText()); String tekst = "" + minSum; output.setText(tekst); } }); divisjon.setOnAction(new EventHandler() { @Override public void handle(ActionEvent event) { int minSum = (Integer.parseInt(x.getText())) / Integer.parseInt(y.getText()); String tekst = "" + minSum; output.setText(tekst); } }); hbTekst.getChildren().addAll(x,y); rutenett.add(pluss, 0, 0); rutenett.add(minus, 1, 0); rutenett.add(multiplikasjon, 0, 1); rutenett.add(divisjon, 1, 1); rootPane.getChildren().addAll(hbTekst, rutenett, output); Scene scene = new Scene(rootPane); stage.setScene(scene); stage.show(); } public static void main(String[] args) { Application.launch(args); } }