簡體   English   中英

圖像不顯示在JavaFx中

[英]Image does not display in JavaFx

我想創建一個像4個圖片1個單詞的應用程序,但圖像不顯示。 我也使用了SceneBuilder。 我在SceneBuilder中插入了圖像並且預覽很好,但是當我運行主文件時它沒有顯示。 我嘗試使用窗格並使用.getChildren()添加ImageView,但是我無法包含FXML文件。 所以任何幫助將不勝感激,謝謝。

我運行主文件時程序的屏幕截圖

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>

我發現要在fxml文件中顯示JavaFX應用程序中的圖片,你必須在圖片名稱前加上file:並使用相對路徑,例如:

文件:Apple.jpg

在SceneBuilder中“特定”ImageView部分的Image字段中。

這將在您的應用程序中顯示圖像,但會在SceneBuilder屏幕中將其替換為問號(圖像未找到)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM