没有回应。在场景之间切换JavaFX。

时间:2015-11-09 12:16:37

标签: java javafx responsive-design fxml scene

我正在尝试在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 (responsive)FXML2 (not responsive)的图片。

有人可以解释我为什么会这样吗?

2 个答案:

答案 0 :(得分:0)

您必须指定哪个控制器负责处理fxml元素的操作。在第一个fxml示例中,您有fx:controller="application.fxmlTESTcontroller",这基本上是说这个控制器类应该处理在此元素下xml树上发生的所有事情。您在第二个fxml中没有这样的行,因此您必须指定哪个控制器负责所采取的操作。因此,您需要在第二个fxml文件中使用类似的行。

此外,当按下按钮时,您必须指定应该处理操作的方法。同样,在第一个文件中你有onAction="#changeSceneMethod",这就是说按下按钮时,应该调用changeSceneMethod。但是,您在第二个fxml中没有此类属性,并且您需要添加一个属性,因为当前没有任何内容正在侦听此按钮上的事件。

答案 1 :(得分:0)

change.layout();

change.requestLayout();
更新子列表后