Javafx 简明教程

JavaFX - Label

Label 是文本的一部分,它描述或告诉用户有关应用程序中其他元素功能的信息。它有助于减少干扰并且提供清晰性,从而带来更好的用户体验。始终记得,它不是一个可编辑文本控件。在下图中,我们可以在红色方框中看到一个按钮,还有一些描述其用途的文本 −

A Label is a piece of text that describe or informs users about the functionality of other elements in the application. It helps in reducing confusion and provides clarity which leads to a better user experience. Always remember, it is a not an editable text control. In the figure below, we can see a button in red box and there are some text describing its purpose −

sample label

Label in JavaFX

在 JavaFX 中,标签由属于 javafx.scene.control 包的 Label 类表示。要在 JavaFX 应用程序中创建标签,我们可以使用以下任何构造函数 −

In JavaFX, the label is represented by a class named Label which belongs to the javafx.scene.control package. To create a label in JavaFX application, we can use any of the below constructor −

  1. Label() − It is the default constructor that constructs an empty label.

  2. Label(String str) − It constructs a label with the predefined text.

  3. Label(String str, Node graph) − It constructs a new label with the specified text and graph.

Steps to create a Label in JavaFX

要在 JavaFX 中创建标签,请按照以下步骤操作 −

To create a Label in JavaFX, follow the steps given below −

Step 1: Instantiate the Label class

如前所述,我们需要实例化 Label 类来创建标签文本。我们可以使用其默认构造函数或参数化构造函数。如果使用默认构造函数,则通过 setText() 方法添加标签文本。

As discussed earlier, we need to instantiate the Label class to create a label text. We can use either its default constructor or parameterized constructor. If we use the default one, the label text is added by using the setText() method.

// Instanting the Label class
Label label = new Label("Sample label");

Step 2: Set the required properties of Label

就像文本节点一样,我们可以使用 setFont() 方法和 setFill() 方法分别为 JavaFX 中的标签节点设置所需属性,如字体和字体颜色。

Just like a text node we can set the desired properties like font and font color to the label node in JavaFX using the setFont() method and setFill() method respectively.

// 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);

Step 4: Launching Application

一旦创建了标签并设置其属性,请定义一个 group 对象来保存标签。接下来,通过将其组对象和场景的尺寸传递到其构造函数来创建一个 Scene 对象。然后,设置舞台并启动应用程序以显示结果。

Once the Label is created and its properties are set, define a group object to hold the label. Next, create a Scene object by passing the group obejct and the dimensions of the Scene to its constructor. Then, set the stage and launch the application to display the result.

Example

在以下示例中,我们将在 JavaFX 应用程序中创建一个标签。将此代码另存为名称为 JavafxLabel.java 的文件。

In the following example, we are going to create a Label in JavaFX application. Save this code in a file with the name JavafxLabel.java.

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 JavafxLabel 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, 400, 300, Color.BEIGE);
      stage.setTitle("Label Example");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

要从命令提示符编译并执行已保存的 Java 文件,请使用以下命令−

To compile and execute the saved Java file from the command prompt, use the following commands −

javac --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxLabel.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxLabel

当我们执行上述代码时,它将生成一个标签文本,如下面的输出所示。

When we execute the above code, it will generate a Label text as shown in the following output.

label output