当前位置: 首页 > 知识库问答 >
问题:

在JavaFX和Scenebuilder中创建新的ImageView

戴霖
2023-03-14

我打算再试一次。。。我是Scenebuilder的新手,我正在尝试为我的项目创建一个照片库!我已经添加了我想要的,那就是一个图像视图,其中的图像是从FileChooser中选择的。。。但是现在我想得到一个建议,如何保存这个,并在每次按下addPhoto按钮时创建一个新的,而不是覆盖我在ImageView中已经有的。以下是我的addPhoto按钮代码:

@FXML

public void initialize(ActionEvent e) throws Exception{


        addPhotos.setOnAction(event -> {
            FileChooser chooser = new FileChooser();
            File file = chooser.showOpenDialog(null);
           pic = new Image(file.toURI().toString());
           if(pic != null) {
             ImageView  imgView = new ImageView(pic);

           }

             imgView.setImage(pic);

    });

FXML代码

    <BorderPane prefHeight="737.0" prefWidth="934.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="legioDesk.GalleryController">
   <top>
      <Button fx:id="addPhotos" mnemonicParsing="false" onAction="#initialize" text="addPhotos" BorderPane.alignment="CENTER" />
   </top>
   <center>
      <TilePane fx:id="tp" prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER">
         <children>
            <ImageView fx:id="imgView" fitHeight="306.0" fitWidth="378.0" pickOnBounds="true" preserveRatio="true" />
         </children>
      </TilePane>
   </center>
</BorderPane>

共有2个答案

闻人花蜂
2023-03-14

好吧,既然你说单一图像版本有效,现在你想添加一个新图像。抓住你的TilePane,得到孩子,然后添加你的图像视图。

pic = new Image(file.toURI().toString());
if(pic != null) {
    ImageView  nextView = new ImageView(pic);
    tp.getChildren().add(nextView);
}
//delete this it is changing the original one.
//imgView.setImage(pic);

这可能可行,但我无法测试它,因为您没有提供足够的代码

郏扬
2023-03-14

您在事件处理程序中创建了一个新的图像视图,但您从未对其进行任何操作,因此它被丢弃。

请注意,两个ImageView都有相同的变量名:您创建(和放弃)的变量名的作用域是if块,因此您在块外引用的变量名是您在FXML文件中定义的变量名。

所以你的代码可以

@FXML

public void initialize(ActionEvent e) throws Exception{


    addPhotos.setOnAction(event -> {
        FileChooser chooser = new FileChooser();
        File file = chooser.showOpenDialog(null);
        pic = new Image(file.toURI().toString());
        if(pic != null) {
           // Create a new image view, containing the selected image
           // (but do nothing with it)
           ImageView  imgView = new ImageView(pic);

        }
        // now update the existing ImageView (from the FXML file) with
        // the chosen image:
        imgView.setImage(pic);

    });
}

您想要做的(我猜,因为您没有非常清楚地解释所需的行为)是将新的图像视图添加到磁贴窗格中:

@FXML

public void initialize(ActionEvent e) throws Exception{


    addPhotos.setOnAction(event -> {
        FileChooser chooser = new FileChooser();
        File file = chooser.showOpenDialog(null);
        pic = new Image(file.toURI().toString());
        if(pic != null) {
           ImageView  imgView = new ImageView(pic);
           imgView.setFitWidth(306);
           imgView.setFitHeight(378);
           imgView.setPreserveRatio(true);
           tp.getChildren().add(imgView);
        }
    });
}

当然,您不需要FXML文件中的图像视图:

<BorderPane prefHeight="737.0" prefWidth="934.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="legioDesk.GalleryController">
   <top>
      <Button fx:id="addPhotos" mnemonicParsing="false" onAction="#initialize" text="addPhotos" BorderPane.alignment="CENTER" />
   </top>
   <center>
      <TilePane fx:id="tp" prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER">
      </TilePane>
   </center>
</BorderPane>
 类似资料:
  • 我对javafx和场景构建器非常陌生。当单击按钮时,我试图创建一个新标签。以下是按钮创建标签的方法: 但它不起作用:(是否因为我没有在fxml中创建lbl1?是否可以在java代码中创建标签而不在fxml中创建?请帮助我!

  • JavaFX中存在控件ToggleGroup。我已经安装了,版本: 在中找不到此控件: 谢谢你的帮助!

  • 我正在使用IDEA(与OpenJDK 11一起使用),并且我正在尝试使用SceneBuilder来显示我的FXML文件。它可以工作,除非我使用继承的JavaFX组件,例如: 我总是得到一个错误:

  • 嗨,我有javafx tableview问题。我试过解决办法,但没能解决这个问题。有行,可以在行之间切换,但数据没有显示。我不明白问题出在哪里。你能帮我一下吗?

  • 所以我要做的是,我要编写一个GUI来在LineChart上显示实时数据。到目前为止,我可以让linechart工作,但不能进入GUI。 使用scenebuilder,我制作了一个带有linechart对象的视图,以便将其链接到我生成的图表。但由于某些原因,这似乎不能与我的MainApp中的代码一起工作。 这只显示了带有空线程的视图。我知道图表应该知道然而,因为我可以使用它来创建一个场景,并显示到G