如何检查两个或更多个元素是否在JavaFx布局中发生冲突

时间:2018-11-19 19:22:16

标签: java user-interface javafx graphics observable

我制作了一个简单的电子邮件客户端,但是有一个问题:将元素设置为AnchorPane时,它仅设置了最后一个元素,因此我认为存在度量错误的问题。我检查了不止一次,但是正如您所看到的,这些元素之间的分隔很好: mailbox

这是我将元素放入锚定窗格的方式:

public void start(Stage stage) throws Exception {
    FXMLLoader listLoader = new FXMLLoader(getClass().getResource("lista.fxml"));
    FXMLLoader textareaLoader = new FXMLLoader(getClass().getResource("textarea.fxml"));
    FXMLLoader fieldLoader = new FXMLLoader(getClass().getResource("textfield.fxml"));
    FXMLLoader menuLoader = new FXMLLoader(getClass().getResource("menubar.fxml"));
    FXMLLoader buttonLoader = new FXMLLoader(getClass().getResource("button.fxml"));

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

这些是具有所有坐标的FXML文件:

List.fxml

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

MenuBar

<AnchorPane xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="mailbox.MenuBarController">
    <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>

TextArea

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

TextField

<AnchorPane mouseTransparent="false" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="mailbox.TextFieldController">
   <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>

按钮

<AnchorPane xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="mailbox.ButtonController">
   <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>

1 个答案:

答案 0 :(得分:1)

Blue represents AnchorPane of button.fxml

我为button.fxml AnchorPane添加了蓝色背景。

问题出在按钮的layoutXlayoutY上;按钮的坐标是相对于AnchorPane的{​​{1}}的左上角计算的,而不是相对于button.fxml的{​​{1}}的计算。这会导致AnchorPane隐藏其后面的组件(如蓝色所示),因此list.xml将消耗鼠标的输入事件。

要解决此问题(有比这更好的布局方法,只是临时修复),请在AnchorPane中使用此代码

AnchorPane

上面的代码将开始在您期望的位置布置按钮,并且为了简化起见,我将它们包装在Vbox中。

关于2个节点的碰撞检查,请使用

button.fxml