鍍金池/ 教程/ Java/ JavaFX文本
安裝e(fx)clipse到Eclipse (JavaFX工具)
JavaFX屬性
JavaFX文本域(輸入框)
JavaFX切換按鈕
JavaFX曲線
JavaFX教程
JavaFX菜單(Menu)
JavaFX快速入門
JavaFX復選框
JavaFX ScrollPane布局
JavaFX綁定
JavaFX顏色選擇器(ColorPicker)
JavaFX進度指示器
JavaFX按鈕
JavaFX TitledPane布局
JavaFX圓弧
JavaFX開發(fā)環(huán)境安裝配置
在Eclipse安裝JavaFX Scene Builder
JavaFX路徑
JavaFX VBox
JavaFX線條
JavaFX漸變顏色
JavaFX集合
JavaFX BorderPane布局
JavaFX DatePicker
JavaFX單選按鈕
JavaFX滾動條
JavaFX矩形橢圓
JavaFX GridPane布局
JavaFX HBox
JavaFX進度條
JavaFX多邊形折線
JavaFX超鏈接
JavaFX密碼字段
JavaFX Accordion布局
JavaFX概述和簡介
JavaFX選擇框
JavaFX文本
JavaFX顏色
JavaFX文件選擇器(FileChooser)
JavaFX標簽
JavaFX FlowPane布局

JavaFX文本

另一個基本的JavaFX節(jié)點是Text節(jié)點,它允許我們在場景圖上顯示文本。要創(chuàng)建Text節(jié)點,請使用javafx.scene.text.Text類。
所有JavaFX場景節(jié)點都從javafx.scene.Node中擴展,并且它們繼承了許多功能,例如縮放,翻譯或旋轉的功能。

Text節(jié)點的直接父對象是javafx.scene.shape.Shape類??梢栽趦蓚€文本之間執(zhí)行幾何操作,如減法,相交或聯(lián)合。還可以使用文本剪輯視口區(qū)域。

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.Stage;
// by w Ww .YI  iB A I .C  o M
public class Main extends Application {
    public static void main(String[] args) {
        Application.launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Drawing Text");
        Group root = new Group();
        Scene scene = new Scene(root, 300, 250, Color.WHITE);
        int x = 100;
        int y = 100;
        int red = 30;
        int green = 40;
        int blue = 50;

        Text text = new Text(x, y, "JavaFX 2.0");

        text.setFill(Color.rgb(red, green, blue, .99));
        text.setRotate(60);
        root.getChildren().add(text);


        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

上面的代碼生成以下結果。

旋轉文本

請參考下面旋轉文本的代碼實現(xiàn) -

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.Stage;
// @ W W w . y IIB  A I.C  o M
public class Main extends Application {
    public static void main(String[] args) {
        Application.launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Drawing Text");
        Group root = new Group();
        Scene scene = new Scene(root, 300, 250, Color.WHITE);
        int x = 100;
        int y = 100;
        int red = 30;
        int green = 40;
        int blue = 50;

        Text text = new Text(x, y, "JavaFX 2.0");

        text.setFill(Color.rgb(red, green, blue, .99));
        text.setRotate(60);
        root.getChildren().add(text);

        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

上面的代碼生成以下結果。

文本字體

JavaFX的Font API使我們能夠更改字體樣式和字體大小。參考下面的代碼實現(xiàn)將文本加粗并設置為紅色 -

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.DropShadow;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class Main extends Application {
  public static void main(String[] args) {
    Application.launch(args);
  }

  @Override
  public void start(Stage primaryStage) {
    primaryStage.setTitle("");
    Group root = new Group();
    Scene scene = new Scene(root, 300, 250, Color.WHITE);

    Group g = new Group();


    Text t = new Text();
    t.setCache(true);
    t.setX(10.0);
    t.setY(70.0);
    t.setFill(Color.RED);
    t.setText("JavaFX");
    t.setFont(Font.font(null, FontWeight.BOLD, 32));
    g.getChildren().add(t);

    root.getChildren().add(g);
    primaryStage.setScene(scene);
    primaryStage.show();
  }
}

上面的代碼生成以下結果。

示例

實現(xiàn)使用CHOCOLATE顏色和Font.SERIF的文本

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class Main extends Application {

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

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Title");

        final Circle circ = new Circle(40, 40, 30);
        final Group root = new Group(circ);

        final Scene scene = new Scene(root, 800, 400, Color.BEIGE);

        final Text text1 = new Text(25, 25, "From: yiibai.com");
        text1.setFill(Color.CHOCOLATE);
        text1.setFont(Font.font(java.awt.Font.SERIF, 25));
        root.getChildren().add(text1);

        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

上面的代碼生成以下結果。

文字效果

DropShadow對象基于相對于Text節(jié)點的xy偏移量定位。因此可以設置文本陰影的顏色。

以下代碼顯示了如何使用DropShadow來繪制文本。

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.DropShadow;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
// from =>W w  w. y i i ba i.C  o M
public class Main extends Application {
  public static void main(String[] args) {
    Application.launch(args);
  }

  @Override
  public void start(Stage primaryStage) {
    primaryStage.setTitle("");
    Group root = new Group();
    Scene scene = new Scene(root, 300, 250, Color.WHITE);

    Group g = new Group();

    DropShadow ds = new DropShadow();
    ds.setOffsetY(3.0);
    ds.setColor(Color.color(0.4, 0.4, 0.4));

    Text t = new Text();
    t.setEffect(ds);
    t.setCache(true);
    t.setX(10.0);
    t.setY(70.0);
    t.setFill(Color.RED);
    t.setText("JavaFX drop shadow...");
    t.setFont(Font.font(null, FontWeight.BOLD, 32));
    g.getChildren().add(t);



    root.getChildren().add(g);
    primaryStage.setScene(scene);
    primaryStage.show();
  }
}

上面的代碼生成以下結果。

實例-2

使用0.7f作為setFraction()方法參數(shù)并調(diào)用此方法,本質上是指定所希望顯示70%的反射。
以下代碼顯示如何在文本上使用反射效果。

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.Reflection;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
// from =>W W W.yII b Ai . c  o M
public class Main extends Application {
    public static void main(String[] args) {
        Application.launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("");
        Group root = new Group();
        Scene scene = new Scene(root, 300, 250, Color.WHITE);

        Text t = new Text();
        t.setX(10.0);
        t.setY(50.0);
        t.setCache(true);
        t.setText("Reflections on JavaFX...");
        t.setFill(Color.RED);
        t.setFont(Font.font(null, FontWeight.BOLD, 30));

        Reflection r = new Reflection();
        r.setFraction(0.7);

        t.setEffect(r);

        root.getChildren().add(t); 
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

反射值范圍從0(0%)到1(100%)。還可以通過setTopOffset()方法設置不透明節(jié)點部分和反射部分之間的空間。頂部偏移默認為0
上面的代碼生成以下結果。

實例-3

以下代碼顯示如何使用行分隔符對文本執(zhí)行換行。

import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.InnerShadow;
import javafx.scene.effect.InnerShadowBuilder;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.scene.text.TextBuilder;
import javafx.stage.Stage;

public class Main extends Application {
    public static void main(String[] args) {
        Application.launch(args);
    }// at W W  w.y i  I   b AI .C  o  m 
    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Keyboard");
        Group root = new Group();
        Scene scene = new Scene(root, 530, 300, Color.WHITE);

        final StringProperty statusProperty = new SimpleStringProperty();

        InnerShadow iShadow = InnerShadowBuilder.create()
                .offsetX(3.5f)
                .offsetY(3.5f)
                .build();
        final Text status = TextBuilder.create()
            .effect(iShadow)
            .x(100)
            .y(50)
            .fill(Color.LIME)
            .font(Font.font(null, FontWeight.BOLD, 35))
            .translateY(50)
            .build();
        status.textProperty().bind(statusProperty);
        statusProperty.set("Line\nLine2\nLine3");
        root.getChildren().add(status);

        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

上面的代碼生成以下結果。

實例-4

以下代碼顯示如何設置文本換行寬度。

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

public class Main extends Application {

  @Override
  public void start(Stage stage) {
    Group root = new Group();
    Scene scene = new Scene(root, 300, 150);
    stage.setScene(scene);
    stage.setTitle("Sample");

    Text t = new Text(10, 50, "This is a test");
    t.setWrappingWidth(200);
    t.setText("First row Second row Second row Second row Second row Second row ");
    t.setFont(new Font(20));
    root.getChildren().add(t);
    stage.show();
  }

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

上面的代碼生成以下結果。