簡體   English   中英

在 Windows 上移動父級時移動子級

[英]Move a child stage when moving parent stage on Windows

我想知道當我們在 Windows 上移動父級時,是否有辦法將 JavaFX 子級與其父級一起移動。 事實上,在 MacOS 上,這似乎是默認行為。 正如您在此視頻中看到的: https://imgur.com/a/r3qIklu ,我可以獨立移動縮略圖場景,當我移動主 window 時,縮略圖(這是主 Z05B8Ctw35Z05B8C702FBF4Z 的子場景),仍然“依附”到父級並跟隨它。

但是,在 Windows 上,您可以在此處看到: https://imgur.com/a/SPEkYJ2 ,結果不一樣。 當父級移動時,縮略圖會粘到其當前的 position。 如何在 Windows 上重現 MacOS 行為?

作為參考,這是我的階段的初始化方式:

public void start(Stage primaryStage) {
    //App's main stage
    Parent root = FXMLLoader.load(getClass().getResource("../spaception.fxml"));
    Scene scene = new Scene(root, 1400, 700);
    primaryStage.setScene(scene);
    primaryStage.show();
    //...

    //Child stage (Thumbnail)
    Parent thumbnailRoot = FXMLLoader.load(getClass().getResource("../thumbnail.fxml"));
    Stage thumbnailStage = new Stage();
    thumbnailStage.initOwner(primaryStage);
    thumbnailStage.initStyle(StageStyle.UNDECORATED);
    thumbnailStage.setX(primaryStage.getX()+1100);
    thumbnailStage.setY(primaryStage.getY()+540);
    Scene scene = new Scene(thumbnailRoot, 250, 145);
    thumbnailStage.setScene(scene);
    thumbnailStage.show();
}

將根目錄和 thumbnailRoot 放在一個常規窗格中(相當於 Swing 中的 null 布局)。

要允許用戶移動 thumbnailRoot,請向其添加鼠標事件處理程序以進行按下和拖動,並保留跟蹤 thumbnailRoot 的 position 的私有字段並跟蹤鼠標拖動。

簡單的示例實現:

import javafx.application.Application;
import javafx.geometry.Bounds;
import javafx.geometry.Insets;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.MenuBar;
import javafx.scene.layout.Pane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseButton;

public class ParentAndThumbnail
extends Application {
    private Bounds thumbnailBoundsOnPress;

    private double pressedX;
    private double pressedY;

    @Override
    public void start(Stage stage) {
        Label positionLabel = new Label("Current position: x y");

        VBox left =
            new VBox(6, new Button("+"), new Button("-"), new Button("C"));
        left.setFillWidth(true);
        left.setPadding(new Insets(6));
        left.getChildren().forEach(
            c -> ((Button) c).setMaxWidth(Double.MAX_VALUE));

        VBox right =
            new VBox(6, new Button("+"), new Button("-"), new Button("C"));
        right.setFillWidth(true);
        right.setPadding(new Insets(6));
        right.getChildren().forEach(
            c -> ((Button) c).setMaxWidth(Double.MAX_VALUE));

        MenuBar menuBar = new MenuBar(
            new Menu("File", null,
                new MenuItem("New"),
                new MenuItem("Open"),
                new MenuItem("Save"),
                new MenuItem("Exit")),
            new Menu("Edit", null,
                new MenuItem("Cut"),
                new MenuItem("Copy"),
                new MenuItem("Paste")));

        ImageView imagePane =
            new ImageView(new Image("parent-stage-background.png"));

        BorderPane root = new BorderPane(
            imagePane, menuBar, left, positionLabel, right);

        BorderPane thumbnailRoot = new BorderPane(
            new ImageView(new Image("thumbnail-background.png")));
        thumbnailRoot.setStyle(
            "-fx-border-width: 4px 30px 4px 30px; " +
            "-fx-border-color: black");

        thumbnailRoot.setOnMousePressed(e -> {
            if (e.getButton() == MouseButton.PRIMARY) {
                thumbnailBoundsOnPress = thumbnailRoot.getBoundsInParent();
                pressedX = e.getScreenX();
                pressedY = e.getScreenY();
            }
        });
        thumbnailRoot.setOnMouseDragged(e -> {
            if (e.getButton() == MouseButton.PRIMARY) {
                thumbnailRoot.setLayoutX(
                    thumbnailBoundsOnPress.getMinX()
                    + e.getScreenX() - pressedX);
                thumbnailRoot.setLayoutY(
                    thumbnailBoundsOnPress.getMinY()
                    + e.getScreenY() - pressedY);
            }
        });

        Pane overlay = new Pane();
        overlay.getChildren().addAll(root, thumbnailRoot);

        stage.setOnShown(e -> {
            thumbnailRoot.setLayoutX(root.getWidth() - 400);
            thumbnailRoot.setLayoutY(root.getHeight() - 200);
        });

        stage.setScene(new Scene(overlay));
        stage.setTitle("Spaception");
        stage.show();
    }

    public static class Main {
        public static void main(String[] args) {
            Application.launch(ParentAndThumbnail.class, args);
        }
    }
}

您可以在 .fxml 文件中完成相同的功能:只需將背景和縮略圖放在窗格中。 如果您願意,您也可以在那里添加鼠標事件處理程序。

這是我用作 parent-stage-background.png 的圖像:

在此處輸入圖像描述

這是我用作 thumbnail-background.png 的圖像:

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM