Javafx警报对话框+ HTML

Jem*_*sio 3 html formatting alert dialog javafx

我正在使用新的JavaFX Alert类(Java 1.8_40)并尝试在exibition文本中使用HTML标记,但到目前为止还没有成功.这是我正在尝试做的一个例子.

Alert alert = new Alert(AlertType.INFORMATION);
alert.setHeaderText("This is an alert!");
alert.setContentText("<html>Pay attention, there are <b>HTML</b> tags, here.</html>");
alert.showAndWait();
Run Code Online (Sandbox Code Playgroud)

谁能知道它是否真的有可能,并告诉我一个例子?

提前致谢.

Jam*_*s_D 6

我没有Alert太多使用新类,但我很确定文本属性不支持HTML格式.

您可以使用Web视图显示HTML格式的文本:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

public class AlertHTMLTest extends Application {

    @Override
    public void start(Stage primaryStage) {
        Button button = new Button("Show Alert");
        button.setOnAction(e -> {
            Alert alert = new Alert(AlertType.INFORMATION);
            alert.setHeaderText("This is an alert!");
            WebView webView = new WebView();
            webView.getEngine().loadContent("<html>Pay attention, there are <b>HTML</b> tags, here.</html>");
            webView.setPrefSize(150, 60);
            alert.getDialogPane().setContent(webView);;
            alert.showAndWait();
        });

        StackPane root = new StackPane(button);
        Scene scene = new Scene(root, 350, 75);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
Run Code Online (Sandbox Code Playgroud)