Java FXML如何使图像水平居中

Java FXML如何使图像水平居中,java,javafx,javafx-2,javafx-8,fxml,Java,Javafx,Javafx 2,Javafx 8,Fxml,如何将此图像水平居中?我试过像这样放一个HBox,但是它不起作用 这里是一个带有FXML文件的小MCVE: 应用程序: import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.stage.Stage; /** * * @author Naoghuman */ publi

如何将此图像水平居中?我试过像这样放一个HBox,但是它不起作用


这里是一个带有FXML文件的小MCVE:

应用程序:

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

/**
 *
 * @author Naoghuman
 */
public class CenterImageInHBox extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));

        Scene scene = new Scene(root);

        stage.setScene(scene);
        stage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

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

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

<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="center.image.in.hbox.FXMLDocumentController">
    <children>
        <VBox layoutX="60.0" prefHeight="200.0" prefWidth="100.0" spacing="7.0" style="-fx-background-color: YELLOWGREEN;" AnchorPane.bottomAnchor="14.0" AnchorPane.leftAnchor="14.0" AnchorPane.rightAnchor="14.0" AnchorPane.topAnchor="14.0">
            <children>
                <ToolBar prefHeight="40.0" prefWidth="200.0">
                    <items>
                        <Button mnemonicParsing="false" onAction="#onActionLeft" text="Left" />
                        <Button mnemonicParsing="false" onAction="#onActionCenter" text="Center" />
                        <Button mnemonicParsing="false" onAction="#onActionRight" text="Right" />
                    </items>
                </ToolBar>
                <HBox fx:id="hbImage" alignment="TOP_CENTER" VBox.vgrow="ALWAYS">
                    <children>
                        <ImageView fx:id="ivImage" fitHeight="128.0" fitWidth="128.0" pickOnBounds="true" preserveRatio="true" />
                    </children>
                </HBox>
            </children>
        </VBox>
    </children>
</AnchorPane>
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Pos;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;

/**
 *
 * @author Naoghuman
 */
public class FXMLDocumentController implements Initializable {

    @FXML private HBox hbImage;
    @FXML private ImageView ivImage;

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        Image img = new Image("http://icons.iconarchive.com/icons/evermor-design/galaxian/128/Movie-Clip-icon.png");
        ivImage.setImage(img);
    }    

    public void onActionCenter() {
        hbImage.setAlignment(Pos.TOP_CENTER);
    }

    public void onActionLeft() {
        hbImage.setAlignment(Pos.TOP_LEFT);
    }

    public void onActionRight() {
        hbImage.setAlignment(Pos.TOP_RIGHT);
    }

}
FXML文件:

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

/**
 *
 * @author Naoghuman
 */
public class CenterImageInHBox extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));

        Scene scene = new Scene(root);

        stage.setScene(scene);
        stage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

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

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

<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="center.image.in.hbox.FXMLDocumentController">
    <children>
        <VBox layoutX="60.0" prefHeight="200.0" prefWidth="100.0" spacing="7.0" style="-fx-background-color: YELLOWGREEN;" AnchorPane.bottomAnchor="14.0" AnchorPane.leftAnchor="14.0" AnchorPane.rightAnchor="14.0" AnchorPane.topAnchor="14.0">
            <children>
                <ToolBar prefHeight="40.0" prefWidth="200.0">
                    <items>
                        <Button mnemonicParsing="false" onAction="#onActionLeft" text="Left" />
                        <Button mnemonicParsing="false" onAction="#onActionCenter" text="Center" />
                        <Button mnemonicParsing="false" onAction="#onActionRight" text="Right" />
                    </items>
                </ToolBar>
                <HBox fx:id="hbImage" alignment="TOP_CENTER" VBox.vgrow="ALWAYS">
                    <children>
                        <ImageView fx:id="ivImage" fitHeight="128.0" fitWidth="128.0" pickOnBounds="true" preserveRatio="true" />
                    </children>
                </HBox>
            </children>
        </VBox>
    </children>
</AnchorPane>
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Pos;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;

/**
 *
 * @author Naoghuman
 */
public class FXMLDocumentController implements Initializable {

    @FXML private HBox hbImage;
    @FXML private ImageView ivImage;

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        Image img = new Image("http://icons.iconarchive.com/icons/evermor-design/galaxian/128/Movie-Clip-icon.png");
        ivImage.setImage(img);
    }    

    public void onActionCenter() {
        hbImage.setAlignment(Pos.TOP_CENTER);
    }

    public void onActionLeft() {
        hbImage.setAlignment(Pos.TOP_LEFT);
    }

    public void onActionRight() {
        hbImage.setAlignment(Pos.TOP_RIGHT);
    }

}

当您单击应用程序中的按钮时,您将看到图像已对齐居中取决于您选择的操作。

如何将
HBox
添加到场景中?在我看来,它的大小没有调整到父级宽度。它在一个Vbox中,而Vbox是父级,这是为什么?取决于Vbox。在进行布局时,我通常会启用边框,这解决了95%的布局和对齐难题!谢谢你的帮助!