アンカーペインでイメージビューを中央にするにはどうすればよいですか。
-
21-12-2019 - |
質問
シーンビルダーを使用してGUIを作成し、ユーザーがリスト内の項目を選択すると、Image
sを表示しています。Image
sはサイズが異なり、ペインより小さいImage
が属している場合、画像はペインの左上に表示されます。ImageView
の中央にImage
および/またはPane
を表示するのをどうやって進んでください。
JavaFX ImageViewとImage APIを通して見ていましたが、ImageView
in A Pane
をセンタリングする上であまり見つかりませんでした。私はシーンビルダーで何も見たことがありません。通常、シーンビルダーでは、ノードを中心にする方法がある場合は、センタリングのオプションがあります。
解決
任意の種類のペインで、特定のポスで画像の配置を設定することが可能です。これで、Try HBox
Image
にHBox
を挿入すると、最初は以下のように示しています。
HBox
中央に位置合わせを変更して出力
を見る
あなたの仕事を願っています
Get Image
を介してPane
でプログラム的に行うことも可能です。
これを試してください
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);
. 他のヒント
起動方法で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());
.
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));
.
ルート要素にこのスタックを追加する:
root.getChildren().add(stack);
.
primaryStage
を表示し、Startメソッドで他のコードを実行します。
primaryStage.show();
. 小さな画像を動的に中心にする簡単な方法:
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);
}
}
.
所属していません StackOverflow