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

设置样式&;全屏退出提示的字体大小,JavaFX,javafx,fullscreen,font-size,exit,stage,Javafx,Fullscreen,Font Size,Exit,Stage,您熟悉“按esc退出全屏”消息 在JavaFX中,有以下方法: Stage stage = new Stage(); stage.setFullScreenExitHint("whatever you want it to say, instead of 'press esc to exit full screen.'); 我用它来为用户显示一条指导信息,只是因为它很简单,预先制作好,格式很好,很快就会消失。它工作得很好,但在15个单词之后,文本溢出: 我想可以通过设置字体大小来修复

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

在JavaFX中,有以下方法:

Stage stage = new Stage();
stage.setFullScreenExitHint("whatever you want it to say, instead of 'press esc to exit full screen.');
我用它来为用户显示一条指导信息,只是因为它很简单,预先制作好,格式很好,很快就会消失。它工作得很好,但在15个单词之后,文本溢出:

我想可以通过设置字体大小来修复。在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);
    }

}

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

您可能想知道它是如何渲染的,它是场景中的内部实现,直接向“画师”提供长方体以在屏幕上绘制它

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

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

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