设置全屏退出提示的样式和字体大小,JavaFX
Set Style & Font Size of Full Screen Exit Hint, JavaFX
您熟悉“按 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);
}
}
尝试重用现有功能是个不错的主意。但不幸的是,这不能在这里应用,因为您无法控制全屏显示的消息框。即使使用节点 lookup/traversing 也无法找到正确的节点,因为消息框不是场景图的一部分。
您可能想知道它是如何渲染的,它是 Scene 中的内部实现,将框直接提供给“Painter”以在屏幕上绘制它。
因此,与其依赖“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();
}
}
您熟悉“按 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);
}
}
尝试重用现有功能是个不错的主意。但不幸的是,这不能在这里应用,因为您无法控制全屏显示的消息框。即使使用节点 lookup/traversing 也无法找到正确的节点,因为消息框不是场景图的一部分。
您可能想知道它是如何渲染的,它是 Scene 中的内部实现,将框直接提供给“Painter”以在屏幕上绘制它。
因此,与其依赖“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();
}
}