javafx.scene.p_w_picpath.Image类的作用是从文件或者网站显示一个图片,例如:new Image("p_w_picpath/us.gif")为图形文件us.gif创建一个Image对象。
javafx.scene.p_w_picpath.ImageView是显示图片的node。一个ImageView能从一个Image对象创建。例如:
Image p_w_picpath = new Image("p_w_picpath/us.gif");
ImageView p_w_picpathView = new ImageView(p_w_picpath);
下面的程序显示三张图片:
import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.layout.HBox; import javafx.scene.layout.Pane; import javafx.geometry.Insets; import javafx.stage.Stage; import javafx.scene.p_w_picpath.Image; import javafx.scene.p_w_picpath.ImageView; public class ShowImage extends Application { @Override // Override the start method in the Application class public void start(Stage primaryStage) { // Create a pane to hold the p_w_picpath views Pane pane = new HBox(10); pane.setPadding(new Insets(5,5,5,5)); Image p_w_picpath = new Image("e:\1.jpg"); pane.getChildren().add(new ImageView(p_w_picpath)); ImageView p_w_picpathView2 = new ImageView(p_w_picpath); p_w_picpathView2.setFitHeight(100); p_w_picpathView2.setFitWidth(100); pane.getChildren().add(p_w_picpathView2); ImageView p_w_picpathView3 = new ImageView(p_w_picpath); p_w_picpathView3.setRotate(90); pane.getChildren().add(p_w_picpathView3); // Create a scene and place it in the stage Scene scene = new Scene(pane); primaryStage.setTitle("ShowImage");// Set the stage title primaryStage.setScene(scene); // Place the scene in the stage primaryStage.show(); // Display the stage } }
说明:
1、本代码的显示结果如下:
2、本程序创建了一个HBox对象,HBox能把所有的node水平放置在一排。
3、如果用URL加载图片的话,网址前的http://不能省略。
转载于:https://blog.51cto.com/fengbin8606/1423551