ImageView来自项目空间时将不显示图像 [英] ImageView won't display the Image when it comes from project space

查看:81
本文介绍了ImageView来自项目空间时将不显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我是JavaFX的新手,并且作为项目的一部分,我试图使用ImageView显示一些图像.在添加到我的实际项目之前,我进行了以下设置,以确保我了解如何使用ImageViews.

So, I am new to JavaFX and as part of a project I am attempting to use ImageView to display some images. Before adding to my actual project, I set up the following to be sure I understood how to use ImageViews.

我的控制器:

import javafx.fxml.FXML;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;

public class TestController {


    @FXML
    ImageView imageView;

    Image image;

    public void start(Stage stage){
        /*
         * THIS WORKS! 
         * 
         *  image = new   Image("file:/C://Users//Owner//Pictures//MyProjectPhotos/picture.jpg");
         */

        //BUT THIS DOESN'T :(
        image = new   Image("file:/JavaFXPractice/photos/picture.jpg");

        imageView.setImage(image);

        imageView.setOnMouseClicked(me -> System.out.println("hello"));
    }

}

fxml:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity"
 minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" 
 xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1"    
 fx:controller="application.TestController">
   <children>
      <VBox layoutX="156.0" layoutY="88.0" prefHeight="272.0"    prefWidth="378.0">
         <children>
            <ImageView fx:id="imageView" fitHeight="276.0" fitWidth="378.0"   
             pickOnBounds="true" preserveRatio="true" />
          </children>
       </VBox>
    </children>
 </AnchorPane>

最后是这个

import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {

        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(getClass().getResource("/view/TestPage.fxml"));
        AnchorPane root = (AnchorPane) loader.load();
        Scene scene = new Scene(root);

        primaryStage.setScene(scene);
        primaryStage.setResizable(false);
        primaryStage.show();

        TestController control = loader.getController();
        control.start(primaryStage);
    }

    public static void main(String[] args) {
        launch(args);
    }
}

我的问题在TestController类中.正如您在多行注释(以及它下面的代码的注释/行)中看到的那样,当我从本地计算机获取图像时,该图像正确显示,但是当我从项目空间获取该图像时,该图像不能正确显示.我已经尝试了几个小时来解决这个问题,但是每次尝试从项目空间中检索任何照片时,ImageView上都不会显示任何内容.它不为空(图像也不为空),并且没有报告的错误.我也尝试过寻找答案,但到目前为止还没有运气.

My problem is in the TestController class. As you can see in the multi-line comment (and also the comment/line of code below it), the image displays properly when I get it from my local machine, but not when I get it from my project space. I have tried for hours now to figure this out, but every single time I try to retrieve any photo from my project space, nothing appears on the ImageView. It is not null (neither is the Image), and there are no reported errors. I have also tried searching for an answer, but no luck so far.

推荐答案

您可以像这样加载图像:

You can load your images like this:

image = new Image(getClass().getResource("/photos/picture.jpg").toExternalForm());

在这种假设下,照片"是项目根目录下的文件夹.该文件夹必须在类路径上.根据您的IDE,这可以通过不同的方式来实现.在Eclipse中,可以通过将该文件夹放入源文件夹"来实现.典型的结构是

under the assumption, that "photos" is a folder at the root of your project. This folder must be on the classpath. Depending on your IDE this can be achieved in different ways. In Eclipse this can be achieved by putting this folder into a "source folder". A typical structure is

project
   src
      ...
   res
      photos
         picture.jpg

"src"和"res"都是源文件夹.

where both "src" and "res" are source folders.

这篇关于ImageView来自项目空间时将不显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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