如何使用javafx和fxml中的ImageView组件显示图像?
我想这是一件很简单的事情,但我就是无法理解。 我只想在链接到fxml的ImageView上显示一个图像。 这是我的密码:如何使用javafx和fxml中的ImageView组件显示图像?,java,imageview,fxml,Java,Imageview,Fxml,我想这是一件很简单的事情,但我就是无法理解。 我只想在链接到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
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);
}
}
和我的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>
文件链接应该不会有问题,因为当我包含outcommented行时,它工作得很好。这将是仅在java中完成的方式,但我想在这里使用fxml,因为我在所有其他组件中使用fxml,但它不适用于ImageView,我不知道为什么。我还尝试创建一个新的控制器类并将ImageView链接到那里,但这两种方法都不起作用。有人能帮我吗
谢谢如果您想使用FXML,您应该分离控制器(就像使用SampleController一样)。然后,FXML中的
fx:controller
应该指向该点
您可能缺少控制器中的initialize
方法,该方法是Initializable
接口的一部分。此方法在加载FXML后调用,因此我建议您在那里设置映像
您的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);
}
}
imageView = new ImageView("/gui.img/img.jpg");
我在这里进行了测试,它正常工作。您不需要初始值设定项,除非每次动态加载不同的图像。我认为在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>
在图像标记中指定backgroundLoading属性是可选的,默认为false。当加载图像需要一段时间或更长时间时,最好将backgroundLoading设置为true,这样在加载图像之前将使用占位符,并且程序在加载时不会冻结。请查看下面使用JavaFX加载图像的示例
@FXML
ImageView image;
@Override
public void initialize(URL url, ResourceBundle rb) {
image.setImage(new Image ("/about.jpg"));
}
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();
}
}
在项目中创建一个名为Image的源文件夹,并将图像添加到该文件夹中,否则您可以直接从外部URL加载图像,如下所示
图像img=新图像(“”) 建议将图像放到参考资料中,然后您可以这样使用它:
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);
}
}
imageView = new ImageView("/gui.img/img.jpg");
src/sample/images/shopp.png
**
Parent root =new StackPane();
ImageView ımageView=new ImageView(new Image(getClass().getResourceAsStream("images/shopp.png")));
((StackPane) root).getChildren().add(ımageView);
**
代码部分:
Image imProfile = new Image(getClass().getResourceAsStream("/img/profile128.png"));
ImageView profileImage=new ImageView(imProfile);
在javafx maven中:
imageView
在此阶段未初始化。它将引发异常。@FaizanMubasher当通过FXML加载程序加载此控制器时,只有在初始化所有JavaFX组件后才会自动调用initialize
方法,因此它不会引发异常。此代码正在工作,谢谢。另一个答案是,只有在我没有编译它的情况下,在我编译到JAR之后,图像才起作用。