[英]Not responsive. Changing between scenes JavaFX.
I am trying to change my FXML files in one Stage, in borderPane>CENTER. 我正在尝试在borderPane> CENTER中的一个阶段中更改FXML文件。 It is working but the second FXML file is not responsive.
它正在工作,但是第二个FXML文件没有响应。 Could someone help ?
有人可以帮忙吗?
Here are my files. 这是我的文件。
Main.java (only start method) 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();
}
}
My controller. 我的控制器。
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 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>
and FXML 2 和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>
And here are the pictures of my FXML1 (responsive) and FXML2 (not responsive) after I run the app. 这是运行该应用程序后我的FXML1(响应)和FXML2(不响应)的图片。
Could someone explain me why is that ? 有人可以解释一下为什么吗?
You have to specify which controller is responsible for handling actions on your fxml
elements. 您必须指定哪个控制器负责处理
fxml
元素上的操作。 In the first fxml
sample, you have fx:controller="application.fxmlTESTcontroller"
, which is basically saying that this controller class should take care of everything that is happening down on the xml
tree under this element. 在第一个
fxml
示例中,您具有fx:controller="application.fxmlTESTcontroller"
,这基本上是说该控制器类应处理该元素下xml
树上正在发生的所有事情。 You don't have such line in the second fxml
so you have to specify which controller is responsible for the actions taken. 您在第二个
fxml
没有这样的行,因此您必须指定哪个控制器负责所执行的操作。 Therefore you need a similar line in the second fxml
file. 因此,在第二个
fxml
文件中需要类似的行。
Moreover, you have to specify which method should take care of the action, when a button is pressed. 此外,您必须指定在按下按钮时应该采取哪种方法进行操作。 Again, in the first file you have
onAction="#changeSceneMethod"
, which is saying that whenever the button is pressed, the changeSceneMethod
should be invoked. 同样,在第一个文件中,您具有
onAction="#changeSceneMethod"
,它表示每当按下按钮时,都应调用changeSceneMethod
。 However, you don't have such attribute in the second fxml
and you need to add one, since nothing is currently listening for events on this button. 但是,第二个
fxml
没有这样的属性,您需要添加一个,因为当前没有人在监听此按钮上的事件。
try 尝试
change.layout();
or 要么
change.requestLayout();
after updating children list. 在更新子列表之后。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.