简体   繁体   English

如何使用 javafx 和 fxml 中的 ImageView 组件显示图像?

[英]How can I show an image using the ImageView component in javafx and fxml?

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.我想要的只是在链接到 fxml 的 ImageView 上显示图像。 Here is my code:这是我的代码:

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.这将是它仅在 java 中完成的方式,但我想在这里使用 fxml,因为我将 fxml 用于所有其他组件,但它对 ImageView 不起作用,我不知道为什么。 I have also tried to create a new controller class and link the ImageView there but that neither works.我还尝试创建一个新的控制器类并将 ImageView 链接到那里,但这都不起作用。 Can anyone help me?谁能帮我?

Thanks谢谢

If you want to use FXML, you should separate the controller (like you were doing with the SampleController).如果您想使用 FXML,您应该分离控制器(就像您对 SampleController 所做的那样)。 Then your fx:controller in your FXML should point to that.然后你的 FXML 中的fx:controller应该指向那个。

Probably you are missing the initialize method in your controller, which is part of the Initializable interface.您可能缺少控制器中的initialize方法,它是Initializable接口的一部分。 This method is called after the FXML is loaded, so I recommend you to set your image there.这个方法在 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.我在这里进行了测试,并且可以正常工作。

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.我认为在 fxml 中尽可能多地做更有条理。 Here is an fxml file that will do what you need.这是一个 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.在 Image 标签中指定 backgroundLoading 属性是可选的,它默认为 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.当加载图像需要一点时间或更长时间时,最好将 backgroundLoading 设置为 true,这样在图像加载之前将使用占位符,并且程序在加载时不会冻结。

The Code part :代码部分:

Image imProfile = new Image(getClass().getResourceAsStream("/img/profile128.png"));

ImageView profileImage=new ImageView(imProfile);

in a javafx maven:在 javafx Maven 中:

在此处输入图片说明

@FXML
ImageView image;

@Override
public void initialize(URL url, ResourceBundle rb) {
  image.setImage(new Image ("/about.jpg"));
}

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 ");

建议将图像放在资源中,而不是像这样使用它:

imageView = new ImageView("/gui.img/img.jpg");

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM