我有 :
现在,在控制器类中,我对父堆栈有一个引用:
@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
乍一看,这看起来有点复杂,但是请仔细阅读它,这将变得很清楚。
基本上,从开始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
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。