Java 如何在锚定窗格中居中图像视图?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20014787/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 22:20:37  来源:igfitidea点击:

How do I center an Image view in an anchor pane?

javauser-interfaceimageviewjavafxscenebuilder

提问by j will

I am using scene builder to create my GUI and I am displaying Images as users select items in a list. The Images are of different sizes and when an Imagethat is smaller than the pane it is in, the image is displayed in the top left of the pane. How do I go about getting the ImageViewand/or Imageto be displayed in the center of the Pane?

我正在使用场景构建器来创建我的 GUI,并且我在Image用户选择列表中的项目时显示s。的Images为不同的尺寸,并且当一个Image比它在窗格越小,图像显示在左上角的窗格。如何让ImageView和/或Image显示在 的中心Pane

I've looked throughout the javafx imageview and image APIs but I haven't found much on centering the ImageViewin a 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.

我已经查看了 javafx 图像视图和图像 API,但我没有找到太多ImageViewPane. 我也没有在场景构建器中看到任何东西。通常在场景构建器中,如果有一种方法可以使节点居中,则会有一个居中选项。

采纳答案by Anshul Parashar

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 Imageinto the HBox, initially its shown like below.

当您插入ImageHBox,初步显示出它像下面。

screenshot

截屏

now set the alignment property of the HBox

现在设置对齐属性 HBox

screenshot

截屏

now change the alignment to centre and see the output

现在将对齐更改为居中并查看输出

screenshot

截屏

I hope its work for you

我希望它对你有用

Its also possible to do so programmatically in a Panevia get image

也可以在Panevia get image 中以编程方式执行此操作

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);

回答by easyScript

Create a Groupand scene in your start method:

Group在你的 start 方法中创建一个和场景:

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

Create an ImageViewand 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 primaryStageand execute other code in your start method:

primaryStage在 start 方法中显示并执行其他代码:

primaryStage.show();

回答by c0der

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);
    }
}

enter image description here

在此处输入图片说明