JavaFX Label

0

JavaFX Label Tutorials :

javafx label :

  • Label class in javafx used to display a text element.
  • Label object is a component for placing text in a container.
  • Label displays a single line of read-only text.

The constructors for label are

Label label = new Label();  
//constructs an empty label
Label label = new Label("string"); 
//constructs a label with specified string

Example of javafx label:


package javafxtuts;


import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Font;
import javafx.stage.Stage;


/**
 *
 * @author javafxtuts
 */
public class Javafxtuts extends Application {
    
    

  @Override
  public void start(Stage primaryStage) {
      primaryStage.setTitle("javafxtuts.com");
      double width = 400;
      double height = 300;
      GridPane root = new GridPane();
      Label label = new Label();
      root.setPadding(new Insets(10));
      root.getChildren().add(label);
      label.setText("javafxtuts.com");
      label.setFont(new Font("Arial", 30));
      
      Scene scene = new Scene(root, width, height);
      primaryStage.setScene(scene);
      primaryStage.show();
  }
  

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
    
}

Output :

Explanation of label example:

    • To creating labels we use AS

Label label = new Label();

    • setText() method is used to set text to the label as

label.setText(“javafxtuts.com”);

    • setFont() method is used to set the font of label.

Applying Effect :

  • The following code rotates a label 45 degrees and translates its position vertically.

Label label = new Label("Javafxtuts.com");
label.setRotate(45);
label.setTranslateY(50);
Share.

Leave A Reply