Java 使用SceneBuilder显示图像
我用SceneBuilder、3个按钮和2个ImageView创建了一个小FXML文件 我想做的是:Java 使用SceneBuilder显示图像,java,javafx,scenebuilder,Java,Javafx,Scenebuilder,我用SceneBuilder、3个按钮和2个ImageView创建了一个小FXML文件 我想做的是: 运行应用程序并在开始时显示2个图像 按下NEXT按钮时,显示另外两幅图像 我的问题不是切换图像,而是将图像显示为场景生成器创建的ImageView 这是我的控制器类: public class Controller { private Button Next; //1st button private Button J2inc; //2nd button private
NEXT
按钮时,显示另外两幅图像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
}
}
和我的启动方法:
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();
}
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
}
}
我不知道如何初始化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" />
要在控制器中对其进行初始化,请通过注释@FXML
使变量可访问,然后在控制器的初始化()方法中对其进行初始化:
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();
}
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
属性提供一个值。在场景生成器中,可以选择ImageView
并在右上角的“图像”字段(在“属性”下)中键入图像的URL。请务必阅读以了解URL的字符串表示形式是如何解释的。什么是“添加FXML失败”的意思?如果您需要知道如何格式化代码,请阅读。另外请注意,如果您使用,它将帮助人们阅读您的Java代码(并且它将被正确地突出显示)。是的,这就是我的意思,我将再次阅读。@Bouji如果它是您问题的解决方案,您应该检查答案是否正确。