📜  JavaFX |字体姿势类

📅  最后修改于: 2022-05-13 01:54:26.018000             🧑  作者: Mango

JavaFX |字体姿势类

FontPosture 类是 JavaFX 的一部分。 FontPosture 类指定字体是 REGULAR 还是 ITALIC。 FontPosture 类继承Enum 类

常用方法:

MethodExplanation
findByName(String n)Returns FontPosture by its name.
valueOf(String n)Returns a FontPosture with the specified name.
values()returns an array with all the elements of the FontPosture type.

创建字体对象并设置指定姿势并将其应用于文本的Java程序:在此程序中,我们将创建两个 Text 对象并将一个字体姿势设置为REGULAR ,另一个设置为ITALIC 。然后将文本设置为TextFlow并将此文本流添加到VBox并将vbox添加到场景中,并将场景添加到舞台。此外,设置文本流的行距和文本对齐方式以及 vbox 的间距。调用show()函数以显示最终结果。

// Java Program to create a font object and set
// a specified posture and  apply it to a text
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.scene.layout.*;
import javafx.scene.paint.*;
import javafx.scene.text.*;
import javafx.geometry.*;
import javafx.scene.layout.*;
import javafx.scene.shape.*;
  
public class FontPosture_1 extends Application {
  
    // launch the application
    public void start(Stage stage)
    {
  
        try {
  
            // set title for the stage
            stage.setTitle("FontPosture");
  
            // create TextFlow
            TextFlow text_flow = new TextFlow();
  
            // create text
            Text text_1 = new Text("GeeksforGeeks\n");
  
            // set the text color
            text_1.setFill(Color.GREEN);
  
            // create a font
            Font font = Font.font("Verdana", FontWeight.EXTRA_BOLD,
                                          FontPosture.REGULAR, 25);
  
            // set font of the text
            text_1.setFont(font);
  
            // create text
            Text text_2 = new Text("A Computer Science portal for geeks\n");
  
            // set the text color
            text_2.setFill(Color.GREEN);
  
            // create a font
            Font font1 = Font.font("Verdana", FontWeight.EXTRA_BOLD,
                                            FontPosture.ITALIC, 12);
  
            // set font of the text
            text_2.setFont(font1);
  
            // set text
            text_flow.getChildren().add(text_1);
            text_flow.getChildren().add(text_2);
  
            // set line spacing
            text_flow.setLineSpacing(20.0f);
  
            // set text alignment
            text_flow.setTextAlignment(TextAlignment.CENTER);
  
            // create VBox
            VBox vbox = new VBox(text_flow);
  
            // set alignment of vbox
            vbox.setAlignment(Pos.CENTER);
  
            // create a scene
            Scene scene = new Scene(vbox, 400, 300);
  
            // set the scene
            stage.setScene(scene);
  
            stage.show();
        }
  
        catch (Exception e) {
  
            System.out.println(e.getMessage());
        }
    }
  
    // Main Method
    public static void main(String args[])
    {
  
        // launch the application
        launch(args);
    }
}

输出:

注意:以上程序可能无法在在线 IDE 中运行,请使用离线编译器。

参考: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/text/FontPosture.html