gpt4 book ai didi

java - AnchorPane 元素忽略鼠标单击

转载 作者:行者123 更新时间:2023-12-01 16:45:02 27 4
gpt4 key购买 nike

我初始化了一些不是我的锚定 Pane 的元素:

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

但是当我尝试单击菜单栏或 ListView 时,它不起作用。例如,在本例中,我可以单击按钮(也许),因为它是我在 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>

我用场景生成器制作了它们,我需要将它们分开保存,因为我更喜欢每个 Controller 都有一个单独的文件。编辑: Output

编辑2:这是第一个 AnchorPane(在右侧您可以看到设置): SB1这是第二个 AnchorPane SB2这是文本区域: enter image description here

最佳答案

AnchorPane如果您正在设计响应式布局,则这是最糟糕的布局之一,因为它不允许您在定位子级时考虑其他子级的大小(除非您使用监听器/绑定(bind)来更新布局参数)。

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

一个可能的解决方法是使子 fxml 尽可能小并指定父级中的根位置:

确定最小值layoutXlayoutY内在的 children 的<AnchorPane> ,为根设置这些值(或将它们添加到根的先前值),并从内部 <AnchorPane> 的所有子项中减去这些值。 .

<!-- 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 的任何修改都可能需要您更新多个其他节点以避免覆盖。更糟糕的是,根据操作系统/设置的不同,情况看起来也会有所不同。对于一种操作系统,场景可能看起来不错,但在另一种操作系统上,您的节点可能会重叠。
因此,我建议避免使用绝对定位,除非您 100% 确定节点大小已知。使用 BorderPane 可以更好地创建如图所示的场景。/HBox/VBox和/或 GridPane ...

关于java - AnchorPane 元素忽略鼠标单击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53391994/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com