在JavaFx标签或文本中加粗文本部分

在JavaFx标签或文本中加粗文本部分,javafx,javafx-2,Javafx,Javafx 2,在我的JavaFx应用程序中,我需要在整个句子中用黑体字呈现一两个单词。目前,该句子呈现为JavaFx标签,但升级组件也不允许我将文本设置为以粗体显示单词“Sample” String s = "This is a <b>Sample</b> sentence" Label label = new Label(s); String s=“这是一个示例句子” 标签=新标签; 输出 This is a Sample sentence 这是一个例句 JavaFXText也不

在我的JavaFx应用程序中,我需要在整个句子中用黑体字呈现一两个单词。目前,该句子呈现为JavaFx标签,但升级组件也不允许我将文本设置为以粗体显示单词“Sample”

String s = "This is a <b>Sample</b> sentence"
Label label = new Label(s);
String s=“这是一个示例句子”
标签=新标签;
输出

This is a Sample sentence 这是一个例句 JavaFXText也不允许这样做。是否有任何组件,我可以有一部分黑体文字


我不确定JavaFx WebView是否适合在窗口中呈现许多小句子。

更新:JavaFx 8为富文本提供了新的控件:


不幸的是,2.2中没有这样的功能,尽管它可能会包含在下一个版本中

目前,您可以尝试使用以下方法:

  • 带有多个
    标签
    文本
    组件的HBox
  • 网络视图
  • 绘制多个文本组件的画布

  • 可以使用JavaFX8中的
    TextFlow
    容器。 然后,您可以轻松地在其中添加不同样式的
    文本
    节点

    TextFlow flow = new TextFlow();
    
    Text text1=new Text("Some Text");
    text1.setStyle("-fx-font-weight: bold");
    
    Text text2=new Text("Some Text");
    text2.setStyle("-fx-font-weight: regular");
    
    flow.getChildren().addAll(text1, text2);
    
    TextFlow容器将自动包装内容文本节点


    由于前面的答案没有包含FXML代码,我将发布一个额外的答案

    正如@Ernisto所建议的,您可以使用一个包含部分的,其中每个部分的样式都可以不同

    示例FXML文件内容

    <TextFlow>
      <Text text="Normal text and "/>
      <Text text="bold text and " style="-fx-font-weight: bold"/>
      <Text text="italic text and " style="-fx-font-style: italic"/>
      <Text text="red text." style="-fx-stroke: red"/>
    </TextFlow>
    
    
    
    输出


    请注意,对于相关问题,可以使用\n而不是
    在多行上显示文本,复制粘贴非常复杂。我还没有找到
    -fx stroke
    是否适用于
    文本
    ,但它不适用于
    标签
    -我找到了其他答案,建议使用
    -fx文本填充
    ,这对我有效。(OpenJFX14)
    public class UtilsDialog {
    
        private static final String TAG = "UtilsDialog";
    
        private static boolean sIsShowing = false;
    
        public static void showDialogShowError(String title, String msg, String defaultStyle,
                                               @Nullable String customStyle, String... styledWords) {
            if (sIsShowing) return;
    
            Stage dialogStage = new Stage(StageStyle.UTILITY);
            dialogStage.initModality(Modality.APPLICATION_MODAL);
            dialogStage.setWidth(400);
            dialogStage.setHeight(220);
    
            BorderPane borderPane = new BorderPane();
    
            borderPane.setPadding(new Insets(15));
            borderPane.setPrefWidth(Integer.MAX_VALUE);
            borderPane.setPrefHeight(Integer.MAX_VALUE);
    
            Scene scene = new Scene(borderPane);
            dialogStage.setScene(scene);
            sIsShowing = true;
            dialogStage.show();
            UtilsGui.closeOnEsc(borderPane, scene);
            scene.addEventHandler(KeyEvent.KEY_PRESSED, t -> {
                if (t.getCode() == KeyCode.ESCAPE) {
                    sIsShowing = false;
                }
            });
    
            // Top
            Text textTitle = new Text(title);
            textTitle.setStyle("-fx-font-size: 18px;");
    
            HBox hBoxTop = new HBox(10);
            hBoxTop.getChildren().addAll(textTitle);
            borderPane.setTop(hBoxTop);
    
            // Center
            TextFlow textFlow = new TextFlow();
            List<String> words = Arrays.asList(msg.split(" "));
            List<String> styledWordsList = Arrays.asList(styledWords);
            for (String word : words) {
                Text tmpWord = new Text(word);
                if (styledWordsList.contains(word
                        .replace(".", "")
                        .replace(",", "")
                        .replace("?", "")
                        .replace("!", "")
                        .replace(";", "")
                        .replace("\n", "")
                )) {
    
                    tmpWord.setStyle(customStyle);
                } else {
                    if (defaultStyle == null) {
                        tmpWord.setStyle("");
                    } else {
                        tmpWord.setStyle(defaultStyle);
                    }
                }
                tmpWord.setText(tmpWord.getText());
                textFlow.getChildren().add(tmpWord);
                textFlow.getChildren().add(new Text(" "));
            }
            Text textMsg = new Text(msg);
            textMsg.setStyle("-fx-font-size: 14px;");
            HBox hBoxInputPane = new HBox(10);
            hBoxInputPane.setAlignment(Pos.CENTER);
    
            VBox vBoxCenter = new VBox(10);
            vBoxCenter.setPadding(new Insets(25, 0, 15, 0));
            vBoxCenter.getChildren().addAll(textFlow);
            borderPane.setCenter(vBoxCenter);
    
            JFXButton btnOk = new JFXButton("OK");
            btnOk.setAlignment(Pos.CENTER_RIGHT);
            btnOk.setStyle("-fx-text-fill: WHITE; -fx-background-color: #5264AE; -fx-font-size: 14px;");
            btnOk.setOnAction(event -> {
                sIsShowing = false;
                dialogStage.close();
            });
    
            // Bottom
            HBox hBoxBottom = new HBox();
            final Pane spacer = new Pane();
            HBox.setHgrow(spacer, Priority.ALWAYS);
            hBoxBottom.getChildren().addAll(spacer, btnOk);
            borderPane.setBottom(hBoxBottom);
    
            // store on close
            dialogStage.setOnCloseRequest(event -> sIsShowing = false);
        }
    }
    
    UtilsDialog.showDialogShowError("Test", "This is the message to show. Does it work?",
                    null, "-fx-font-weight: bold", "This", "message", "show");