我的解决方案为这个简单的行动。我使用了一个控制器和容器,您可以在其中动态更改内容。在这个解决方案中,我使用一个VBox创建了FXML,并在点击按钮后更改了内容。
我不这么认为,你应该在这种情况下改变场景。当然,就像James_D写的那样,如果你想要这样做,你应该有一个FXML的控制器。
控制器:
public class PictureChange {
@FXML VBox vBox;
@FXML Button button;
@FXML private void initialize() {
ImageView v1 = new ImageView(new Image(this.getClass().getResource("/smallPic.png").toExternalForm()));
ImageView v2 = new ImageView(new Image(this.getClass().getResource("/smallPic.png").toExternalForm()));
ImageView v3 = new ImageView(new Image(this.getClass().getResource("/smallPic.png").toExternalForm()));
vBox.getChildren().addAll(v1, v2, v3);
}
@FXML private void changePictures() {
Image bigImage = new Image(this.getClass().getResource("/bigPic.png").toExternalForm());
ImageView vBig = new ImageView(bigImage);
vBox.getChildren().clear();
vBox.getChildren().add(vBig);
}
}
FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.control.Button?>
<HBox prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="picture.PictureChange">
<VBox fx:id="vBox">
</VBox>
<Button fx:id="button" text="Change Pictures" onAction="#changePictures"/>
</HBox>
每个FXML文件应该有自己的控制器类 –
谢谢你,@James_D。 –
为什么你不能在一个控制器中做?例如,只需在VBox中做所有事情。点击按钮后,清除Vbox,其中3个ImagesView,然后只添加两个。这种变化将是动态的。 – BadVegan