如何使用 JavaFX 创建标签?
使用 Label 组件,可在用户界面上显示文本元素/图像。这是一个不可编辑的文本控件,主要用于指定应用程序中其他节点的目的。
在 JavaFX 中,可通过实例化 javafx.scene.control.Label 类来创建标签。
就像文本节点一样,可以使用 setFont() 方法为 JavaFX 中的文本节点设置所需的字体,还可以使用 setFill() 方法为其添加颜色。
要创建标签 -
实例化 Label 类。
为其设置所需属性。
将标签添加到场景。
示例
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
public class LabelExample extends Application {
public void start(Stage stage) {
//Creating a Label
Label label = new Label("Sample label");
//Setting font to the label
Font font = Font.font("Brush Script MT", FontWeight.BOLD, FontPosture.REGULAR, 25);
label.setFont(font);
//Filling color to the label
label.setTextFill(Color.BROWN);
//Setting the position
label.setTranslateX(150);
label.setTranslateY(25);
Group root = new Group();
root.getChildren().add(label);
//Setting the stage
Scene scene = new Scene(root, 595, 150, Color.BEIGE);
stage.setTitle("Label Example");
stage.setScene(scene);
stage.show();
}
public static void main(String args[]){
launch(args);
}
}输出

广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP