【问题标题】:JavaFX: How to center an ImageView in a PaneJavaFX:如何在窗格中居中 ImageView
【发布时间】:2018-02-05 18:51:03
【问题描述】:

JavaFX:如何将 ImageView 在窗格中居中

嗨, 我正在尝试根据当前的窗口大小来居中和调整 ImageView 的大小。

如果我将 ImageView 放入窗格中,我无法将其居中。 如果我将其放入 StackPane 中,则无法调整图像大小。

任何想法,我做错了什么? Google 帮不了我。

使用窗格提取 FXML

      <HBox prefHeight="100.0" prefWidth="200.0" VBox.vgrow="ALWAYS">
       <children>
         <Button maxWidth="-Infinity" minWidth="-Infinity" mnemonicParsing="false" prefHeight="2000.0" prefWidth="100.0" text="Button" HBox.hgrow="ALWAYS" />
         <Pane fx:id="paneMainImage" prefHeight="200.0" prefWidth="200.0" HBox.hgrow="ALWAYS">
            <children>
               <ImageView fx:id="imageMainImage" fitHeight="10.0" fitWidth="10.0" pickOnBounds="true" preserveRatio="true" />
            </children>
            <HBox.margin>
               <Insets />
            </HBox.margin>
         </Pane>
         <Button maxWidth="-Infinity" minWidth="-Infinity" mnemonicParsing="false" prefHeight="2000.0" prefWidth="100.0" text="Button" HBox.hgrow="ALWAYS" />
        </children>
      </HBox>

这里是 Java 代码

public class FXMLDocumentController implements Initializable {

@FXML
Pane paneMainImage;

@FXML
ImageView imageMainImage;

@Override
public void initialize(URL url, ResourceBundle rb) {
    Image i = new Image(getClass().getResource("resources/DSC_0168.JPG").toString());
    paneMainImage.getWidth();
    paneMainImage.getHeight();
    imageMainImage.setImage(i);
    imageMainImage.fitWidthProperty().bind(paneMainImage.widthProperty());
    imageMainImage.fitHeightProperty().bind(paneMainImage.heightProperty());
}
}

与 StackPane 类似的代码

 <HBox prefHeight="100.0" prefWidth="200.0" VBox.vgrow="ALWAYS">
     <children>
        <Button maxWidth="-Infinity" minWidth="-Infinity" mnemonicParsing="false" prefHeight="2000.0" prefWidth="100.0" text="Button" HBox.hgrow="ALWAYS" />
        <StackPane fx:id="paneMainImage" prefHeight="150.0" prefWidth="200.0" HBox.hgrow="ALWAYS">
           <children>
              <ImageView fx:id="imageMainImage" fitHeight="10.0" fitWidth="10.0" pickOnBounds="true" preserveRatio="true" />
           </children>
        </StackPane>
        <Button maxWidth="-Infinity" minWidth="-Infinity" mnemonicParsing="false" prefHeight="2000.0" prefWidth="100.0" text="Button" HBox.hgrow="ALWAYS" />
     </children>
  </HBox>

Java

public class FXMLDocumentController implements Initializable {

@FXML
StackPane paneMainImage;

@FXML
ImageView imageMainImage;

@Override
public void initialize(URL url, ResourceBundle rb) {
    Image i = new Image(getClass().getResource("resources/DSC_0168.JPG").toString());
    paneMainImage.getWidth();
    paneMainImage.getHeight();
    imageMainImage.setImage(i);
    imageMainImage.fitWidthProperty().bind(paneMainImage.widthProperty());
    imageMainImage.fitHeightProperty().bind(paneMainImage.heightProperty());
}
}

【问题讨论】:

  • 尝试使用此代码,这可能会工作paneMainImage.setStyle("-fx-alignment: CENTER;");

标签: javafx imageview pane


【解决方案1】:

another post 上也有类似的讨论。您是否尝试过使用此处讨论的任何解决方案(按复杂程度排序)?

  1. 使用 BorderPane 作为图像的父级使其居中使用

  2. setX 和 setY 方法显式设置图像的位置

【讨论】:

  • 谢谢,我会调查的
猜你喜欢
  • 2014-06-27
  • 1970-01-01
  • 1970-01-01
  • 2015-12-16
  • 1970-01-01
  • 1970-01-01
  • 2016-08-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多