在父BorderPane中居中并调整ImageView的大小

时间:2015-02-14 22:48:00

标签: java layout javafx imageview

我有以下FXML:

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

<?import javafx.scene.image.*?>
<?import javafx.scene.effect.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>

<BorderPane fx:id="mainBorderPane" prefHeight="600.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="de.mho.livepicture.server.ui.presenter.UIPresenter">
   <top>
      <MenuBar BorderPane.alignment="CENTER">
        <menus>
          <Menu mnemonicParsing="false" text="Datei">
            <items>
              <MenuItem mnemonicParsing="false" onAction="#exit" text="Beenden" />
            </items>
          </Menu>
          <Menu mnemonicParsing="false" text="Hilfe">
            <items>
              <MenuItem mnemonicParsing="false" text="Über..." />
            </items>
          </Menu>
        </menus>
      </MenuBar>
   </top>
   <bottom>
      <HBox alignment="CENTER_RIGHT" spacing="6.0" BorderPane.alignment="CENTER_RIGHT">
         <children>
            <Button fx:id="startStopServerButton" mnemonicParsing="false" onAction="#startStopServer" text="Server starten" />
            <Button mnemonicParsing="false" onAction="#exit" text="Beenden">
               <opaqueInsets>
                  <Insets />
               </opaqueInsets>
               <HBox.margin>
                  <Insets />
               </HBox.margin>
            </Button>
         </children>
         <BorderPane.margin>
            <Insets bottom="6.0" left="6.0" right="6.0" top="6.0" />
         </BorderPane.margin>
      </HBox>
   </bottom>
   <center>
      <ImageView fx:id="imageView" pickOnBounds="true" preserveRatio="true" BorderPane.alignment="CENTER" />
   </center>
</BorderPane>

在我的演示者(控制器)中,我执行以下操作以动态居中并调整图像大小:

mainBorderPane.prefWidthProperty().bind(primaryStage.widthProperty());
mainBorderPane.prefHeightProperty().bind(primaryStage.heightProperty());

imageView.setImage(new Image(MAIN_ICON_BLACK_URL));
imageView.fitWidthProperty().bind(mainBorderPane.widthProperty());
imageView.fitHeightProperty().bind(mainBorderPane.heightProperty());

我不知道如何让ImageView调整BorderPane中心的可用空间大小。它总是使用舞台/场景的整个可用大小。

1 个答案:

答案 0 :(得分:0)

尝试将节点添加到left的{​​{1}}和right。这应该可以防止BorderPane占据整个空间。