开发者社区> 问答> 正文

如何访问JAVAFX / FXML中的深层嵌套按钮

我有 :

  • 1个包含6个手风琴的堆栈窗格
  • 手风琴有fx:id
  • 每个手风琴有1到3个标题窗格
  • 每个带标题的窗格有1个TilePane
  • TilePanes包含按钮。

现在,在控制器类中,我对父堆栈有一个引用:

@FXML
    private StackPane stackpaneOfCategories;

是孩子,手风琴:

ObservableList<Node> stackpaneAccordionsList = stackpaneOfCategories.getChildren();

现在,看到层次结构后,我想为每个按钮单击时提供一个.setOnAction,我尝试通过遍历id来做到这一点,但是我很快意识到这不是最佳解决方案。

如何为手风琴的StackPane中嵌套的按钮设置OnAction?

编辑: 这是FXML代码:

 <?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Accordion?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TitledPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.layout.TilePane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Text?>

<GridPane alignment="CENTER" hgap="10.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="900.0" stylesheets="@style.css" vgap="10.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Lancer">
  <columnConstraints>
      <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
      <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
      <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
      <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
      <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
      <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
      <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
    <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 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 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>
      <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="Payement" textAlignment="CENTER" wrapText="true" GridPane.columnSpan="2" />
      <StackPane fx:id="stackpaneOfCategories" prefHeight="150.0" prefWidth="200.0" GridPane.columnIndex="3" GridPane.columnSpan="6" GridPane.rowIndex="2" GridPane.rowSpan="6">
         <children>
            <Accordion fx:id="categorieBoisson">
              <panes>
                <TitledPane animated="false" text="Boissons Chaudes + Eau">
                     <content>
                        <TilePane hgap="5.0" prefHeight="200.0" prefWidth="200.0" vgap="5.0">
                           <children>
                              <Button mnemonicParsing="false" prefHeight="80.0" prefWidth="80.0" text="Eau 1L" textAlignment="CENTER" wrapText="true" />
                              <Button layoutX="20.0" layoutY="20.0" mnemonicParsing="false" prefHeight="80.0" prefWidth="80.0" text="Cappuccino" textAlignment="CENTER" wrapText="true" />
                              <Button layoutX="105.0" layoutY="20.0" mnemonicParsing="false" prefHeight="80.0" prefWidth="80.0" text="Latté" textAlignment="CENTER" wrapText="true" />
                              <Button layoutX="190.0" layoutY="20.0" mnemonicParsing="false" prefHeight="80.0" prefWidth="80.0" text="Americano" textAlignment="CENTER" wrapText="true" />
                              <Button layoutX="20.0" layoutY="20.0" mnemonicParsing="false" prefHeight="80.0" prefWidth="80.0" text="Espresso" textAlignment="CENTER" wrapText="true" />
                           </children>
                        </TilePane>
                     </content>
                </TitledPane>
                <TitledPane animated="false" text="Canettes">
                     <content>
                        <TilePane hgap="5.0" prefHeight="200.0" prefWidth="200.0" vgap="5.0">
                           <children>
                              <Button mnemonicParsing="false" prefHeight="80.0" prefWidth="80.0" text="Coca Cola 33 CL" textAlignment="CENTER" wrapText="true" />
                              <Button layoutX="20.0" layoutY="20.0" mnemonicParsing="false" prefHeight="80.0" prefWidth="80.0" text="Pepsi 33 CL" textAlignment="CENTER" wrapText="true" />
                              <Button layoutX="105.0" layoutY="20.0" mnemonicParsing="false" prefHeight="80.0" prefWidth="80.0" text="Eau 33CL" textAlignment="CENTER" wrapText="true" />
                           </children>
                        </TilePane>
                     </content>
                </TitledPane>
                <TitledPane animated="false" text="Gazeuses 1L">
                     <content>
                        <TilePane hgap="5.0" prefHeight="200.0" prefWidth="200.0" vgap="5.0">
                           <children>
                              <Button mnemonicParsing="false" prefHeight="80.0" prefWidth="80.0" text="Sprite" textAlignment="CENTER" wrapText="true" />
                              <Button layoutX="20.0" layoutY="20.0" mnemonicParsing="false" prefHeight="80.0" prefWidth="80.0" text="Hamoud Boualem" textAlignment="CENTER" wrapText="true" />
                              <Button layoutX="105.0" layoutY="20.0" mnemonicParsing="false" prefHeight="80.0" prefWidth="80.0" text="Miranda" textAlignment="CENTER" wrapText="true" />
                           </children>
                        </TilePane>
                     </content>
                </TitledPane>
              </panes>
            </Accordion>
            <Accordion fx:id="categorieBurger">
              <panes>
                <TitledPane animated="false" text="Burger M">
                     <content>
                        <TilePane hgap="5.0" prefHeight="200.0" prefWidth="200.0" vgap="5.0">
                           <children>
                              <Button mnemonicParsing="false" prefHeight="80.0" prefWidth="80.0" text="Chicken Burger" textAlignment="CENTER" wrapText="true" />
                              <Button layoutX="20.0" layoutY="20.0" mnemonicParsing="false" prefHeight="80.0" prefWidth="80.0" text="Mexican Burger" textAlignment="CENTER" wrapText="true" />
                           </children>
                        </TilePane>
                     </content>
                </TitledPane>
                <TitledPane animated="false" text="Burger L">
                     <content>
                        <TilePane hgap="5.0" prefHeight="200.0" prefWidth="200.0" vgap="5.0">
                           <children>
                              <Button mnemonicParsing="false" prefHeight="80.0" prefWidth="80.0" text="Chicken Burger" textAlignment="CENTER" wrapText="true" />
                              <Button mnemonicParsing="false" prefHeight="80.0" prefWidth="80.0" text="Mexican Burger" textAlignment="CENTER" wrapText="true" />
                           </children>
                        </TilePane>
                     </content>
                </TitledPane>
              </panes>
            </Accordion>
            <Accordion fx:id="categorieTacos" layoutX="10.0" layoutY="10.0">
               <panes>
                  <TitledPane animated="false" layoutX="10.0" layoutY="452.0" text="Tacos L">
                     <content>
                        <TilePane hgap="5.0" prefHeight="200.0" prefWidth="200.0" vgap="5.0">
                           <children>
                              <Button mnemonicParsing="false" prefHeight="80.0" prefWidth="80.0" text="Poulet a l'ail" textAlignment="CENTER" wrapText="true" />
                              <Button layoutX="20.0" layoutY="20.0" mnemonicParsing="false" prefHeight="80.0" prefWidth="80.0" text="Poulet Special" textAlignment="CENTER" wrapText="true" />
                              <Button layoutX="105.0" layoutY="20.0" mnemonicParsing="false" prefHeight="80.0" prefWidth="80.0" text="Foie" textAlignment="CENTER" wrapText="true" />
                              <Button layoutX="190.0" layoutY="20.0" mnemonicParsing="false" prefHeight="80.0" prefWidth="80.0" text="Mexicano" textAlignment="CENTER" wrapText="true" />
                           </children>
                        </TilePane>
                     </content>
                  </TitledPane>
                  <TitledPane animated="false" layoutX="10.0" layoutY="452.0" text="Tacos XL">
                     <content>
                        <TilePane hgap="5.0" prefHeight="200.0" prefWidth="200.0" vgap="5.0">
                           <children>
                              <Button mnemonicParsing="false" prefHeight="80.0" prefWidth="80.0" text="Poulet a l'ail" textAlignment="CENTER" wrapText="true" />
                              <Button mnemonicParsing="false" prefHeight="80.0" prefWidth="80.0" text="Poulet Special" textAlignment="CENTER" wrapText="true" />
                              <Button mnemonicParsing="false" prefHeight="80.0" prefWidth="80.0" text="Foie" textAlignment="CENTER" wrapText="true" />
                              <Button mnemonicParsing="false" prefHeight="80.0" prefWidth="80.0" text="Mexicano" textAlignment="CENTER" wrapText="true" />
                           </children>
                        </TilePane>
                     </content>
                  </TitledPane>
               </panes>
            </Accordion>
            <Accordion fx:id="categoriePizza" layoutX="10.0" layoutY="10.0">
               <panes>
                  <TitledPane animated="false" layoutX="10.0" layoutY="452.0" text="Pizza M">
                     <content>
                        <TilePane hgap="5.0" prefHeight="200.0" prefWidth="200.0" vgap="5.0">
                           <children>
                              <Button mnemonicParsing="false" prefHeight="80.0" prefWidth="80.0" text="Margarita" textAlignment="CENTER" wrapText="true" />
                              <Button layoutX="20.0" layoutY="20.0" mnemonicParsing="false" prefHeight="80.0" prefWidth="80.0" text="Vegetarienne" textAlignment="CENTER" wrapText="true" />
                              <Button layoutX="105.0" layoutY="20.0" mnemonicParsing="false" prefHeight="80.0" prefWidth="80.0" text="Mexican" textAlignment="CENTER" wrapText="true" />
                              <Button layoutX="190.0" layoutY="20.0" mnemonicParsing="false" prefHeight="80.0" prefWidth="80.0" text="Océan" textAlignment="CENTER" wrapText="true" />
                              <Button layoutX="275.0" layoutY="20.0" mnemonicParsing="false" prefHeight="80.0" prefWidth="80.0" text="Chicken" textAlignment="CENTER" wrapText="true" />
                              <Button layoutX="360.0" layoutY="20.0" mnemonicParsing="false" prefHeight="80.0" prefWidth="80.0" text="4 Cheeses" textAlignment="CENTER" wrapText="true" />
                              <Button layoutX="445.0" layoutY="20.0" mnemonicParsing="false" prefHeight="80.0" prefWidth="80.0" text="444 Special" textAlignment="CENTER" wrapText="true" />
                           </children>
                        </TilePane>
                     </content>
                  </TitledPane>
               </panes>
            </Accordion>
            <Accordion fx:id="categorieBaguette" layoutX="10.0" layoutY="10.0">
               <panes>
                  <TitledPane animated="false" layoutX="10.0" layoutY="452.0" text="Baguettes Farcies M">
                     <content>
                        <TilePane hgap="5.0" prefHeight="200.0" prefWidth="200.0" vgap="5.0">
                           <children>
                              <Button mnemonicParsing="false" prefHeight="80.0" prefWidth="80.0" text="Poulet" textAlignment="CENTER" wrapText="true" />
                              <Button layoutX="20.0" layoutY="20.0" mnemonicParsing="false" prefHeight="80.0" prefWidth="80.0" text="Viande Hachée" textAlignment="CENTER" wrapText="true" />
                              <Button layoutX="105.0" layoutY="20.0" mnemonicParsing="false" prefHeight="80.0" prefWidth="80.0" text="Thon" textAlignment="CENTER" wrapText="true" />
                              <Button layoutX="190.0" layoutY="20.0" mnemonicParsing="false" prefHeight="80.0" prefWidth="80.0" text="4 Cheeses" textAlignment="CENTER" wrapText="true" />
                           </children>
                        </TilePane>
                     </content>
                  </TitledPane>
               </panes>
            </Accordion>
            <Accordion fx:id="categorieSouflee" layoutX="10.0" layoutY="10.0">
               <panes>
                  <TitledPane animated="false" layoutX="10.0" layoutY="452.0" text="Souflés M">
                     <content>
                        <TilePane hgap="5.0" prefHeight="200.0" prefWidth="200.0" vgap="5.0">
                           <children>
                              <Button mnemonicParsing="false" prefHeight="80.0" prefWidth="80.0" text="Poulet" textAlignment="CENTER" wrapText="true" />
                              <Button layoutX="20.0" layoutY="20.0" mnemonicParsing="false" prefHeight="80.0" prefWidth="80.0" text="Viande Hachée" textAlignment="CENTER" wrapText="true" />
                              <Button layoutX="105.0" layoutY="20.0" mnemonicParsing="false" prefHeight="80.0" prefWidth="80.0" text="Thon" textAlignment="CENTER" wrapText="true" />
                              <Button layoutX="190.0" layoutY="20.0" mnemonicParsing="false" prefHeight="80.0" prefWidth="80.0" text="4 Cheeses" textAlignment="CENTER" wrapText="true" />
                           </children>
                        </TilePane>
                     </content>
                  </TitledPane>
               </panes>
            </Accordion>
         </children>
      </StackPane>
      <Button layoutX="10.0" layoutY="10.0" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#backto_commandes" text="Annuler" textAlignment="CENTER" wrapText="true" GridPane.rowIndex="8" />
      <Button layoutX="10.0" layoutY="544.0" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="Supprimer" textAlignment="CENTER" wrapText="true" GridPane.columnIndex="1" GridPane.rowIndex="8" />
      <Button layoutX="98.0" layoutY="544.0" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="Imprimer" textAlignment="CENTER" wrapText="true" GridPane.columnIndex="7" GridPane.rowIndex="8" />
      <Button layoutX="632.0" layoutY="544.0" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="Envoyer" textAlignment="CENTER" wrapText="true" GridPane.columnIndex="8" GridPane.rowIndex="8" />
      <Text strokeType="OUTSIDE" strokeWidth="0.0" text="2500.00" GridPane.columnIndex="1" GridPane.halignment="CENTER" GridPane.rowIndex="7" GridPane.valignment="CENTER" />
      <Text layoutX="174.0" layoutY="519.0" strokeType="OUTSIDE" strokeWidth="0.0" text="NET TOTAL :" GridPane.halignment="CENTER" GridPane.rowIndex="7" GridPane.valignment="CENTER" />
      <Text layoutX="166.0" layoutY="519.0" strokeType="OUTSIDE" strokeWidth="0.0" text="DA" GridPane.columnIndex="2" GridPane.halignment="CENTER" GridPane.rowIndex="7" GridPane.valignment="CENTER" />
      <VBox alignment="TOP_RIGHT" GridPane.columnIndex="2" GridPane.halignment="CENTER" GridPane.rowIndex="1" GridPane.valignment="CENTER">
         <children>
            <Text strokeType="OUTSIDE" strokeWidth="0.0" text="Table : 12" />
            <Text strokeType="OUTSIDE" strokeWidth="0.0" text="Couverts : 4" />
         </children>
      </VBox>
      <VBox GridPane.rowIndex="1">
         <children>
            <Text strokeType="OUTSIDE" strokeWidth="0.0" text="N° 59924" />
            <Text strokeType="OUTSIDE" strokeWidth="0.0" text="ID: H.AbdAllah N°5482187" />
         </children>
      </VBox>
      <VBox alignment="BOTTOM_CENTER" GridPane.columnIndex="1" GridPane.rowIndex="1">
         <children>
            <Text strokeType="OUTSIDE" strokeWidth="0.0" text="2 Mars 2020 21:46" />
         </children>
      </VBox>
      <TableView prefHeight="200.0" prefWidth="200.0" GridPane.columnSpan="3" GridPane.rowIndex="2" GridPane.rowSpan="5">
        <columns>
          <TableColumn prefWidth="58.0" text="Quantité" />
          <TableColumn prefWidth="181.0" text="Nom Article" />
            <TableColumn minWidth="0.0" prefWidth="53.0" text="Prix" />
        </columns>
      </TableView>
      <ScrollPane fitToHeight="true" GridPane.columnIndex="3" GridPane.columnSpan="6" GridPane.halignment="CENTER" GridPane.rowSpan="2" GridPane.valignment="CENTER">
         <content>
            <HBox fx:id="hboxOfCategories" alignment="CENTER_LEFT" spacing="10.0">
               <children>
                  <Button maxHeight="1.7976931348623157E308" mnemonicParsing="false" prefHeight="57.0" prefWidth="82.0" text="Boissons" />
                  <Button layoutX="10.0" layoutY="10.0" maxHeight="1.7976931348623157E308" mnemonicParsing="false" prefHeight="57.0" prefWidth="82.0" text="Burger" />
                  <Button layoutX="102.0" layoutY="10.0" maxHeight="1.7976931348623157E308" mnemonicParsing="false" prefHeight="57.0" prefWidth="82.0" text="Tacos" />
                  <Button layoutX="378.0" layoutY="10.0" maxHeight="1.7976931348623157E308" mnemonicParsing="false" prefHeight="57.0" prefWidth="82.0" text="Pizza" />
                  <Button layoutX="194.0" layoutY="10.0" maxHeight="1.7976931348623157E308" mnemonicParsing="false" prefHeight="57.0" prefWidth="82.0" text="Baguette Farcie" textAlignment="CENTER" wrapText="true" />
                  <Button layoutX="286.0" layoutY="10.0" maxHeight="1.7976931348623157E308" mnemonicParsing="false" prefHeight="57.0" prefWidth="82.0" text="Souflee" />
               </children>
            </HBox>
         </content>
      </ScrollPane>
   </children>
</GridPane>

问题来源:Stack Overflow

展开
收起
montos 2020-03-25 21:00:35 590 0
1 条回答
写回答
取消 提交回答
  • 乍一看,这看起来有点复杂,但是请仔细阅读它,这将变得很清楚。

    基本上,从开始StackPane,它将使用的先前结果(是的Setof Nodes)“递归”进入结构forEach。直到进入按钮,然后设置其OnAction事件处理程序。

    stackpaneOfCategories.getChildren()
                    .forEach(accordion->((Accordion)accordion).getPanes()
                    .forEach(tilePane->((TilePane)((tilePane).getContent())).getChildren()
                    .forEach(button->((Button)button)
                                             .setOnAction(a->System.out.println(((Button)button).getText())))));
                                            
    

    这种方法的缺点是,它OnAction为所有按钮设置了相同的参数,如果您希望每个按钮具有不同的行为,我想除了使用fx:ids 设置它们,您别无选择(但是我可能错了)。

    回答来源:Stack Overflow

    2020-03-25 21:01:26
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载