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);
9
在start方法中创建 Group 和场景:
Group root = new Group();
Scene scene = new Scene(root, 551, 400, Color.BLACK);
primaryStage.setScene(scene);
创建 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());
3 回答
在任何类型的窗格中,它现在可以在特定位置设置图像对齐..为此你可以使用try
HBox
当您将
Image
插入HBox
时,最初显示如下所示 .现在设置
HBox
的alignment属性现在将对齐更改为居中并查看输出
我希望它为你工作
它也可以通过编程方式在
Pane
中获取图像试试这个
在start方法中创建
Group
和场景:创建
ImageView
并设置以下属性:创建
StackPane
(至少这是我使用的)并绑定您的属性:将此堆栈添加到根元素:
显示
primaryStage
并在start方法中执行其他代码:一种使小图像动态居中的简单方法: