当我将其作为webstart或浏览器运行时,javaFx中的ImageView不会显示 [英] ImageView in javaFx is not shown when i run it as webstart or in browser

查看:801
本文介绍了当我将其作为webstart或浏览器运行时,javaFx中的ImageView不会显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试将图像加载到 ImageView 中,当我将其作为独立应用程序运行时,它可以正常工作,但是当我尝试运行它时浏览器或webstart它没有显示。



我试过:

  ivFoto = new ImageView(new Image(file:/ C:/Users/Carlos/Desktop/4fbzW.jpg)); 

  ivFoto = new ImageView(new Image(file:\\C:\\Users\\Carlos \\Desktop\\4fbzW.jpg)); 

  ivFoto = new ImageView(file:/// C:/Users/Carlos/Desktop/4fbzW.jpg); 

如果有人知道我在这里做错了什么,我真的很感激帮助!

解决方案

在你的应用程序jar中打包你的图像( 4fbzW.jpg )文件(与加载图像的类在同一位置)并将其作为资源引用:

  ivFoto = new ImageView(
new Image(
this.getClass()。getResource(4fbzW.jpg)。toExternalForm()

);






图片有几处出错您问题中的参考:


  1. 硬编码文件: protocol。



    JavaFX应用程序通常打包为jar文件,因此应使用jar协议访问应用程序的资源。通过使用 getResource 方法,资源将被用于加载引用资源的类的相同协议提取,因此它将自动使用正确的协议来查找资源和您无需担心此细节。


  2. 参考开发机器上的本地资源。



    Webstart和浏览器嵌入式应用程序部署方案主要用于引用通过网络传送到远程客户端的应用程序。客户端计算机的用户可以拥有任何用户名或使用任何支持的操作系统,因此他们不太可能有一个文件位于: C:/ Users / Carlos / Desktop 。通过将图像与您的应用程序打包并从相对于应用程序代码的位置获取图像,无论在何处部署图像,都可以始终如一地找到图像。


  3. <基于文件的访问违反了WebStart和浏览器嵌入式应用程序的安全沙箱



    这些部署模式运行具有受限权限的应用程序,以防止恶意应用程序做坏事(如删除所有用户文件或将其发送到外国情报机构)。要允许应用程序在这些部署模式下以提升的安全权限运行,您需要签署应用程序,用户需要明确信任您提供的软件。在您的应用的特定情况下,我不建议签名,而是将您需要的资源打包到应用中,这样您和您的用户就不需要承担已签名应用的不便。







参见: javafx.scene.image.Image(" flower.png")在哪里寻找flower.png?


I've been trying to load an image into an ImageView, and it works fine when I run it as a standalone app, but when I try to run it in the browser or as webstart it doesn't get shown.

I've tried:

ivFoto = new ImageView(new Image("file:/C:/Users/Carlos/Desktop/4fbzW.jpg"));

or

ivFoto = new ImageView(new Image("file:\\C:\\Users\\Carlos\\Desktop\\4fbzW.jpg"));

or

ivFoto = new ImageView("file:///C:/Users/Carlos/Desktop/4fbzW.jpg");

If anyone has any idea of what I'm doing wrong here, I would really appreciate the help!

解决方案

Package your image (4fbzW.jpg) in your application jar file (in the same location as the class loading the image) and reference it as a resource:

ivFoto = new ImageView(
  new Image(
    this.getClass().getResource("4fbzW.jpg").toExternalForm()
  )
);


There are a couple of things wrong with the image references in your question:

  1. Hardcoding the file: protocol.

    A JavaFX application will usually be packaged as a jar file, so resources for the application should be accessed using the jar protocol. By using the getResource method, the resource will be fetched by the same protocol used to load the class referencing the resource, thus it will automatically use the correct protocol to find the resource and you don't need to worry about this detail.

  2. Referring to local resources on your development machine.

    Webstart and browser embedded application deployment scenarios are primarily used to reference applications which are delivered over a network to a remote client. The user of the client machine could have any username or be using any supported OS, so they will be unlikely to have a file located at: C:/Users/Carlos/Desktop. By packaging the image with your application and getting it from a location relative to your application's code, then the image can always be found consistently no matter where it is deployed.

  3. File based access violates the security sandbox for WebStart and browser embedded apps

    These kind of deployment modes run applications with restricted privileges to prevent malicious applications from doing bad things (like deleting all users files or sending them to a foreign intelligence service). To allow applications to run with elevated security privileges under these deployment modes, you need to sign the applications and the user needs to explicitly trust the software provided by you. In the particular case of your app, I would not recommend signing, but instead package the resources you require with the app so that you and your users do not need to undergo the inconvenience of a signed app.


See also: Where does javafx.scene.image.Image("flower.png") look for flower.png?

这篇关于当我将其作为webstart或浏览器运行时,javaFx中的ImageView不会显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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