我用SceneBuilder,3个按钮和2个ImageView创建了一个小的FXML文件。
我想做的是:
NEXT
按钮时,显示2张其他图像。我的问题不是用于切换图像,而是将其显示为由场景构建器创建的ImageView。
这是我的Controller类:
public class Controller {
private Button Next; //1st button
private Button J2inc; //2nd button
private Button J1inc; /3rd button
private ImageView Img1;
private ImageView Img2;
void Inc2(ActionEvent event) {
//nothing for the moment
}
void Inc1(ActionEvent event) {
//nothing for the moment
}
void Nextimg(ActionEvent event) {
//nothing for the moment
}
}
我的start
方法:
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("Css.fxml"));
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.setTitle("P ");
primaryStage.show();
}
我不知道如何初始化ImageView img1
,因此它会加载一些东西。
无法在此处添加FXML,因此我只会添加ImageView行:
<ImageView fx:id="Img1" fitHeight="750.0" fitWidth="450.0" layoutY="22.0" pickOnBounds="true" preserveRatio="true" AnchorPane.bottomAnchor="50.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="450.0" AnchorPane.topAnchor="25.0" />
答案 0 :(得分:1)
要在控制器中对其进行初始化,可以通过对@FXML
进行注释来使变量可访问,并在控制器的initialize()
方法中对其进行初始化:
public class Controller {
private Button Next; //1st button
private Button J2inc; //2nd button
private Button J1inc; /3rd button
@FXML
private ImageView Img1;
private ImageView Img2;
public void initialize() {
Img1.setImage(new Image(...));
}
void Inc2(ActionEvent event) {
//nothing for the moment
}
void Inc1(ActionEvent event) {
//nothing for the moment
}
void Nextimg(ActionEvent event) {
//nothing for the moment
}
}
如果您希望直接在FXML中初始化它,请为image
属性提供值。在Scene Builder中,您可以选择ImageView
并在右上角的“图像”字段中键入图像的URL(在“属性”下)。请务必阅读{{3}},了解如何解释URL的字符串表示。