我初始化了一些元素而不是我的Anchor窗格:
AnchorPane root = new AnchorPane(listLoader.load(), textareaLoader.load(), fieldLoader.load(), menuLoader.load(), buttonLoader.load());
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试单击MenuBar或列表视图时,它不起作用.例如,在这种情况下,我可以单击按钮(可能),因为它是我在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>
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
我用Scene Builder制作它们,我需要将它们分开保存,因为我更喜欢每个控制器的单个文件.编辑:

EDIT2:这是第一个AnchorPane(在右边你可以看到设置):
这是第二个AnchorPane
这是Textarea:

AnchorPane 如果您正在设计响应式布局,那么它是最糟糕的布局之一,因为在定位子级时,它不允许您考虑其他子级的大小(除非您使用侦听器/绑定来更新布局参数).
在这个特定情况下你的问题是你button.fxml覆盖了其他fxmls的所有内容,因此接收所有MouseEvents.要想象fxml覆盖的区域,只需将此属性添加到以下的根button.fxml:style="-fx-background-color: rgba(0, 0, 255, 0.5)".
一个可能的解决方法是让子fxmls尽可能小并指定父节点中的根位置:
确定内部的最小值layoutX和layoutY子项<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>
Run Code Online (Sandbox Code Playgroud)
(相应地继续使用其他fxmls.)
以这种方式叠加节点仍然是一个可怕的想法,因为您使用绝对位置,并且对其中一个fxml的任何修改都可能要求您更新多个其他位置以避免叠加.根据操作系统/设置,更糟糕的看起来会有所不同 对于一个操作系统,场景可能看起来很好,另一个操作系统可能会重叠.
因此,我建议避免使用绝对定位,除非您100%确定节点大小已知.场景像所示更好的使用创造了一个BorderPane/ HBox/ VBox和/或GridPane...
| 归档时间: |
|
| 查看次数: |
82 次 |
| 最近记录: |