gpt4 book ai didi

javafx-8 - JavaFX - 移动窗口效果

转载 作者:行者123 更新时间:2023-12-02 19:15:38 25 4
gpt4 key购买 nike

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

最佳答案

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

window

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

}

关于javafx-8 - JavaFX - 移动窗口效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32393450/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com