简体   繁体   English

如何将图像视图置于锚窗格中心?

[英]How do I center an Image view in an anchor pane?

I am using scene builder to create my GUI and I am displaying Image s as users select items in a list. 我正在使用场景构建器来创建我的GUI,并且当用户选择列表中的项目时,我正在显示Image The Image s are of different sizes and when an Image that is smaller than the pane it is in, the image is displayed in the top left of the pane. Image具有不同的大小,当Image小于其所在的窗格时,图像显示在窗格的左上角。 How do I go about getting the ImageView and/or Image to be displayed in the center of the Pane ? 如何让ImageView和/或Image显示在Pane的中央?

I've looked throughout the javafx imageview and image APIs but I haven't found much on centering the ImageView in a Pane . 我在整个javafx imageview和图像API中看了一下,但是我没有找到很多将ImageView集中在Pane I haven't seen anything in scene builder, either. 我也没有在场景构建器中看到任何东西。 Usually in scene builder, if there is a way to center a node, there will be an option for centering. 通常在场景构建器中,如果有一种方法可以使节点居中,则会有一个居中选项。

In any kind of pane its now possible to set image alignment in specific postion.. for this you can use try HBox 在任何类型的窗格中,现在可以在特定位置设置图像对齐..为此您可以使用try HBox

when you insert an Image into the HBox , initially its shown like below. 当您将Image插入HBox ,最初显示如下所示。

截图

now set the alignment property of the HBox 现在设置HBox的alignment属性

截图

now change the alignment to centre and see the output 现在将对齐更改为居中并查看输出

截图

I hope its work for you 我希望它为你工作

Its also possible to do so programmatically in a Pane via get image 它也可以通过get图像以编程方式在Pane完成

try this 尝试这个

AnchorPane anchorPane = new AnchorPane();
Image img = new Image(getClass().getResourceAsStream("Edit-Male-User-icon.png"));
ImageView imageView = new ImageView(img);
anch.getChildren().add(imageView );
ImageView app = (ImageView) (Node) anchorPane.getChildren().get(0);
app.setX(100);
app.setY(100);

Create a Group and scene in your start method: 在start方法中创建一个Group和场景:

Group root = new Group();
Scene scene = new Scene(root, 551, 400, Color.BLACK);
primaryStage.setScene(scene);

Create an ImageView and set the following properties: 创建ImageView并设置以下属性:

ImageView imageView = new ImageView();
// set aspect ratio
imageView.setPreserveRatio(true);
// resize based on the scene
imageView.fitWidthProperty().bind(scene.widthProperty());
imageView.fitHeightProperty().bind(scene.heightProperty());

Create a StackPane (at least that is what I used) and bind your properties: 创建一个StackPane (至少这是我使用的)并绑定您的属性:

StackPane stack = new StackPane();
stack.getChildren().add(imageView);

    stack.translateXProperty()
            .bind(scene.widthProperty().subtract(stack.widthProperty())
                    .divide(2));

    stack.translateYProperty()
            .bind(scene.heightProperty().subtract(stack.heightProperty())
                    .divide(2));

Add this stack to root element: 将此堆栈添加到根元素:

root.getChildren().add(stack);

Show primaryStage and execute other code in your start method: 在start方法中显示primaryStage并执行其他代码:

primaryStage.show();

A simple way to make small images dynamically centered : 一种使小图像动态居中的简单方法:

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class CenterInStage extends Application {

    private final String TITLE = "Center in Stage";
    private final double WIDTH = 350;
    private final double HEIGHT = 250;
    private final String IMAGE_PATH =
            "http://icons.iconarchive.com/icons/iconka/meow-2/64/cat-rascal-icon.png";

    @Override
    public void start(Stage primaryStage) {

        primaryStage.setTitle(TITLE);

        Image image = new Image(IMAGE_PATH);
        ImageView imageView = new ImageView(image);
        imageView.setPreserveRatio(true);

        StackPane pane = new StackPane();
        pane.getChildren().add(imageView);
        StackPane.setAlignment(imageView, Pos.CENTER);

        Scene scene = new Scene(pane, WIDTH, HEIGHT);

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

    public static void main(String[] args) {
        launch(args);
    }
}

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM