创建具有不同样式的大量文本 - JavaFX FXML [英] Creating a large body of text with different styles - JavaFX FXML

查看:186
本文介绍了创建具有不同样式的大量文本 - JavaFX FXML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的JavaFx应用程序的fxml类中,我想使用最少的组件添加大量文本(而不是每行添加多个标签)。我还想在同一个组件中创建不同样式的文本。我应该使用什么组件(例如TextArea)以及如何在其中创建多个样式(使用css)。

In the fxml class of my JavaFx application I want to add a large body of text using minimal components (instead of adding multiple labels per line). I would also like to create varying styles of text in the same component. What component should I use (e.g TextArea) and how would I go about create multiple styles inside it (using css).

推荐答案

使用 TextFlow 并添加文字。您可以使用css为不同的样式设置单个文本组件的样式。

Use a TextFlow and add Text to it. You can style individual Text component with different style using css on them.

完整示例:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.text.Text;
import javafx.scene.text.TextFlow;
import javafx.stage.Stage;

public class TextFlowExample extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        Text text1 = new Text("First Text\n");
        text1.setStyle("-fx-font-size: 20; -fx-fill: darkred;");
        Text text2 = new Text("\nSecond Text");
        text2.setStyle("-fx-font-size: 30; -fx-fill: goldenrod;");
        TextFlow textFlow = new TextFlow(text1, text2);
        primaryStage.setScene(new Scene(textFlow, 200, 200));
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

输出

等效FXML

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.layout.*?>
<?import java.lang.*?>
<?import javafx.scene.text.*?>


<TextFlow lineSpacing="10.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" textAlignment="CENTER" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <Text strokeType="OUTSIDE" strokeWidth="0.0" style="-fx-font-size: 20; -fx-fill: darkred;" text="&#10;&#10;First Text" />
      <Text strokeType="OUTSIDE" strokeWidth="0.0" style="-fx-font-size: 30; -fx-fill: goldenrod;" text="&#10;&#10;Second Text" />
   </children>
</TextFlow>

这篇关于创建具有不同样式的大量文本 - JavaFX FXML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆