简体   繁体   English

为什么不使用SceneBuilder和Eclipse使用FXML for JavaFX来显示图像?

[英]Why won't an image show using FXML for JavaFX using Scenebuilder and Eclipse?

I've built a simple GUI using Scenebuilder with several buttons. 我使用带有几个按钮的Scenebuilder构建了一个简单的GUI。 In SceneBuilder, everything shows up fine. 在SceneBuilder中,一切显示正常。

But when I run the program, in all the buttons except for one, all of their ImageViewers' images display properly. 但是,当我运行该程序时,除一个按钮外,所有按钮中的所有ImageViewer图像均正确显示。 I encountered this issue before, and after cleaning the project, things showed up, but not this time. 在清理项目之前和之后,我都遇到了此问题,但是事情没有出现。 I use document references to point to the images, and all the images used are present, which is that they are all in the same directory with the fxml and controller class. 我使用文档引用来指向这些图像,并且所有使用的图像都存在,这是因为它们都与fxml和controller类位于同一目录中。

Any idea what could be the cause behind the issue? 任何想法可能是问题背后的原因吗?

Edit: Here's the (shortened) fxml code 编辑:这是(缩短的)fxml代码

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.image.*?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.VBox?>

<fx:root onMouseDragged="#onMouseDrag" onMouseEntered="#onMouseEnter" onMouseExited="#onMouseExit" prefHeight="500.0" prefWidth="600.0" type="VBox" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <ToolBar onMouseDragged="#onMouseDrag" onMouseEntered="#onMouseEnter" onMouseExited="#onMouseExit" prefHeight="0.0" prefWidth="376.0">
        <items>
            <Button fx:id="closeButton" mnemonicParsing="false" onAction="#closeWindow">
               <graphic>
                  <ImageView fitHeight="40.0" fitWidth="40.0" pickOnBounds="true" smooth="false">
                     <image>
                        <Image url="@images/close-32.png" />
                     </image>
                  </ImageView>
               </graphic>
            </Button>
        </items>
      </ToolBar>
   </children>
</fx:root>

Here's the simple controller. 这是简单的控制器。 I explicitly call setup on the the hosting Stage 我在托管阶段明确调用了setup

public LogViewerControllerFX() {
    Platform.setImplicitExit(false);
}

public void setup() {
    FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource(
            "LogViewerFX.fxml"));

    fxmlLoader.setRoot(this);
    fxmlLoader.setController(this);

    try {
        fxmlLoader.load();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Here's the Stage: 这是舞台:

import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.paint.Color;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

public class LogViewerDialogFX extends Stage {

    public LogViewerDialogFX() {
        LogViewerControllerFX logViewController = new LogViewerControllerFX();
        logViewController.setup();  

        Scene scene = new Scene(logViewController);
        scene.setFill(Color.TRANSPARENT);
        setScene(scene);
        initStyle(StageStyle.TRANSPARENT);
        initModality(Modality.WINDOW_MODAL);
        setTitle(I18N.getText("title.logViewer"));
        getIcons().add(new Image("net/logviewer/image/logs-25.png"));
    }
}

This all happens within the JavaFX thread, of course. 当然,所有这些都在JavaFX线程内发生。

Maybe try an rebuild of your eclipse project. 也许尝试重建您的Eclipse项目。 Or try to set the Image over css. 或尝试通过CSS设置图片。

<Button styleClass="Button-Image"/>

.Button-Image{
    -fx-graphic: url('images/close-32.png');
}

Correct me if I am wrong but please take a look here: 如果我错了请指正我,但请在这里看看:

<Image url="@images/close-32.png" />

Refers to an image in '/images/' folder. 指“ / images /”文件夹中的图像。 However: 然而:

getIcons().add(new Image("net/logviewer/image/logs-25.png"));

Refers to an image in '/image/' folder. 引用“ / image /”文件夹中的图像。 As you can see, they both refers to different folder. 如您所见,它们都引用不同的文件夹。 You may want to check which one is the right one, 'image/' or 'images/' folder. 您可能需要检查哪个是正确的文件夹,即“ image /”或“ images /”文件夹。

I'm not sure if this is what constitutes a good answer or not, but I got it working by redoing the references to all images involved (they turned out the same on the fxml, however), rebooted Eclipse and SceneBuilder, and perfomed a complete clean of all projects. 我不确定这是否是一个好的答案,但是我通过重做对所有涉及到的映像的引用(但是在fxml上它们却相同),重新启动了Eclipse和SceneBuilder,并执行了彻底清理所有项目。

Like I noticed before, only after cleaning do changes in SceneBuilder become usable in Eclipse. 就像我之前注意到的那样,只有清理后,SceneBuilder中的更改才可以在Eclipse中使用。 I'm not sure if it's the e(fx)clipse plugin, or how Eclipse treats fxml files, but there's definitely something in need of improvement in that particular process. 我不确定这是e(fx)clipse插件还是Eclipse如何处理fxml文件,但是在该特定过程中肯定需要改进。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM