如何处理javafx中的多个按钮

F.M*_*F.M 1 java javafx fxml

我的 fxml 文件上有多个按钮。onAction通过单击任何按钮,将显示定义的 fxml 表单。问题是:

我是否应该使用例如 foreach 加载所有表单,并且当按钮单击正确的表单时仅显示或没有必要?

如果我应该加载所有表单,我应该在initialize函数中执行它吗?

我是新手JavaFx,不知道最好的方法是什么?

编辑

例如,当 btn1 单击时,将显示“添加用户”表单,当单击“btn2”时,将显示“删除用户”表单,...

问题是:

我是否应该加载添加用户表单并删除用户表单,...当程序启动时以及当例如 btn1 单击添加用户表单时是否显示?

<BorderPane fx:controller="com.project.controller.eventcontroller" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
   <left>
      <VBox  prefHeight="400.0" prefWidth="110.0" style="-fx-background-color: #404040;" BorderPane.alignment="CENTER">
         <children>
            <Button fx:id="btn1" alignment="TOP_LEFT" mnemonicParsing="false" prefHeight="17.0" prefWidth="111.0" text="btn1" />
            <Button fx:id="btn2" alignment="TOP_LEFT" layoutX="10.0" layoutY="10.0" mnemonicParsing="false" prefHeight="17.0" prefWidth="111.0" text="btn2">
               <VBox.margin>
                  <Insets top="12.0" />
               </VBox.margin>
            </Button>
            <Button fx:id="btn3" alignment="TOP_LEFT" layoutX="10.0" layoutY="47.0" mnemonicParsing="false" prefHeight="17.0" prefWidth="111.0" text="btn3">
               <VBox.margin>
                  <Insets top="12.0" />
               </VBox.margin>
            </Button>
            <Button fx:id="btn4" alignment="TOP_LEFT" layoutX="10.0" layoutY="84.0" mnemonicParsing="false" prefHeight="17.0" prefWidth="111.0" text="btn4">
               <VBox.margin>
                  <Insets top="12.0" />
               </VBox.margin>
            </Button>
         </children>
      </VBox>
   </left>
</BorderPane>
Run Code Online (Sandbox Code Playgroud)

感谢大家的帮助

jew*_*sea 5

推荐方法

我的建议是:

  1. 为每个按钮定义单独的操作处理程序,例如edit()delete()
  2. onAction使用注释将每个处理程序注入到控制器代码中@FXML
  3. 在 FXML 中,为按钮定义相应的onAction="#edit"和属性。onAction="#delete"
  4. 在每个操作处理程序中,加载并显示新的 FXML 以处理操作的 UI。

是的,您可以在其他地方预加载用于编辑和删除操作的 FXML,然后在执行编辑和删除操作时仅显示相应的节点,但我不建议您这样做。

例子

假设:

  1. 您已经定义了 FXML 文件edit.fxmldelete.fxml用于处理编辑和删除功能的 UI。
  2. 您希望在请求操作时将当前场景根替换为操作的 UI。
  3. 您的主应用程序类名为MyApp
  4. FXML 文件作为资源位于与主应用程序类相同的包中。

控制器处理按钮操作的代码片段

@FXML
private Button editButton;

@FXML
private Button deleteButton;

@FXML
private void edit(ActionEvent e) {
    editButton.getScene().setRoot(
        FXMLLoader.load(MyApp.class.getResource("edit.fxml"));
    );  
}

@FXML
private void delete(ActionEvent e) {
    deleteButton.getScene().setRoot(
        FXMLLoader.load(MyApp.class.getResource("delete.fxml"));
    );  
}
Run Code Online (Sandbox Code Playgroud)

用于与控制器交互的 FXML 片段

<Button fx:id="editButton" text="Edit" onAction="#edit" />
<Button fx:id="deleteButton" text="Delete" onAction="#delete" />
Run Code Online (Sandbox Code Playgroud)

参考

有一些将操作处理程序分配给 FXML 中定义的按钮的示例:

共享数据

使用以下任一方法:

或者

例如,您可以使用传递参数技术将要编辑的项目的 ID 传递到编辑控制器。

显示对话框

如果您想显示对话框而不是替换当前场景的内容,请执行以下操作:

@FXML
private void edit(ActionEvent e) {
    Scene editScene = new Scene(
        FXMLLoader.load(MyApp.class.getResource("edit.fxml"));
    );

    Stage owner = (Stage) editButton.getScene().getWindow(); 

    Stage editDialog = new Stage();
    editDialog.initOwner(owner);
    editDialog.initModality(Modality.APPLICATION_MODAL);
    editDialog.setScene(editScene); 
    editDialog.showAndWait();
}
Run Code Online (Sandbox Code Playgroud)

根据需要调整舞台设置。对于此示例,我们创建一个模式对话框。在允许用户再次与应用程序的其余部分交互之前,系统将等待,直到用户完成与对话框的交互并关闭对话框。

上面的代码使用了标准Stage,但您也可以使用Dialog类或子类来代替。该DialogAPI 稍微复杂一些,使用起来也比较困难,这里我不演示它的用法。

不相关的建议

  • 当您为事物分配 ID 值时,请使其有意义(而不是btn1btn2btn3等)。

  • 您不需要fx:id为未注入控制器的项目分配值(如果您认为这使您的应用程序更易于理解,您仍然可以这样做)。

  • 为您的控制器分配一个有意义的名称和正确的大小写。例如, not com.project.controller.eventcontroller、 but 相反com.project.ActionMenu,或其他一些适当的名称,具体取决于应用程序域中所需的功能。

  • 尽可能使用带有布局提示的布局窗格,而不是硬编码首选尺寸和绝对布局定位。