当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


JavaFX 类 TextAlignment用法及代码示例


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

常用方法:

方法 说明
valueOf(String n) 返回指定名称的文本对齐方式。
values() 返回一个包含所有文本对齐方式值的数组。

例: Java程序创建一个TextFlow并向其中添加文本对象,设置文本Alignment并设置一个组合框以更改Alignment并设置文本流的行距:在此程序中,我们将创建一个名为tile_pane的TilePane。将名为label的Label和一些按钮添加到tile_pane。使用setAlignment()函数设置tile_pane的对齐方式。将所有TextAlignment值的名称存储在String数组中。现在创建一个组合框,其中将包含TextAlignment值的名称,还创建一个Action Event来处理组合框事件。事件处理程序会将TextFlow的TextAlignment设置为所选的TextAlignment值。现在创建一个VBox并将磁贴和组合框添加到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<ActionEvent> event = new EventHandler<ActionEvent>()  
        { 
   
            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



相关用法


注:本文由纯净天空筛选整理自andrew1234大神的英文原创作品 JavaFX | TextAlignment Class。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。