如何使用javafx和fxml中的ImageView组件显示图像? [英] How can I show an image using the ImageView component in javafx and fxml?

查看:5281
本文介绍了如何使用javafx和fxml中的ImageView组件显示图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这是一件非常简单的事情,但我无法支持它。
我想要的是在链接到fxml的ImageView上显示图像。
这是我的代码:

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:

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>

文件链接应该没有问题,因为当我包含已注释的行时,它可以正常工作。这将是它在java中的方式,但我想在这里使用fxml,因为我使用fxml用于所有其他组件,但它只是不适用于ImageView,我不知道为什么。我还尝试创建一个新的控制器类并将ImageView链接到那里,但这两者都不起作用。任何人都可以帮助我吗?

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?

谢谢

推荐答案

如果你想使用FXML,你应该将控制器分开(就像你在使用SampleController一样)。然后你的FXML中的 fx:controller 应该指向那个。

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.

可能你错过了在控制器中初始化方法,该方法是 Initializable 界面的一部分。在加载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.

您的 SampleController class必须是这样的:

Your SampleController class must be something like this:

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.

这篇关于如何使用javafx和fxml中的ImageView组件显示图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆