【发布时间】:2018-11-20 11:26:09
【问题描述】:
我初始化了一些不是我的锚窗格的元素:
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 制作了它们,我需要将它们分开保存,因为我更喜欢为每个控制器单独保存一个文件。 编辑:
【问题讨论】:
-
为什么不能使用
BorderPane或任何其他layout? -
因为我应该重新设计所有应用程序并修改所有 fxml 文件。如您所见,使用 AnchorPane,我已经将所有元素定位在需要它们的位置
-
尝试在窗口中添加鼠标事件过滤器以查看实际的
EventTarget是什么。如果它不是ListView或其子节点之一,那么您可能有一些节点与它重叠。 -
@Slaw “重叠”是指一个元素在另一个元素上?因为我试图单独添加它们并且如果我设置前两个元素它可以工作,但是一旦我设置了文本区域和文本字段,我就不能再点击前两个元素了。我将编辑问题以向您显示应用程序的输出。
-
某些节点可能仍与其他节点重叠,即使您看不到它。添加用于加载 FXML 文件的代码,以便我们可以在我们自己的机器上运行/调试您的应用程序(即包括 minimal reproducible example)。