如何将 ImageView 锚定到其 BorderPane 区域的中心?
How can I anchor an ImageView to the center of its BorderPane area?
我有一个带 ImageView 的 BorderPane 布局,如下所示,但 ImageView 始终固定在左上角,并且在我调整 window 大小时保持在那里。我想让 ImageView 将锚点保持在它绑定到的部分的中心(红框)。这可能使用 FXML 吗?
FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.Group?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.*?>
<VBox prefWidth="800.0" prefHeight="600.0" xmlns="http://javafx.com/javafx/8.0.112" xmlns:fx="http://javafx.com/fxml/1">
<BorderPane xmlns:fx="http://javafx.com/fxml" VBox.vgrow="ALWAYS">
<top>
<VBox alignment="TOP_CENTER">
<ToolBar minHeight="50.0" prefHeight="50.0" prefWidth="800.0" stylesheets="@../css/style.css"
GridPane.rowIndex="1">
<ImageView fitHeight="35.0" fitWidth="200.0" pickOnBounds="true" preserveRatio="true">
<Image url="@../images/toolbar-logo.png"/>
</ImageView>
</ToolBar>
<MenuBar fx:id="menuBar" prefHeight="0.0" prefWidth="0.0"/>
</VBox>
</top>
<left>
<StackPane prefWidth="230.0" prefHeight="800.0">
<ListView fx:id="listView"/>
</StackPane>
</left>
<center>
<StackPane>
<ScrollPane>
<Group fx:id="selectionGroup">
<ImageView fx:id="mainImageView"/>
</Group>
</ScrollPane>
</StackPane>
</center>
<right>
<TextField promptText="Text" />
</right>
</BorderPane>
</VBox>
变化:
<StackPane>
<ScrollPane>
<Group fx:id="selectionGroup">
<ImageView fx:id="mainImageView"/>
</Group>
</ScrollPane>
</StackPane>
收件人:
<StackPane>
<children>
<ImageView fx:id="mainImageView" fitHeight="150.0" fitWidth="200.0" pickOnBounds="true" preserveRatio="true" />
</children>
</StackPane>
您可能需要调整 fitWidth
和 fitHeight
属性。
我有一个带 ImageView 的 BorderPane 布局,如下所示,但 ImageView 始终固定在左上角,并且在我调整 window 大小时保持在那里。我想让 ImageView 将锚点保持在它绑定到的部分的中心(红框)。这可能使用 FXML 吗?
FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.Group?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.*?>
<VBox prefWidth="800.0" prefHeight="600.0" xmlns="http://javafx.com/javafx/8.0.112" xmlns:fx="http://javafx.com/fxml/1">
<BorderPane xmlns:fx="http://javafx.com/fxml" VBox.vgrow="ALWAYS">
<top>
<VBox alignment="TOP_CENTER">
<ToolBar minHeight="50.0" prefHeight="50.0" prefWidth="800.0" stylesheets="@../css/style.css"
GridPane.rowIndex="1">
<ImageView fitHeight="35.0" fitWidth="200.0" pickOnBounds="true" preserveRatio="true">
<Image url="@../images/toolbar-logo.png"/>
</ImageView>
</ToolBar>
<MenuBar fx:id="menuBar" prefHeight="0.0" prefWidth="0.0"/>
</VBox>
</top>
<left>
<StackPane prefWidth="230.0" prefHeight="800.0">
<ListView fx:id="listView"/>
</StackPane>
</left>
<center>
<StackPane>
<ScrollPane>
<Group fx:id="selectionGroup">
<ImageView fx:id="mainImageView"/>
</Group>
</ScrollPane>
</StackPane>
</center>
<right>
<TextField promptText="Text" />
</right>
</BorderPane>
</VBox>
变化:
<StackPane>
<ScrollPane>
<Group fx:id="selectionGroup">
<ImageView fx:id="mainImageView"/>
</Group>
</ScrollPane>
</StackPane>
收件人:
<StackPane>
<children>
<ImageView fx:id="mainImageView" fitHeight="150.0" fitWidth="200.0" pickOnBounds="true" preserveRatio="true" />
</children>
</StackPane>
您可能需要调整 fitWidth
和 fitHeight
属性。