首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我的图像无法使用JavaFX加载

我的图像无法使用JavaFX加载
EN

Stack Overflow用户
提问于 2017-04-12 04:06:35
回答 1查看 845关注 0票数 0

我正在阅读“JavaFX快速入门指南”这本书,我读到了第7章,但由于某种原因,我的图像无法加载

下面是我的代码:

代码语言:javascript
复制
    /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package chapter7;


import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.scene.image.ImageView;
import javafx.scene.image.Image;
import javafx.scene.paint.Color;

/**
 *
 * @author svetlana
 */
public class Chapter7 extends Application {

     /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

   @Override
    public void start(Stage primaryStage) {

        Image butterfly = new Image("http://pngimg.com/uploads/butterfly/butterfly_PNG1024.png");
        ImageView imageView = new ImageView();

        imageView.setImage(butterfly);
        imageView.setFitWidth(300);
        imageView.setPreserveRatio(true);


        StackPane root = new StackPane();
        root.getChildren().add(imageView);

        primaryStage.setScene(new Scene(root, 300, 250));
        primaryStage.show();
    }
}

这些是NetBeans提供给我的错误

代码语言:javascript
复制
    ant -f /home/svetlana/NetBeansProjects/Chapter7 jfxsa-run
init:
Deleting: /home/svetlana/NetBeansProjects/Chapter7/build/built-jar.properties
deps-jar:
Updating property file: /home/svetlana/NetBeansProjects/Chapter7/build/built-jar.properties
Compiling 1 source file to /home/svetlana/NetBeansProjects/Chapter7/build/classes
compile:
Detected JavaFX Ant API version 1.3
Launching <fx:jar> task from /usr/lib/jvm/java-8-oracle/jre/../lib/ant-javafx.jar
Warning: From JDK7u25 the Codebase manifest attribute should be used to restrict JAR repurposing.
         Please set manifest.custom.codebase property to override the current default non-secure value '*'.
Launching <fx:deploy> task from /usr/lib/jvm/java-8-oracle/jre/../lib/ant-javafx.jar
No base JDK. Package will use system JRE.
No base JDK. Package will use system JRE.
jfx-deployment-script:
jfx-deployment:
jar:
Copying 12 files to /home/svetlana/NetBeansProjects/Chapter7/dist/run1372567593
jfx-project-run:
Executing /home/svetlana/NetBeansProjects/Chapter7/dist/run1372567593/Chapter7.jar using platform /usr/lib/jvm/java-8-oracle/jre/bin/java
Deleting directory /home/svetlana/NetBeansProjects/Chapter7/dist/run1372567593
jfxsa-run:
BUILD SUCCESSFUL (total time: 6 seconds)

如果我可以直接从浏览器访问,我就可以访问图片。

EN

回答 1

Stack Overflow用户

发布于 2017-04-12 04:16:08

为什么here. .。

代码语言:javascript
复制
@Override
public void start(Stage primaryStage)
{
    try {
        Image butterfly = createImage("http://pngimg.com/uploads/butterfly/butterfly_PNG1024.png");
        ImageView imageView = new ImageView();

        imageView.setImage(butterfly);
        imageView.setFitWidth(300);
        imageView.setPreserveRatio(true);


        StackPane root = new StackPane();
        root.getChildren().add(imageView);

        primaryStage.setScene(new Scene(root, 300, 250));
        primaryStage.show();
    }
    catch (IOException ex) {
        Logger.getLogger(JavaFXApplication72.class.getName()).log(Level.SEVERE, null, ex);
    }
}

/**
 * @param args the command line arguments
 */
public static void main(String[] args)
{
    launch(args);
}

Image createImage(String url) throws IOException 
{
    URLConnection conn = new URL(url).openConnection();
    conn.setRequestProperty("User-Agent", "Wget/1.13.4 (linux-gnu)");

    try (InputStream stream = conn.getInputStream()) {
        return new Image(stream);
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43355282

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档