例
import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.stage.Stage; public class ImageViewExample extends Application { public void start(Stage stage) throws IOException { //创建图像对象 InputStream stream = new FileInputStream("D:\\images\\elephant.jpg"); Image image = new Image(stream); //创建图像视图 ImageView imageView = new ImageView(); //将图像设置为图像视图 imageView.setImage(image); //设置图像视图参数 imageView.setX(10); imageView.setY(10); imageView.setFitWidth(575); imageView.setPreserveRatio(true); //设置场景对象 Group root = new Group(imageView); Scene scene = new Scene(root, 595, 370); stage.setTitle("Displaying Image"); stage.setScene(scene); stage.show(); } public static void main(String args[]) { launch(args); } }
输出结果