【问题标题】:Make portion of a text bold in a JavaFx Label or Text在 JavaFx 标签或文本中将部分文本加粗
【发布时间】:2012-09-02 17:28:55
【问题描述】:

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

String s = "This is a <b>Sample</b> sentence"
Label label = new Label(s);

输出

这是一个示例句子

JavaFx Text 也不允许这样做。是否有任何组件可以让我将部分文本以粗体显示?

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

【问题讨论】:

  • 请注意,对于一个相关的问题,您可以使用 \n 而不是
    让文本多行

标签: javafx javafx-2


【解决方案1】:

更新: JavaFX 8 为富文本提供了新控件:TextFlow


很遗憾,2.2 中没有这样的功能,尽管它可能会包含在下一个版本中。

现在您可以尝试使用以下方法:

  1. 具有多个 LabelText 组件的 HBox
  2. 网页视图
  3. 绘制了多个文本组件的画布

【讨论】:

    【解决方案2】:

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

    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 容器会自动包装内容 Text 节点。

    【讨论】:

      【解决方案3】:

      由于之前的答案没有包含 FXML 代码,我会再发布一个。

      按照@Ernisto 的建议,您可以使用包含Text 部分的TextFlow,其中每个部分的样式都可以不同。

      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>
      

      输出

      【讨论】:

      • 我还没有找到-fx-stroke 是否适用于Text 但它不适用于Label - 我发现了其他建议-fx-text-fill 的答案对我有用。 (OpenJFX 14)
      【解决方案4】:
      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");
      

      【讨论】:

      • 太复杂了
      • 是的,复制粘贴很复杂。
      猜你喜欢
      • 1970-01-01
      • 2014-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-07
      • 1970-01-01
      • 2020-05-15
      • 1970-01-01
      相关资源
      最近更新 更多