JavaFx - 显示图像循环

时间:2016-03-05 09:53:23

标签: java javafx

我在墙上。我试图显示一个简单的图像数组

我收到了网址错误...我在包dir和其他位置的图像都没有成功。

关于我做错了什么的任何想法。在此测试之后,我将此应用程序转换为时间轴动画

package javafxapplication4;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Pos;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;


public class JavaFXApplication4 extends Application {

    @Override
    public void start(Stage stage) throws Exception {Parent root =       

    FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));

        // Create a pane.
        VBox vBox = new VBox();
        vBox.setAlignment(Pos.CENTER);

         // set Image
        Image[] imageList = new Image[5];

        imageList[0] = new   

     Image("C:...\src\\javafxapplication4\\1.jpg");
            imageList[1] = new   
     Image("C:...JavaFXApplication4\\src\\javafxapplication4\\2.jpg");
            imageList[2] = new 
     Image("C:...JavaFXApplication4\\src\\javafxapplication4\\3.jpg");
            imageList[3] = new 
     Image("C:...JavaFXApplication4\\src\\javafxapplication4\\4.jpg");
            imageList[4] = new 
     Image("C:..JavaFXApplication4\\src\\javafxapplication4\\5.jpg");
        for (int n = 0; n > 0; n++) {

            vBox.getChildren().add(new ImageView(imageList[n]));

        }
        // Set the stage and show.
        Scene scene = new Scene(vBox);
        stage.setTitle("Image display");
        stage.setScene(scene);
        stage.show();

    }

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

}

控制台输出

Executing   

c:...\NetBeansProjects\JavaFXApplication4\
dist\run589201299\
JavaFXApplication4.jar using platform C:\Program
files\Java\jdk1.7.0_80\jre/bin/java Exception in Application  start   method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
atsun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.ja57)
at sun.reflect.DelegatingMethodAccessorImpl.
invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.javafx.main.Main.launchApp(Main.java:698)
at com.javafx.main.Main.main(Main.java:871)
Caused by: java.lang.RuntimeException: 
Exception in Application start method
at 
com.sun.javafx.application.LauncherImpl.launchApplication1 
      (LauncherImpl.java:403)
at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:47)
at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:115)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalArgumentException: 
Invalid URL: unknown     protocol: c
at javafx.scene.image.Image.validateUrl(Image.java:993)
at javafx.scene.image.Image.<init>(Image.java:538)
at javafxapplication4.JavaFXApplication4.start(JavaFXApplication4.java:35)
at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:319)
at com.sun.javafx.application.PlatformImpl$5.run(PlatformImpl.java:219)
at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:182)
at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:179)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:179)
at    com.sun.glass.ui.InvokeLaterDispatcher$Future.run
(InvokeLaterDispatcher.java:76)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:17)
at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:67)
... 1 more
Caused by: java.net.MalformedURLException: unknown protocol: c
at java.net.URL.<init>(URL.java:596)
at java.net.URL.<init>(URL.java:486)
at java.net.URL.<init>(URL.java:435)
at javafx.scene.image.Image.validateUrl(Image.java:988)

1 个答案:

答案 0 :(得分:0)

Image(String) constructor需要网址,而不是文件路径。 URL包括协议(在这种情况下为file:)并且需要对某些字符进行特殊编码。将文件名转换为URL的最简单方法是使用File类:

String fileName = ...
String fileURL = new File(fileName).toURI().toURL().toExternalForm();

Image img = new Image(fileURL);

如果您的图像在类路径中,您也可以使用ClassLoader获取资源URL(相对于类路径的根)或相对于所使用的类,如果您使用{{3} }:

String fileURL = JavaFXApplication4.class.getResource("2.jpg").toExternalForm();

String fileURL = JavaFXApplication4.class.getResource("/javafxapplication4/2.jpg").toExternalForm();

但要小心。如果未正确指定路径或未在类路径中包含资源,URL返回的getResource将为null,从而产生NPE。