📜  JavaFX |文本对齐类

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

JavaFX |文本对齐类

TextAlignment 类是 JavaFX 的一部分。 TextAlignment 类表示水平文本对齐方式。 TextAlignment 类继承 Enum 类。

常用方法:

MethodExplanation
valueOf(String n)Returns the text Alignment of specified name.
values()Returns an array which contains all the values of text alignment.

示例: Java程序创建一个 TextFlow 并向其添加文本对象,设置文本对齐方式并设置一个组合框以更改对齐方式并设置文本流的行距:在此程序中,我们将创建一个名为 tile_pane 的TilePane 。将名为 label 的标签和一些按钮添加到tile_pane 。使用setAlignment()函数设置tile_pane的对齐方式。将 TextAlignment 值的所有名称存储在字符串数组中。现在创建一个组合框,其中将包含 TextAlignment 值的名称,并创建一个动作事件来处理组合框事件。事件处理程序会将 TextFlow 的 TextAlignment 设置为所选的 TextAlignment 值。现在创建一个 VBox 并将tilepane和组合框添加到vbox 。最后,将vbox添加到场景中并将场景添加到舞台并调用show()函数显示最终结果。

// Java program to create a TextFlow and
// add text object to it, set text Alignment
// and also set a combo box to change Alignment
// and set line spacing of the text flow.
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.*;
import javafx.collections.*;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
   
public class Alignment_1 extends Application {
   
// launch the application
public void start(Stage stage)
{
   
    try {
   
        // set title for the stage
        stage.setTitle("Alignment");
   
        // 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);
   
        // set font of the text
        text_1.setFont(Font.font("Verdana", 25));
   
        // create text
        Text text_2 = new Text("How many times were you frustrated "+ 
                               "while looking out for a good "+
                               "collection of programming/algorithm/ "+
                               "interview questions? What did you "+ 
                               "expect and what did you get? "+ 
                               "This portal has been created to "+ 
                               "provide well written, well "+ 
                               "thought and well explained solutions "+
                                            "for selected questions.");
   
        // set the text color
        text_2.setFill(Color.BLUE);
   
        // set font of the text
        text_2.setFont(Font.font("Helvetica", 
                    FontPosture.ITALIC, 15));
   
        // add text to textflow
        text_flow.getChildren().add(text_1);
        text_flow.getChildren().add(text_2);
   
        // alignment names
        String weight[] = { "CENTER", "JUSTIFY", "LEFT", "RIGHT" };
   
        // Create a combo box
        ComboBox combo_box = 
         new ComboBox(FXCollections.observableArrayList(weight));
   
        // Create action event
        EventHandler event = new EventHandler() 
        {
   
            public void handle(ActionEvent e)
            {
                   
                // set alignment
                text_flow.setTextAlignment(TextAlignment.valueOf(
                                  (String)combo_box.getValue()));
            }
        };
   
        // Set on action
        combo_box.setOnAction(event);
   
        // set text Alignment
        text_flow.setTextAlignment(TextAlignment.CENTER);
   
        // set line spacing
        text_flow.setLineSpacing(20.0f);
   
        // create VBox
        VBox vbox = new VBox(combo_box, 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/TextAlignment.html