Tartalomjegyzék

< Java FX

Java FX Példák

Nyomógomb

App.java
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.control.Button;
 
public class App extends Application implements EventHandler<ActionEvent> {
 
    Button button;
 
    public static void main(String[] args) {
        launch(args);
    }
 
    @Override
    public void start(Stage stage) {
        button = new Button();
        button.setText("Katt");
        button.setOnAction(this);
        StackPane root = new StackPane();
        root.getChildren().add(button);
        Scene scene = new Scene(root, 300, 250);
        stage.setScene(scene);
        stage.setTitle("Helló Világ!");
        stage.show();
    }
 
    @Override
    public void handle(ActionEvent event) {
        if (event.getSource() == button) {
            System.out.println("Kattintás volt");
        }
 
    }
 
}

Lambda

App.java
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.control.Button;
 
public class App extends Application {
 
    Button button;
 
    public static void main(String[] args) {
        launch(args);
    }
 
    @Override
    public void start(Stage stage) {
        button = new Button();
        button.setText("Katt");
        button.setOnAction(e -> System.out.println("Kattintás volt"));
        StackPane root = new StackPane();
        root.getChildren().add(button);
        Scene scene = new Scene(root, 300, 250);
        stage.setScene(scene);
        stage.setTitle("Helló Világ!");
        stage.show();
    }
}

Több utasítás is megadható:

        button.setOnAction(e -> {
            System.out.println("Kattintás volt");
            System.out.println("Kattintás volt");
        });