我正在阅读“JavaFX快速入门指南”一书,我正在阅读第7章,由于某些原因,我的图片无法加载
这是我的代码:
/*
* 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为我提供的错误
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)
如果我可以直接从浏览器访问,我可以访问该图片。
答案 0 :(得分:0)
here。
的原因@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);
}
}