AnchorPane 元素忽略鼠标点击

AnchorPane elements ignore Mouse click

我初始化了一些不是我的锚窗格的元素:

AnchorPane root = new AnchorPane(listLoader.load(), textareaLoader.load(), fieldLoader.load(), menuLoader.load(), buttonLoader.load());

但是当我尝试单击菜单栏或列表视图时,它不起作用。例如,在这种情况下,我可以单击按钮(也许),因为它是我在 AnchorPane 构造函数中初始化的最后一个元素。我不能使用 BorderPane 或任何其他布局,因此我需要找到具有此配置的解决方案。 这些是我的 fxml 文件:

list.fxml

<AnchorPane xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="mailbox.ListController">
<children>
    <AnchorPane> 
        <children>
            <ListView fx:id="listView" layoutX="1.0" layoutY="31.0" prefHeight="371.0" prefWidth="239.0" />
        </children>
    </AnchorPane> 
</children>

menuBar.fxml

<AnchorPane xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="mailbox.MenuBarController">
<children>
    <AnchorPane> 
        <children>
            <MenuBar fx:id="menuBar" layoutX="0.0" layoutY="0.0">
                <menus>
                    <Menu text="File">
                        <items>
                            <MenuItem onAction="#elimina" text="Elimina" />
                        </items>
                    </Menu>
                    <Menu text="Cambia Account">
                        <items>
                            <MenuItem fx:id="email1" text="filippo@hotmail.it" />
                            <MenuItem fx:id="email2" text="giancarlo@yahoo.it" />
                            <MenuItem fx:id="email3" text="alessandro@gmail.it" />
                        </items>
                    </Menu>
                </menus>
            </MenuBar>
        </children>
    </AnchorPane>
</children>

textArea.fxml

<AnchorPane xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="mailbox.TextAreaController">
<children>
    <AnchorPane> 
        <children>
            <TextArea fx:id="textarea" editable="false" layoutX="246.0" layoutY="255.0" prefHeight="144.0" prefWidth="350.0" />
        </children>
    </AnchorPane>
</children>

button.fxml

<AnchorPane xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="mailbox.ButtonController">
<children>
    <AnchorPane> 
        <children>
            <Button id="scrivi" layoutX="268.0" layoutY="200.0" mnemonicParsing="false" prefHeight="27.0" prefWidth="65.0" text="Scrivi" />
            <Button id="reply" layoutX="342.0" layoutY="200.0" mnemonicParsing="false" prefHeight="27.0" prefWidth="65.0" text="Reply" />
            <Button id="replyall" layoutX="420.0" layoutY="200.0" mnemonicParsing="false" prefHeight="27.0" prefWidth="75.0" text="Reply-All" />
            <Button id="forward" layoutX="511.0" layoutY="200.0" mnemonicParsing="false" prefHeight="27.0" prefWidth="75.0" text="Forward" />
        </children>
    </AnchorPane>
</children>

textfield.fxml

<AnchorPane mouseTransparent="false" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="mailbox.TextFieldController">
<children>
    <AnchorPane> 
        <children>
            <TextField fx:id="id" editable="false" layoutX="355.0" layoutY="39.0" mouseTransparent="false" prefHeight="27.0" prefWidth="35.0" />
            <TextField fx:id="mitt" editable="false" layoutX="355.0" layoutY="72.0" mouseTransparent="false" prefHeight="27.0" prefWidth="182.0" />
            <TextField fx:id="dest" editable="false" layoutX="355.0" layoutY="108.0" mouseTransparent="false" prefHeight="27.0" prefWidth="182.0" />
            <TextField fx:id="oggetto" editable="false" layoutX="355.0" layoutY="144.0" mouseTransparent="false" prefHeight="27.0" prefWidth="182.0" />
            <TextField fx:id="data" editable="false" layoutX="437.0" layoutY="39.0" mouseTransparent="false" prefHeight="27.0" prefWidth="100.0" />
            <Label layoutX="329.0" layoutY="44.0" text="ID:" />
            <Label layoutX="291.0" layoutY="77.0" text="Mittente:" />
            <Label layoutX="398.0" layoutY="44.0" text="Data:" />
            <Label layoutX="268.0" layoutY="113.0" text="Destinatario:" />
            <Label layoutX="292.0" layoutY="149.0" text="Oggetto:" />
        </children>
    </AnchorPane>
</children>

我用 Scene Builder 制作了它们,我需要将它们分开保存,因为我更喜欢每个控制器都有一个单独的文件。 编辑:

编辑2: 这是第一个AnchorPane(右边可以看到设置): 这是第二个 AnchorPane 这是文本区域:

AnchorPane 是最糟糕的布局之一,如果您正在设计响应式布局,因为它不允许您在定位时考虑其他 children 的大小child(除非您使用 listeners/bindings 更新布局参数)。

您在这种特定情况下的问题是您的 button.fxml 涵盖了其他 fxml 的所有内容,因此接收了所有 MouseEvent。要可视化 fxml 覆盖的区域,只需将此属性添加到 button.fxml 的根:style="-fx-background-color: rgba(0, 0, 255, 0.5)".

一种可能的解决方法是使 child fxml 尽可能小,并在 parent:

中指定根位置

确定内部 <AnchorPane> 的 children 的最小值 layoutXlayoutY,为根设置这些值(或将它们添加到先前的值根)并从内部 <AnchorPane>.

的所有 children 中减去这些值
<!-- specify min layoutX/Y as position for root -->
<AnchorPane xmlns="http://javafx.com/javafx/8"
    xmlns:fx="http://javafx.com/fxml/1"
    layoutX="268"
    layoutY="200"
    fx:controller="mailbox.ButtonController">
    <children>
        <AnchorPane>
            <children>
                <!-- subtract root layoutX/Y from positions -->
                <Button id="scrivi" mnemonicParsing="false" prefHeight="27.0"
                    prefWidth="65.0" text="Scrivi" />
                <Button id="reply" layoutX="74" mnemonicParsing="false"
                    prefHeight="27.0" prefWidth="65.0" text="Reply" />
                <Button id="replyall" layoutX="152" mnemonicParsing="false"
                    prefHeight="27.0" prefWidth="75.0" text="Reply-All" />
                <Button id="forward" layoutX="243" mnemonicParsing="false"
                    prefHeight="27.0" prefWidth="75.0" text="Forward" />
            </children>
        </AnchorPane>
    </children>
</AnchorPane>

(相应地处理其他 fxml。)

以这种方式覆盖节点仍然是一个糟糕的想法,因为您使用的是绝对位置,并且对其中一个 fxml 的任何修改都可能需要您更新多个其他节点以避免覆盖。更糟糕的是,根据 OS/settings 看起来会有所不同。对于一个 OS 场景可能看起来不错,在另一个上你的节点可能重叠。
出于这个原因,我建议避免使用绝对定位,除非您 100% 确定节点大小已知。使用 BorderPane/HBox/VBox and/or GridPane...

可以更好地创建如图所示的场景