package javafx8image; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.input.MouseEvent; import javafx.scene.layout.StackPane; import javafx.stage.Stage; /** * * @web http://java-buddy.blogspot.com/ */ public class JavaFX8Image extends Application { @Override public void start(Stage primaryStage) { ImageView imageView = new ImageView(); imageView.setOnMouseClicked(new EventHandler<MouseEvent>(){ @Override public void handle(MouseEvent event) { imageView.setImage(null); } }); Button btn = new Button(); btn.setText("Load image"); btn.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) { imageView.setImage(new Image("http://goo.gl/kYEQl")); } }); StackPane root = new StackPane(); root.getChildren().addAll(btn, imageView); Scene scene = new Scene(root, 300, 250); primaryStage.setTitle("java-buddy.blogspot.com"); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } }
Use Lambda expression
package javafx8imagelambda; import javafx.application.Application; import static javafx.application.Application.launch; import javafx.event.ActionEvent; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.input.MouseEvent; import javafx.scene.layout.StackPane; import javafx.stage.Stage; /** * * @web http://java-buddy.blogspot.com/ */ public class JavaFX8ImageLambda extends Application { @Override public void start(Stage primaryStage) { ImageView imageView = new ImageView(); //use Lambda expression imageView.setOnMouseClicked((MouseEvent event) -> { imageView.setImage(null); }); Button btn = new Button(); btn.setText("Load image"); //use Lambda expression btn.setOnAction((ActionEvent event) -> { imageView.setImage(new Image("http://goo.gl/kYEQl")); }); StackPane root = new StackPane(); root.getChildren().addAll(btn, imageView); Scene scene = new Scene(root, 300, 250); primaryStage.setTitle("java-buddy.blogspot.com"); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } }
No comments:
Post a Comment