【发布时间】:2019-08-03 18:59:12
【问题描述】:
我在 javafx 中有一个窗口,其中有一个默认图像,
现在我添加了一个fileChooser 来设置一个新的文件路径,在图像视图中设置一个新图像。
eclips 抛出一个java.io.FileNotFoundException
这是我的代码:
private void right(BorderPane layout) throws FileNotFoundException {
image = new Image(new FileInputStream("C:\\Users\\itayz\\eclipse-workspace\\Lab2\\carexample.jpg"));
imageView = new ImageView(image);
imageView.setX(70);
imageView.setY(55);
imageView.setFitHeight(355);
imageView.setFitWidth(300);
Stage fileStage = new Stage();
FileChooser fileChooser = new FileChooser();
Button button = new Button("select file");
Scene scene = new Scene(button, 960, 600);
fileStage.setScene(scene);
layout.setRight(combine(button, imageView));
button.setOnAction(e -> {
File selectedFile = fileChooser.showOpenDialog(carStage);
try {
image = new Image(new FileInputStream(selectedFile.toURI().toURL().toExternalForm()));
imageView.setImage(image);
System.out.println(selectedFile.toURI().toURL().toExternalForm().toString());
} catch (FileNotFoundException | MalformedURLException e1) {
e1.printStackTrace();
}
});
}
这是错误:
java.io.FileNotFoundException: file:\C:\Users\itayz\OneDrive\תמונות\20181216_204014.jpg (The filename, directory name, or volume label syntax is incorrect)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at CarPane2.lambda$0(CarPane2.java:60)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8411)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:394)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$353(GlassViewEventHandler.java:432)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
at java.lang.Thread.run(Unknown Source)
【问题讨论】:
-
尝试使用不同的 jpg 名称。可能是编码问题
-
“文件名、目录名或卷标语法不正确” - 这就是它告诉您的错误,所以...您的图像名称似乎被解码为一些符号。尝试重命名此图像并避免出现异常字符。
-
我也更改了文件名和路径:同样的错误:
java.io.FileNotFoundException: file:\C:\Users\itayz\idpic.jpg (The filename, directory name, or volume label syntax is incorrect) -
如果你有一个
File并且想要一个FileInputStream,为什么要使用这个url?new FileInputStream(selectedFile)会简单得多。如果将字符串传递给构造函数,则它必须是文件路径,而不是 url。如果您使用的是 url,则应将其直接传递给Image构造函数,该构造函数确实需要一个 url。
标签: java image user-interface javafx imageview