📅  最后修改于: 2020-10-14 01:19:25             🧑  作者: Mango
在某些情况下,我们需要在应用程序的界面上提供基于文本的信息。 JavaFX库为此提供了一个名为javafx.scene.text.Text的类。此类提供了多种方法来更改文本的各种属性。我们只需要实例化此类即可在应用程序中实现文本。
下表描述了JavaFX Text的属性。
Property | Description | Setter Methods |
---|---|---|
boundstype | This property is of object type. It determines the way in which the bounds of the text is being calculated. | setBoundsType(TextBoundsType value) |
font | Font of the text. | setFont(Font value) |
fontsmoothingType | Defines the requested smoothing type for the font. | setFontSmoothingType(FontSmoothingType value) |
linespacing | Vertical space in pixels between the lines. It is double type property. | setLineSpacing(double spacing) |
strikethrough | This is a boolean type property. We can put a line through the text by setting this property to true. | setStrikeThrough(boolean value) |
textalignment | Horizontal Text alignment | setTextAlignment(TextAlignment value) |
textorigin | Origin of text coordinate system in local coordinate system. | setTextOrigin(VPos value) |
text | It is a string type property. It defines the text string which is to be displayed. | setText(String value) |
underline | It is a boolean type property. We can underline the text by setting this property to true. | setUnderLine(boolean value) |
wrappingwidth | Width limit for the text from where the text is to be wrapped. It is a double type property. | setWrappingWidth(double value) |
x | X coordinate of the text | setX(double value) |
y | Y coordinate of the text | setY(double value) |
为了创建文本节点,需要实例化javafx.scene.text.Text类。使用setter方法setText(字符串)将字符串设置为文本类对象的文本。请按照下面给出的语法实例化Text类。
Text = new Text();
text.setText();
下面的示例说明了Text类。这里,我们没有设置文本的位置,因此文本将显示在屏幕中央。
package application;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class TextExample extends Application{
@Override
public void start(Stage primaryStage) throws Exception {
// TODO Auto-generated method stub
Text text = new Text();
text.setText("Hello !! Welcome to JavaTPoint");
StackPane root = new StackPane();
Scene scene = new Scene(root,300,400);
root.getChildren().add(text);
primaryStage.setScene(scene);
primaryStage.setTitle("Text Example");
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
JavaFX使我们能够将各种字体应用于文本节点。我们只需要使用setter方法setFont()设置Text类的属性字体。此方法接受Font类的对象。 Font类属于包javafx.scene.text。它包含一个名为font()的静态方法。这将返回一个Font类型的对象,该对象将作为参数传递给Text类的setFont()方法。方法Font.font()接受以下参数。
setFont()方法的语法如下。
.setFont(Font.font(, , , )
package application;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class TextExample extends Application{
@Override
public void start(Stage primaryStage) throws Exception {
// TODO Auto-generated method stub
Text text = new Text();
text.setX(100);
text.setY(20);
text.setFont(Font.font("Abyssinica SIL",FontWeight.BOLD,FontPosture.REGULAR,20));
text.setText("Welcome to JavaTPoint");
Group root = new Group();
Scene scene = new Scene(root,500,200);
root.getChildren().add(text);
primaryStage.setScene(scene);
primaryStage.setTitle("Text Example");
primaryStage.show();
}
publicstaticvoid main(String[] args) {
launch(args);
}
}
笔触是指在文本边界处的填充。 JavaFX允许我们将笔触和颜色应用于文本。 javafx.scene.text.Text类提供了一个名为setStroke()的方法,该方法接受Paint类对象作为参数。只需传递将在笔触上绘制的颜色即可。我们还可以通过将double类型的宽度值传递给setStrokeWidth()方法来设置笔划的宽度。要设置文本的颜色,javafx.scene.text.Text类提供了另一个名为setFill()的方法。我们只需要传递文本中要填充的颜色即可。
以下示例说明了上述方法的功能。
package application;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class TextExample extends Application{
@Override
publicvoid start(Stage primaryStage) throws Exception {
// TODO Auto-generated method stub
Text text = new Text();
text.setX(100);
text.setY(20);
text.setFont(Font.font("Abyssinica SIL",FontWeight.BOLD,FontPosture.REGULAR,25));
text.setFill(Color.BLUE);// setting colour of the text to blue
text.setStroke(Color.BLACK); // setting the stroke for the text
text.setStrokeWidth(1); // setting stroke width to 2
text.setText("Welcome to JavaTPoint");
Group root = new Group();
Scene scene = new Scene(root,500,200);
root.getChildren().add(text);
primaryStage.setScene(scene);
primaryStage.setTitle("Text Example");
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
我们可以通过设置javafx.scene.text.Text类的属性删除线和下划线来将修饰应用于文本。这两种方法的语法如下。
.setStrikeThrough(Boolean value) //pass true to put a line across the text
.setUnderLine(Boolean value) //pass true to underline the text
我们还可以将JavaFX Effects应用于Text类对象。我们将在接下来的章节中讨论JavaFX效果。
package application;
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 TextExample extends Application{
@Override
public void start(Stage primaryStage) throws Exception {
// TODO Auto-generated method stub
Text text = new Text();
text.setX(100);
text.setY(40);
text.setFont(Font.font("Liberation Serif",25));
text.setText("Hello !!");
text.setStrikethrough(true);
Text text1=new Text();
text1.setX(100);
text1.setY(140);
text1.setText("Welcome to JavaTPoint!");
text1.setFont(Font.font("Liberation Serif",25));
text1.setUnderline(true);
Group root = new Group();
Scene scene = new Scene(root,500,200);
root.getChildren().addAll(text,text1);
primaryStage.setScene(scene);
primaryStage.setTitle("Text Example");
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}