JavaFX:如何在窗格中居中显示ImageView JavaFX:如何在窗格中居中显示ImageView

JavaFX:如何在窗格中居中显示ImageView JavaFX:如何在窗格中居中显示ImageView,javafx,imageview,pane,Javafx,Imageview,Pane,嗨, 我正在尝试根据当前窗口大小居中调整ImageView的大小 如果将ImageView放入窗格中,则无法将其居中。 如果我把它放在StackPane中,图像的大小调整就不起作用了 知道我做错了什么吗? 谷歌帮不了我 使用窗格提取FXML <HBox prefHeight="100.0" prefWidth="200.0" VBox.vgrow="ALWAYS"> <children> <Button maxWidth=

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

如果将ImageView放入窗格中,则无法将其居中。 如果我把它放在StackPane中,图像的大小调整就不起作用了

知道我做错了什么吗? 谷歌帮不了我

使用窗格提取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>

与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>

在这方面也有类似的讨论。您是否尝试过使用此处讨论的任何解决方案(按复杂性顺序)

  • 使用边框窗格作为图像的父级,使用

  • setX和setY方法来显式设置图像的位置


  • 尝试使用此代码,这可能会起作用
    paneMainImage.setStyle(“-fx alignment:CENTER;”) <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>
    
    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());
    }
    }