图片网址是如何用Java编写的FXML

问题描述 投票:0回答:2

我与FXML文件工作,我不知道如何将URL(路径)写入一个图像,我有,我想absolote路径和目录路径

请大家帮忙尝试复制该项目,并测试它自己

我得到这样一个无效的URL错误>>

Feb 08, 2019 5:09:27 PM javafx.fxml.FXMLLoader$ValueElement processValue
WARNING: Loading FXML document with JavaFX API of version 11.0.1 by 
JavaFX runtime of version 10.0.2
null/bars-chart.png
Exception in Application start method
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:473)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:372)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:941)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:973)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:198)
at java.base/java.lang.Thread.run(Thread.java:844)
Caused by: javafx.fxml.LoadException: 
unknown path:64

at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2625)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2603)
at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2450)
at back_end.image_main.start(image_main.java:25)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:919)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$11(PlatformImpl.java:449)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$9(PlatformImpl.java:418)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:417)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:175)
... 1 more
Caused by: java.lang.IllegalArgumentException: Invalid URL: Invalid URL or resource not found
at javafx.graphics/javafx.scene.image.Image.validateUrl(Image.java:1107)
at javafx.graphics/javafx.scene.image.Image.<init>(Image.java:688)
at javafx.fxml/com.sun.javafx.fxml.builder.JavaFXImageBuilder.build(JavaFXImageBuilder.java:47)
at javafx.fxml/com.sun.javafx.fxml.builder.JavaFXImageBuilder.build(JavaFXImageBuilder.java:37)
at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:771)
at javafx.fxml/javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2838)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2557)
... 11 more
Caused by: java.lang.IllegalArgumentException: Invalid URL or resource not found
at javafx.graphics/javafx.scene.image.Image.validateUrl(Image.java:1099)
... 17 more
Exception running application back_end.image_main

我的项目的结构

fxml_image :
           src :
                front_end :
                           >(bars-chart.png)
                           >(main.fxml)
                back_end :
                           >(image_main.java)

这是(main.fxml)>>

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.SeparatorMenuItem?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>

<VBox prefHeight="400.0" prefWidth="640.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1">
  <children>
<MenuBar VBox.vgrow="NEVER">
  <menus>
    <Menu mnemonicParsing="false" text="File">
      <items>
        <MenuItem mnemonicParsing="false" text="New" />
        <MenuItem mnemonicParsing="false" text="Open…" />
        <Menu mnemonicParsing="false" text="Open Recent" />
        <SeparatorMenuItem mnemonicParsing="false" />
        <MenuItem mnemonicParsing="false" text="Close" />
        <MenuItem mnemonicParsing="false" text="Save" />
        <MenuItem mnemonicParsing="false" text="Save As…" />
        <MenuItem mnemonicParsing="false" text="Revert" />
        <SeparatorMenuItem mnemonicParsing="false" />
        <MenuItem mnemonicParsing="false" text="Preferences…" />
        <SeparatorMenuItem mnemonicParsing="false" />
        <MenuItem mnemonicParsing="false" text="Quit" />
      </items>
    </Menu>
    <Menu mnemonicParsing="false" text="Edit">
      <items>
        <MenuItem mnemonicParsing="false" text="Undo" />
        <MenuItem mnemonicParsing="false" text="Redo" />
        <SeparatorMenuItem mnemonicParsing="false" />
        <MenuItem mnemonicParsing="false" text="Cut" />
        <MenuItem mnemonicParsing="false" text="Copy" />
        <MenuItem mnemonicParsing="false" text="Paste" />
        <MenuItem mnemonicParsing="false" text="Delete" />
        <SeparatorMenuItem mnemonicParsing="false" />
        <MenuItem mnemonicParsing="false" text="Select All" />
        <MenuItem mnemonicParsing="false" text="Unselect All" />
      </items>
    </Menu>
    <Menu mnemonicParsing="false" text="Help">
      <items>
        <MenuItem mnemonicParsing="false" text="About MyHelloApp" />
      </items>
    </Menu>
  </menus>
</MenuBar>
<AnchorPane maxHeight="-1.0" maxWidth="-1.0" prefHeight="-1.0" prefWidth="-1.0" VBox.vgrow="ALWAYS">
  <children>
    <Label alignment="CENTER" layoutX="155.0" layoutY="177.0" style="&#10;" text="Drag components from Library here…" textAlignment="CENTER" textFill="#9f9f9f" wrapText="false">
      <font>
        <Font size="18.0" />
      </font>
        </Label>
            <ImageView fitHeight="349.0" fitWidth="606.0" layoutX="12.0"         layoutY="11.0" pickOnBounds="true" preserveRatio="true">
               <image>
                  <Image url="@bars-chart.png" />
               </image>
            </ImageView>
      </children>
    </AnchorPane>
  </children>
</VBox>

这是Java文件(image_main)

package back_end;

import java.io.FileInputStream;
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class image_main extends Application  {

    public static void main(String[] args) { Application.launch(args); }

    @Override 
    public void start(Stage stage) throws IOException { 

    // Create the FXMLLoader 
    FXMLLoader loader = new FXMLLoader(); 
    // Path to the FXML File 
    String fxmlDocPath = "src/front_end/main.fxml"; 
    FileInputStream fxmlStream = new FileInputStream(fxmlDocPath);
    // Create the Pane and all Details 
    VBox root = (VBox) loader.load(fxmlStream);
    // Create the Scene 
    Scene scene = new Scene(root); 
    // Set the Scene to the Stage 
    stage.setScene(scene); 
    // Set the Title to the Stage 
    stage.setTitle("A FXML Example which includes FXML Files"); 
    // Display the Stage 
    stage.show();


}}
java eclipse image javafx fxml
2个回答
2
投票

路径是使用FXML文件的位置解决。如果您加载使用InputStream源的FXML数据FXML,FXMLLoader无法确定位置的URL。

指定位置,而不是:

FXMLLoader loader = new FXMLLoader(new File("src/front_end/main.fxml").toURI().toURL());
VBox root = (VBox) loader.load();

假设FXML是一种资源,我建议使用getResource,而不是得到的网址:

FXMLLoader loader = new FXMLLoader(getClass().getResource("/front_end/main.fxml"));

0
投票

我不建议你,包括你的“SRC”文件夹中的图像文件。您应该创建一个名为“资源”的新文件夹,并使其作为源文件夹(选项 - >构建路径 - >用作源文件夹)。把你的图片在“资源”文件夹中。然后做在FXML文件进行以下更改。

<Image url="@../bars-chart.png" />

我希望这会为你工作。

© www.soinside.com 2019 - 2024. All rights reserved.