import javafx.application.Application; import javafx.scene.layout.*; import javafx.scene.*; import javafx.scene.control.*; import javafx.stage.Stage; import javafx.scene.text.*; import javafx.scene.shape.Rectangle; import javafx.scene.paint.*; import javafx.event.*; public class HeiPerson extends Application { public TextField tekstfelt = null; public Label tekst = null; @Override public void start(Stage stage) { VBox root = new VBox(); Button knapp = new Button("Trykk!"); // Vanlig metode knapp.setOnAction(new Knappebehandler()); // Ved bruk av lambda // knapp.setOnAction((e) -> tekst.setText("Hei " + tekstfelt.getText())); tekstfelt = new TextField("Skriv ditt navn"); tekst = new Label("..."); root.getChildren().addAll(tekstfelt, knapp, tekst); stage.setScene(new Scene(root, 200, 200)); stage.show(); } class Knappebehandler implements EventHandler { public void handle(ActionEvent e) { String inputText = "Hei " + tekstfelt.getText(); tekst.setText(inputText); } } }