在JavaFX和Scenebuilder中创建新的ImageView

我将再尝试一次...我是Scenebuilder的新手,我正试图为我的项目制作一个照相馆!我已经添加了我想要的东西,那就是一个带有从FileChooser中选择的图像的ImageView。但是现在我想获得一条建议,关于如何保存该图像并在每次按下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>
iCMS 回答:在JavaFX和Scenebuilder中创建新的ImageView

好吧,因为您说单个图像版本有效,现在您想添加一个新图像。抓住您的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);

这可能有效,但是由于您没有提供足够的代码,所以我无法对其进行测试。

,

您在事件处理程序中创建了一个新的"/AlloyTest/\(imageName)",但是您从不对其进行任何操作,因此它将被丢弃。

请注意,两个ImageView具有相同的变量名:您创建(并丢弃)的变量的作用域为ImageView块,因此在该块外部引用的变量是您定义的变量在FXML文件中。

您的代码也是如此

if

您想要做的(我想是因为您没有很清楚地说明所需的行为)是将新的图像视图添加到平铺窗格中:

@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文件中的图像视图:

@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);
        }
    });
}
本文链接:https://www.f2er.com/2268052.html

大家都在问