设置全屏退出提示的样式和字体大小,JavaFX

问题描述

您熟悉“按 esc 退出全屏”消息。

在JavaFX中,有方法

Stage stage = new Stage();
stage.setFullScreenExitHint("whatever you want it to say,instead of 'press esc to exit full screen.');

我用它来为用户显示一条说明信息,只是因为它非常简单、预先制作、格式良好,并且在短时间内消失。效果很好,但可能在 15 个字后文本溢出:

enter image description here

我以为可以通过设置字体大小来解决。在 JavaFX 中,您可以通过

Node node = new Node();
node.setStyle("-fx-font-size:___pt");//or px,rem,em.

如果可能,您将如何更改该消息的样式?

请求的最小可重现示例:

import javafx.application.Application;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.Border;
import javafx.scene.layout.borderpane;
import javafx.stage.Stage;

import java.io.IOException;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws IOException {
        Parent root = new borderpane();
     
        primaryStage.setFullScreen(true);
        primaryStage.setFullScreenExitHint("""
                Welcome to minesweeper. At left,you can alter the # of rows and columns. At right,set the # of mines. Play is bottom right.
                                
                                
                                
                                
                _
                """);

        Scene scene = new Scene(root);

        primaryStage.setScene(scene);
        primaryStage.show();


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

}

解决方法

尝试重用现有功能并不是一个坏主意。但不幸的是,这不能在这里应用,因为您无法控制全屏显示的消息框。即使通过节点查找/遍历来找到正确的节点,也无法做到这一点,因为消息框不是场景图的一部分。

您可能想知道它是如何呈现的,它是 Scene 中的内部实现,将框直接提供给“Painter”以在屏幕上绘制。

因此,我建议您不要依赖“setFullScreenExitHint”方法,而是建议您创建自己的消息框,以便您可以完全控制所有参数(显示持续时间、位置、字体大小、样式...)。>

以下是全屏显示的自定义消息框的完整工作演示:

enter image description here

import javafx.animation.FadeTransition;
import javafx.animation.PauseTransition;
import javafx.animation.SequentialTransition;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Popup;
import javafx.stage.Stage;
import javafx.util.Duration;

public class FullScreenStageDemo extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        StackPane root = new StackPane();
        // Adding a busy background,to showcase the transparency of the message box.
        root.setStyle("-fx-background-color: linear-gradient(from 41px 34px to 50px 50px,reflect,#ff7f50 30%,#faebd7 47%);");
        primaryStage.setScene(new Scene(root));
        primaryStage.setTitle("Full Screen Stage");
        primaryStage.setFullScreen(true);
        primaryStage.setFullScreenExitHint(""); // Setting empty string will not show the default message box
        primaryStage.setOnShown(e->{
            Duration displayDuration = Duration.millis(5000); // Adjust as per your need

            Label msg = new Label("Welcome to Minesweeper. At left,you can alter the # of rows and columns. At right,set the # of mines. Play is bottom right.");
            msg.setWrapText(true);
            msg.setStyle("-fx-font-size:15px;-fx-font-family:verdana;-fx-text-fill:#FFFFFF;");

            StackPane box = new StackPane(msg);
            box.setMaxWidth(500);
            box.setStyle("-fx-padding:20px;-fx-background-radius:5px,4px;-fx-background-color:#33333380,#AAAAAA60;-fx-background-insets:0,2;");

            Popup popup = new Popup();
            popup.getContent().add(box);
            popup.show(primaryStage);

            PauseTransition pause = new PauseTransition(displayDuration);
            FadeTransition fade = new FadeTransition(Duration.millis(1000),box);
            fade.setFromValue(1);
            fade.setToValue(0);

            SequentialTransition overlayTransition = new SequentialTransition();
            overlayTransition.getChildren().addAll(pause,fade);
            overlayTransition.setOnFinished(event -> popup.hide());
            overlayTransition.play();
        });
        primaryStage.show();
    }
}