2017-03-19 174 views
1

我想创建一个像4个图片1个字的应用程序,但图像不显示。我也使用了SceneBuilder。我在SceneBuilder中插入图像,预览很好,但是当我运行主文件时,它不显示。我尝试使用窗格并添加ImageView使用.getChildren()和工作,但我不能包括FXML文件。所以,任何帮助将不胜感激,谢谢。图片不显示在JavaFx

Screenshot of the program when I run the main file

Screenshot of the preview from SceneBuilder

public class Main extends Application { 

@FXML 
private ImageView apple; 
//Window 
@Override 
public void start(Stage primaryStage) throws Exception{ 
    try 
    { 
     Parent root = FXMLLoader.load(getClass().getResource("sample.fxml")); 
     Scene scene = new Scene(root, 546, 600); 
     primaryStage.setTitle("4 Pics 1 Word"); 
     primaryStage.setResizable(false); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 
    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 
    } 
    //Time display 


} 


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

编辑1:为ImageView的FXML代码:

<ImageView fx:id="apple" fitHeight="360.0" fitWidth="430.0" layoutX="53.0" layoutY="30.0" pickOnBounds="true"> 
    <image> 
     <Image url="@../../Apple.jpg" /> 
    </image> 
    </ImageView> 
+0

我想你的程序只是没有找到图像,但没有看到(缩短)的FXML文件,它不可能帮助你。 – mipa

+0

对不起。我现在将添加它 – Djoker

回答

0

我发现,从FXML文件显示在您的JavaFX应用程序的图片,你必须在图片名称前加file:并使用相对路径,如:

文件:Apple.jpg

在SceneBuilder “特定” 的ImageView部的像场。

这将在您的应用程序中显示图像,但会在SceneBuilder屏幕中用问号(图像未找到)替换它。

+0

谢谢您的回复,并对最近的回复感到抱歉,我一直忙于工作。我尝试使用文件:在路径之前,SceneBuilder屏幕被替换为您所说的问号,但应用程序保持不变。 :( – Djoker

+1

如果您正在使用Eclipse,请务必在SceneBuilder中进行更改后更新您的项目环境。 – Damien