NO IMAGE

1.介面

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class Main extends Application implements EventHandler{

Button button;
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("set the title");
button=new Button();
button.setText("Click me");
**button.setOnAction(this);**
StackPane layout=new StackPane();
layout.getChildren().add(button);
Scene scene=new Scene(layout,300,300);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
**@Override
public void handle(ActionEvent event) {
System.out.println("Wow ! I am clicked !");
}**

}

2.anonymous 匿名類

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class Main extends Application{

Button button;
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("set the title");
button=new Button();
button.setText("Click me");
**button.setOnAction(new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent event) {
System.out.println("Today is traditional lovers day in China !");
System.out.println("I am a single dog !");
}
});**
StackPane layout=new StackPane();
layout.getChildren().add(button);
Scene scene=new Scene(layout,300,300);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}

}

3.lambda表示式

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class Main extends Application{

Button button;
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("set the title");
button=new Button();
button.setText("Click me");
**button.setOnAction(e -> {
System.out.println("I love u !");
System.out.println("Do u love me ?");
});**
StackPane layout=new StackPane();
layout.getChildren().add(button);
Scene scene=new Scene(layout,300,300);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}

}