2017-06-20 41 views
1

我有吹碼,它不起作用,我不明白什麼是問題?爲什麼它不會將圖像添加到流程窗格?

FlowPane flowPane = new FlowPane(); 
System.out.println("log:brows song"); 
for (int i = 0; i < 10; i++) { 
    Image image = new Image(new File("sample/image/2.jpg").toURI().toString()); 
    ImageView imageView = new ImageView(image); 
    flowPane.getChildren().add(imageView); 
} 
mainAnchorePaneArtist.getChildren().clear(); 

mainAnchorePaneArtist.getChildren().add(flowPane); 

它清除mainAnchorePaneArtist中的所有節點,但它不會添加流窗格或可能不顯示它!

+0

它會拋出任何異常嗎? – c0der

+0

'File file = new File(「sample/image/2.jpg」); \t System.out.println(file.exists());'可能會輸出false。 – c0der

回答

2

我試着用你的代碼,它的工作原理:請驗證圖像位置,可能圖像位置有問題。 試試這個:

public class MainClass extends Application { 

    @Override 
    public void start(Stage primaryStage) { 
     try { 
       AnchorPane group = new AnchorPane(); 
       Scene scene = new Scene(group ,600, 300); 
       primaryStage.setTitle("Sample Application"); 
       primaryStage.setScene(scene); 
       FlowPane flowPane = new FlowPane(); 
       System.out.println("log:brows song"); 
       for (int i = 0; i < 10; i++) { 
        Image image = new Image(new File("2.jpg").toURI().toString()); 
        ImageView imageView = new ImageView(image); 
        flowPane.getChildren().add(imageView); 
       } 
       group.getChildren().clear(); 

       group.getChildren().add(flowPane); 
       primaryStage.show(); 
     } catch(Exception e) { 
      e.printStackTrace(); 
     } 
    } 
    public static void main(String[] args) { 
     launch(args); 
    } 

} 
相關問題