无法在fxml文件中定位ImageView

时间:2016-05-30 13:22:19

标签: javafx javafx-2 fxml

我正在尝试从fxml文件中定位ImageView 这是我的fxml文件: -

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.String?>
<?import javafx.collections.FXCollections?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.VBox?>

<AnchorPane fx:id="ap" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="-Infinity" minWidth="-Infinity" prefHeight="480.0" prefWidth="911.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ch.Buildsapp.Main.BuildsController">
    <children>
        <VBox prefHeight="200.0" prefWidth="100.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
            <children>
                <HBox alignment="CENTER_LEFT" spacing="20.0" VBox.vgrow="NEVER">
                    <padding>
                        <Insets bottom="12.0" left="12.0" right="12.0" top="12.0" />
                    </padding>
                    <children>
                        <GridPane hgap="10.0" HBox.hgrow="NEVER">
                            <columnConstraints>
                                <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" />
                                <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" />
                                <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" />
                                <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" />
                                <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" />
                                <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" />
                                <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" />
                            </columnConstraints>
                            <rowConstraints>
                                <RowConstraints minHeight="10.0" vgrow="SOMETIMES" />
                                <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                            </rowConstraints>
                            <children>
                                <Label prefHeight="21.0" prefWidth="88.0" text="Platform" />
                                <ComboBox fx:id="versionCombo" prefHeight="31.0" prefWidth="108.0" GridPane.rowIndex="1">
                                    <items>
                                        <FXCollections fx:factory="observableArrayList">
                                            <String fx:value="Win" />
                                            <String fx:value="Mac" />
                                        </FXCollections>
                                    </items>
                                    <value>
                                        <String fx:value="Mac" />
                                    </value>
                                </ComboBox>
                                <Label prefHeight="21.0" prefWidth="80.0" text="Product" GridPane.columnIndex="1" />
                                <ComboBox fx:id="verCombo" prefHeight="31.0" prefWidth="154.0" GridPane.columnIndex="1" GridPane.rowIndex="1">
                                </ComboBox>
                                <Label text="Version" GridPane.columnIndex="2" />
                                <ComboBox fx:id="versionNo" prefHeight="31.0" prefWidth="144.0" GridPane.columnIndex="2" GridPane.rowIndex="1" />
                                <Label prefHeight="21.0" prefWidth="60.0" text="Server" GridPane.columnIndex="3" />
                                <ComboBox fx:id="locCombo" prefHeight="31.0" prefWidth="103.0" GridPane.columnIndex="3" GridPane.rowIndex="1">
                                    <items>
                                        <FXCollections fx:factory="observableArrayList">
                                            <String fx:value="SJ" />
                                            <String fx:value="MN" />
                                        </FXCollections>
                                    </items>
                                    <value>
                                        <String fx:value="SJ" />
                                    </value>
                                </ComboBox>
                                <Button fx:id="downloadButton" minWidth="80.0" mnemonicParsing="false" text="Download" GridPane.columnIndex="4" GridPane.rowIndex="1" />
                                <Button fx:id="installButton" minWidth="80.0" mnemonicParsing="false" text="Install" GridPane.columnIndex="5" GridPane.rowIndex="1" />
                                <Button fx:id="locButton" mnemonicParsing="false" prefHeight="31.0" prefWidth="120.0" text="Open Folder" GridPane.columnIndex="6" GridPane.rowIndex="1" />
                            </children>
                        </GridPane>
                                      <ImageView fx:id="image" fitHeight="18.0" fitWidth="61.0" nodeOrientation="INHERIT" pickOnBounds="true" preserveRatio="true">
                           <image>
                              <Image url="@../../../../../../Downloads/info_512pxGREY.png" />
                           </image>
                     <HBox.margin>
                        <Insets />
                     </HBox.margin>
                        </ImageView>

                    </children>
                </HBox>
                <TableView fx:id="tableView" VBox.vgrow="ALWAYS">
                    <columns>
                        <TableColumn fx:id="builds" prefWidth="482.0" text="Builds" />
                        <TableColumn fx:id="date" minWidth="0.0" prefWidth="500.0" text="Date" />
                    </columns>
                </TableView>
            </children>
        </VBox>

    </children>
</AnchorPane>

我希望图像显示在右上角但是它没有位于那里我试图用“layoutX和layoutY”改变X和Y坐标,但这似乎不起作用。

1 个答案:

答案 0 :(得分:1)

不同的Pane有不同的选择孩子位置的方式。

如果是AnchorPane,则可以使用锚点属性完成与右上角的对齐。这些属性控制父AnchorPane的子节点到窗格边框的距离。

以下示例将Rectangle放在AnchorPane的右上角:

<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1">
    <children>
        <Rectangle width="50" height="50" AnchorPane.topAnchor="0" AnchorPane.rightAnchor="0" />
    </children>
</AnchorPane>

您也可以将ImageView放在fxml的右上方。