如何使用 javafx 和 fxml 中的 ImageView 组件显示图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22710053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I show an image using the ImageView component in javafx and fxml?
提问by user3472050
I suppose it's a very simple thing but I just can't get behind it. All I want is to show an image over an ImageView linked to fxml. Here is my code:
我想这是一件非常简单的事情,但我无法支持它。我想要的只是在链接到 fxml 的 ImageView 上显示图像。这是我的代码:
package application;
import java.io.File;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
public class Main extends Application
{
@FXML
private ImageView imageView;
@Override
public void start(Stage primaryStage)
{
try
{
AnchorPane root = (AnchorPane)FXMLLoader.load(getClass().getResource("Sample.fxml"));
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setTitle("Hello World");
File file = new File("src/Box13.jpg");
Image image = new Image(file.toURI().toString());
imageView = new ImageView(image);
//root.getChildren().add(imageView);
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
And my fxml file
还有我的 fxml 文件
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="316.0" prefWidth="321.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="application.SampleController">
<children>
<ImageView fx:id="imageView" fitHeight="150.0" fitWidth="200.0" layoutX="61.0" layoutY="83.0" pickOnBounds="true" preserveRatio="true" >
</ImageView>
</children>
</AnchorPane>
There should be no problem with the file linking as it works fine when I include the outcommented line. This would be the way it's done in java only but I want to use fxml here as I am using fxml for all other components but it just doesn't work for the ImageView and I don't know why. I have also tried to create a new controller class and link the ImageView there but that neither works. Can anyone help me?
文件链接应该没有问题,因为当我包含注释行时它可以正常工作。这将是它仅在 java 中完成的方式,但我想在这里使用 fxml,因为我将 fxml 用于所有其他组件,但它对 ImageView 不起作用,我不知道为什么。我还尝试创建一个新的控制器类并将 ImageView 链接到那里,但这都不起作用。谁能帮我?
Thanks
谢谢
回答by Fernando Paz
If you want to use FXML, you should separate the controller (like you were doing with the SampleController). Then your fx:controller
in your FXML should point to that.
如果您想使用 FXML,您应该分离控制器(就像您对 SampleController 所做的那样)。然后你fx:controller
在你的 FXML 中应该指向那个。
Probably you are missing the initialize
method in your controller, which is part of the Initializable
interface. This method is called after the FXML is loaded, so I recommend you to set your image there.
您可能缺少initialize
控制器中的方法,它是Initializable
接口的一部分。这个方法在 FXML 加载后调用,所以我建议你在那里设置你的图像。
Your SampleController
class must be something like this:
你的SampleController
班级必须是这样的:
public class SampleController implements Initializable {
@FXML
private ImageView imageView;
@Override
public void initialize(URL location, ResourceBundle resources) {
File file = new File("src/Box13.jpg");
Image image = new Image(file.toURI().toString());
imageView.setImage(image);
}
}
I tested here and it's working.
我在这里进行了测试,并且可以正常工作。
回答by Aaron Zimmerman
You don't need an initializer, unless you're dynamically loading a different image each time. I think doing as much as possible in fxml is more organized. Here is an fxml file that will do what you need.
您不需要初始化程序,除非您每次都动态加载不同的图像。我认为在 fxml 中尽可能多地做更有条理。这是一个 fxml 文件,可以满足您的需求。
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>
<AnchorPane
xmlns:fx="http://javafx.co/fxml/1"
xmlns="http://javafx.com/javafx/2.2"
fx:controller="application.SampleController"
prefHeight="316.0"
prefWidth="321.0"
>
<children>
<ImageView
fx:id="imageView"
fitHeight="150.0"
fitWidth="200.0"
layoutX="61.0"
layoutY="83.0"
pickOnBounds="true"
preserveRatio="true"
>
<image>
<Image
url="src/Box13.jpg"
backgroundLoading="true"
/>
</image>
</ImageView>
</children>
</AnchorPane>
Specifying the backgroundLoading property in the Image tag is optional, it defaults to false. It's best to set backgroundLoading true when it takes a moment or longer to load the image, that way a placeholder will be used until the image loads, and the program wont freeze while loading.
在 Image 标签中指定 backgroundLoading 属性是可选的,它默认为 false。当加载图像需要一点时间或更长时间时,最好将 backgroundLoading 设置为 true,这样在图像加载之前将使用占位符,并且程序在加载时不会冻结。
回答by sabracadabra
@FXML
ImageView image;
@Override
public void initialize(URL url, ResourceBundle rb) {
image.setImage(new Image ("/about.jpg"));
}
回答by Ranjitsinh
Please find below example to load image using JavaFX.
请找到以下示例以使用 JavaFX 加载图像。
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class LoadImage extends Application {
public static void main(String[] args) {
Application.launch(args);
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Load Image");
StackPane sp = new StackPane();
Image img = new Image("javafx.jpg");
ImageView imgView = new ImageView(img);
sp.getChildren().add(imgView);
//Adding HBox to the scene
Scene scene = new Scene(sp);
primaryStage.setScene(scene);
primaryStage.show();
}
}
Create one source folder with name Image in your project and add your image to that folder otherwise you can directly load image from external URL like following.
在您的项目中创建一个名为 Image 的源文件夹,并将您的图像添加到该文件夹中,否则您可以直接从外部 URL 加载图像,如下所示。
Image img = new Image("http://mikecann.co.uk/wp-content/uploads/2009/12/javafx_logo_color_1.jpg");
Image img = new Image(" http://mikecann.co.uk/wp-content/uploads/2009/12/javafx_logo_color_1.jpg");
回答by stuckhelper
It's recommended to put the image to the resources, than you can use it like this:
建议将图像放在资源中,而不是像这样使用它:
imageView = new ImageView("/gui.img/img.jpg");
回答by veysel sar?
src/sample/images/shopp.png
源代码/样本/图像/shopp.png
**
Parent root =new StackPane();
ImageView ?mageView=new ImageView(new Image(getClass().getResourceAsStream("images/shopp.png")));
((StackPane) root).getChildren().add(?mageView);
**