[英]Not responsive. Changing between scenes JavaFX.
我正在尝试在borderPane> CENTER中的一个阶段中更改FXML文件。 它正在工作,但是第二个FXML文件没有响应。 有人可以帮忙吗?
这是我的文件。
Main.java(仅启动方法)
@Override
public void start(Stage primaryStage) {
try {
Parent root = FXMLLoader.load(getClass().getResource("fxml1.fxml"));
Stage stage = primaryStage;
Scene scene = new Scene(root, 300, 275);
stage.setTitle("FXML Welcome");
stage.setScene(scene);
stage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
我的控制器。
public class fxmlTESTcontroller {
@FXML
private Button changeScene1;
@FXML
private VBox change;
@FXML
void changeSceneMethod(ActionEvent e) throws IOException
{
change.getChildren().clear();
change.getChildren().add(FXMLLoader.load(getClass().getResource("fxml2.fxml")));
}
}
FXML 1
<AnchorPane xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.fxmlTESTcontroller">
<children>
<BorderPane prefHeight="300.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<left>
<ListView maxWidth="200.0" BorderPane.alignment="CENTER" />
</left>
<center>
<VBox fx:id="change" prefHeight="200.0" prefWidth="100.0" BorderPane.alignment="CENTER">
<children>
<GridPane VBox.vgrow="ALWAYS">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Text" />
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Text" GridPane.columnIndex="1" />
<TextField GridPane.columnIndex="1" GridPane.rowIndex="1" />
<TextArea prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="1" GridPane.rowIndex="2" />
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Text" GridPane.rowIndex="1" />
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Text" GridPane.rowIndex="2" />
</children>
</GridPane>
<HBox>
<children>
<Pane HBox.hgrow="ALWAYS" />
<Button fx:id="changeScene1" mnemonicParsing="false" onAction="#changeSceneMethod" text="Button" HBox.hgrow="ALWAYS" />
</children>
</HBox>
</children>
</VBox>
</center>
</BorderPane>
</children>
</AnchorPane>
和FXML 2
<VBox xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" prefHeight="200.0" prefWidth="100.0" BorderPane.alignment="CENTER">
<children>
<GridPane VBox.vgrow="ALWAYS">
<columnConstraints>
<ColumnConstraints hgrow="ALWAYS" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="ALWAYS" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="ALWAYS" />
</rowConstraints>
<children>
<Button mnemonicParsing="false" text="Button" GridPane.columnIndex="1" />
<Label text="Label" />
<Label text="Label" GridPane.rowIndex="1" />
<Label text="Label" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<Label text="Label" GridPane.rowIndex="2" />
<TextArea GridPane.columnIndex="1" GridPane.hgrow="ALWAYS" GridPane.rowIndex="2" GridPane.vgrow="ALWAYS" />
</children>
</GridPane>
<HBox VBox.vgrow="ALWAYS">
<children>
<Pane HBox.hgrow="ALWAYS" />
<Button mnemonicParsing="false" text="Button" HBox.hgrow="ALWAYS" />
</children>
</HBox>
</children>
</VBox>
这是运行该应用程序后我的FXML1(响应)和FXML2(不响应)的图片。
有人可以解释一下为什么吗?
您必须指定哪个控制器负责处理fxml
元素上的操作。 在第一个fxml
示例中,您具有fx:controller="application.fxmlTESTcontroller"
,这基本上是说该控制器类应处理该元素下xml
树上正在发生的所有事情。 您在第二个fxml
没有这样的行,因此您必须指定哪个控制器负责所执行的操作。 因此,在第二个fxml
文件中需要类似的行。
此外,您必须指定在按下按钮时应该采取哪种方法进行操作。 同样,在第一个文件中,您具有onAction="#changeSceneMethod"
,它表示每当按下按钮时,都应调用changeSceneMethod
。 但是,第二个fxml
没有这样的属性,您需要添加一个,因为当前没有人在监听此按钮上的事件。
尝试
change.layout();
要么
change.requestLayout();
在更新子列表之后。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.