【问题标题】:JavaFX - move window with effectJavaFX - 移动窗口有效果
【发布时间】:2015-09-04 08:26:58
【问题描述】:

我有未装饰的非全屏窗口,当鼠标离开它的区域时,我喜欢将它移到屏幕边界之外,但这样做很顺利。我发现一些 JavaFX 功能可以这样做——时间轴,但该时间轴的 KeyValue 不支持 stage.xProperty——因为这个属性是 readonlyProperty。有没有办法使用 JavaFX 函数平滑地移动我的窗口?

【问题讨论】:

  • shaking Stage in javaFX 的可能重复项
  • 实际上,虽然所涉及的技术有些相似,但目前对摇晃舞台问题的答案对于您的目的来说并不是那么好,因为它们不能平稳地移动舞台。

标签: javafx-8


【解决方案1】:

您可以设置通过时间轴中的 KeyValues 操作的代理属性。代理上的侦听器可以修改实际舞台位置。

import javafx.animation.*;
import javafx.application.*;
import javafx.beans.property.*;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.text.TextAlignment;
import javafx.stage.*;
import javafx.util.Duration;

public class StageSwiper extends Application {

    private static final int W = 350;
    private static final Duration DURATION = Duration.seconds(0.5);

    @Override
    public void start(Stage stage) throws Exception {
        Label instructions = new Label(
            "Window will slide off-screen when the mouse exits it.\n" +
                    "Click the window to close the application."
        );
        instructions.setTextAlignment(TextAlignment.CENTER);

        final StackPane root = new StackPane(instructions);
        root.setStyle("-fx-background-color: null;");

        DoubleProperty stageX = new SimpleDoubleProperty();
        stageX.addListener((observable, oldValue, newValue) -> {
            if (newValue != null && newValue.doubleValue() != Double.NaN) {
                stage.setX(newValue.doubleValue());
            }
        });

        final Timeline slideLeft = new Timeline(
                new KeyFrame(
                        DURATION,
                        new KeyValue(
                                stageX,
                                -W,
                                Interpolator.EASE_BOTH
                        )
                ),
                new KeyFrame(
                        DURATION.multiply(2)
                )
        );
        slideLeft.setOnFinished(event -> {
            slideLeft.jumpTo(Duration.ZERO);
            stage.centerOnScreen();
            stageX.setValue(stage.getX());
        });

        root.setOnMouseClicked(event -> Platform.exit());
        root.setOnMouseExited(event -> slideLeft.play());

        stage.setScene(new Scene(root, W, 100, Color.BURLYWOOD));
        stage.initStyle(StageStyle.UNDECORATED);
        stage.show();

        stage.centerOnScreen();
        stageX.set(stage.getX());
    }


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

}

【讨论】:

  • 谢谢,它工作正常!现在我将尝试将此解决方案嵌入到我的项目中。
猜你喜欢
  • 1970-01-01
  • 2017-06-30
  • 2011-03-06
  • 2017-08-26
  • 1970-01-01
  • 1970-01-01
  • 2020-11-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多