【问题标题】:why it doesn't add image to flow pane?为什么它不将图像添加到流窗格?
【发布时间】:2017-06-20 03:02:35
【问题描述】:

我有打击代码,但它不起作用我不明白这是怎么回事?

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 中的所有节点,但它不添加流窗格或可能不显示它!

【问题讨论】:

  • 会抛出异常吗?
  • File file = new File("sample/image/2.jpg"); System.out.println(file.exists()); 可能会输出 false。

标签: java javafx flowpane


【解决方案1】:

我尝试了您的代码并且它有效:请验证图像位置,也许图像位置有问题。 试试这个:

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);
  }

}

【讨论】:

    猜你喜欢
    • 2014-10-12
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    • 1970-01-01
    • 2011-10-06
    • 2015-02-26
    • 1970-01-01
    相关资源
    最近更新 更多