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

JavaFX標(biāo)簽

JavaFX APIjavafx.scene.control包中的Label類可用于顯示一個(gè)文本元素。
我們可以包裝文本元素以適應(yīng)特定空間,添加圖形圖像或使用JavaFX Label控件應(yīng)用視覺效果。

以下代碼顯示如何使用Label顯示文本。

import javafx.application.Application;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class Main extends Application {

  public static void main(String[] args) {
    Application.launch(args);
  }
// create w w W .Y i  ib A I. c  O M
  @Override
  public void start(Stage primaryStage) {
    Group root = new Group();
    Scene scene = new Scene(root, 300, 130, Color.WHITE);

    GridPane gridpane = new GridPane();
    gridpane.setPadding(new Insets(5));
    gridpane.setHgap(10);
    gridpane.setVgap(10);

    Label label = new Label("Label");
    GridPane.setHalignment(label, HPos.CENTER);
    gridpane.add(label, 0, 0);

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

創(chuàng)建標(biāo)簽

JavaFX API提供了Label類的三個(gè)構(gòu)造函數(shù)來創(chuàng)建標(biāo)簽。

//An empty label
Label label1 = new Label();

//A label with the text element
Label label2 = new Label("Name");

//A label with the text element and graphical icon
Image image = new Image(getClass().getResourceAsStream("labels.jpg"));
Label label3 = new Label("Name", new ImageView(image));

標(biāo)簽內(nèi)容

創(chuàng)建標(biāo)簽后,我們可以使用Label類中的以下方法添加文本和圖形內(nèi)容。

  • setText(String text) - 設(shè)置標(biāo)簽的文本標(biāo)題
  • setGraphic(Node graphic) - 設(shè)置圖形圖標(biāo)

setGraphicTextGap()方法設(shè)置文本和圖標(biāo)之間的間距。setTextFill()方法設(shè)置標(biāo)簽文本的顏色。以下代碼創(chuàng)建文本標(biāo)簽,向其添加圖標(biāo),并為文本設(shè)置填充顏色。

Label label1 = new Label("Name");
Image image = new Image(getClass().getResourceAsStream("icon.jpg"));
label1.setGraphic(new ImageView(image));
label1.setTextFill(Color.web("#FF76a3"));

以下代碼顯示如何設(shè)置Label Text顏色。

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class Main extends Application {
  public static void main(String[] args) {
    launch(args);
  }
// by W  w  W. y iIb a I. c O M
  @Override
  public void start(Stage stage) {
    Scene scene = new Scene(new Group());
    stage.setTitle("Label Sample");
    stage.setWidth(400);
    stage.setHeight(180);

    HBox hbox = new HBox();

    Label label1 = new Label("Search");
    label1.setTextFill(Color.web("#0076a3"));

     hbox.setSpacing(10);
    hbox.getChildren().add((label1));
    ((Group) scene.getRoot()).getChildren().add(hbox);

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

setTextAlignment()方法可以在其布局區(qū)域內(nèi)設(shè)置標(biāo)簽內(nèi)容的對(duì)齊方式。setContentDisplay()方法設(shè)置圖形相對(duì)于文本的位置。該方法接受以下ContentDisplay常量中的一個(gè):LFFT,RIGHT,CENTER,TOP,BOTTOM。

標(biāo)簽字體

如果未設(shè)置Label控件的字體,則使用默認(rèn)字體大小進(jìn)行渲染。要設(shè)置字體文本大小,請(qǐng)使用Label類中的setFont方法。

以下代碼將label1文本的大小設(shè)置為30點(diǎn)像素,將字體名稱設(shè)置為Arial。

label.setFont(new Font("Arial", 30));

將文本大小設(shè)置為32點(diǎn)像素,將字體名稱設(shè)置為Cambria。

label.setFont(Font.font("Cambria", 32));

以下代碼顯示如何設(shè)置標(biāo)簽的字體。

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;

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

  @Override
  public void start(Stage stage) {
    Scene scene = new Scene(new Group());
    stage.setWidth(400);
    stage.setHeight(180);

    HBox hbox = new HBox();// create  w W w .y  I I  bA  i.c OM 

    Label label1 = new Label("Search");
    label1.setFont(new Font("Arial", 30));

    hbox.setSpacing(10);
    hbox.getChildren().add((label1));
    ((Group) scene.getRoot()).getChildren().add(hbox);

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

包裝文本

要包裝文本以將文本適合布局區(qū)域,請(qǐng)使用setWrapText方法并設(shè)置為true值。

Label label = new Label("A long long long long long text");
label.setWrapText(true);

以下代碼顯示如何包裝Label。

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.text.TextAlignment;
import javafx.stage.Stage;

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

  @Override
  public void start(Stage stage) {
    Scene scene = new Scene(new Group());
    stage.setTitle("Label Sample");
    stage.setWidth(400);
    stage.setHeight(180);

    HBox hbox = new HBox();

    Label label1 = new Label("Search long long long long long long long long long ");
    label1.setPrefWidth(100);
    label1.setWrapText(true);

    hbox.setSpacing(10);
    hbox.getChildren().add((label1));
    ((Group) scene.getRoot()).getChildren().add(hbox);

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

當(dāng)不可能渲染文本字符串時(shí),我們可以使用setTextOverrun方法控制如何從標(biāo)簽渲染文本。setTextOverrun方法接受一個(gè)OverrunStyle值。

上面的代碼生成以下結(jié)果。

應(yīng)用效果

我們可以對(duì)Label控件應(yīng)用視覺效果或轉(zhuǎn)換。以下代碼將標(biāo)簽旋轉(zhuǎn)270度,并將其位置垂直平移。

Label label = new Label("Name");
label.setRotate(270);
label.setTranslateY(50);

以下代碼顯示如何使用旋轉(zhuǎn)創(chuàng)建垂直標(biāo)簽。

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.text.TextAlignment;
import javafx.stage.Stage;

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

  @Override
  public void start(Stage stage) {
    Scene scene = new Scene(new Group());
    stage.setTitle("Label Sample");
    stage.setWidth(400);
    stage.setHeight(180);

    HBox hbox = new HBox();

    Label label1 = new Label("Search");
    label1.setRotate(270);

    hbox.setSpacing(10);
    hbox.getChildren().add((label1));
    ((Group) scene.getRoot()).getChildren().add(hbox);

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

以下代碼顯示了如何使用setTranslateY來移動(dòng)標(biāo)簽。

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.text.TextAlignment;
import javafx.stage.Stage;

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

  @Override
  public void start(Stage stage) {
    Scene scene = new Scene(new Group());
    stage.setTitle("Label Sample");
    stage.setWidth(400);
    stage.setHeight(180);

    HBox hbox = new HBox();

    Label label1 = new Label("Search");
    label1.setTranslateY(50);

    hbox.setSpacing(10);
    hbox.getChildren().add((label1));
    ((Group) scene.getRoot()).getChildren().add(hbox);

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

當(dāng)用戶將鼠標(biāo)光標(biāo)懸停在標(biāo)簽上時(shí),可以縮放標(biāo)簽。當(dāng)在標(biāo)簽上觸發(fā)MOUSE_ENTERED事件時(shí),以下代碼將縮放效果應(yīng)用于標(biāo)簽。

以下代碼顯示如何縮放標(biāo)簽。

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

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

  @Override
  public void start(Stage stage) {
    Scene scene = new Scene(new Group());
    stage.setTitle("Label Sample");
    stage.setWidth(400);
    stage.setHeight(180);

    HBox hbox = new HBox();

    final Label label1 = new Label("Search long long long long long long long long long ");
    label1.setOnMouseEntered(new EventHandler<MouseEvent>() {
      @Override
      public void handle(MouseEvent e) {
        label1.setScaleX(1.5);
        label1.setScaleY(1.5);
      }
    });

    label1.setOnMouseExited(new EventHandler<MouseEvent>() {
      @Override
      public void handle(MouseEvent e) {
        label1.setScaleX(1);
        label1.setScaleY(1);
      }
    });

    hbox.setSpacing(10);
    hbox.getChildren().add((label1));
    ((Group) scene.getRoot()).getChildren().add(hbox);

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

上面的代碼生成以下結(jié)果。

標(biāo)簽鼠標(biāo)事件

以下代碼顯示了如何為標(biāo)簽添加鼠標(biāo)進(jìn)出事件處理程序。

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
// @  W w W . yI Ib  AI.C O m 
public class Main extends Application {
  public static void main(String[] args) {
    launch(args);
  }

  @Override
  public void start(Stage stage) {
    Scene scene = new Scene(new Group());
    stage.setTitle("Label Sample");
    stage.setWidth(400);
    stage.setHeight(180);

    HBox hbox = new HBox();

    final Label label1 = new Label("Search long long long long long long long long long ");
    label1.setOnMouseEntered(new EventHandler<MouseEvent>() {
      @Override
      public void handle(MouseEvent e) {
        label1.setScaleX(1.5);
        label1.setScaleY(1.5);
      }
    });

    label1.setOnMouseExited(new EventHandler<MouseEvent>() {
      @Override
      public void handle(MouseEvent e) {
        label1.setScaleX(1);
        label1.setScaleY(1);
      }
    });

    hbox.setSpacing(10);
    hbox.getChildren().add((label1));
    ((Group) scene.getRoot()).getChildren().add(hbox);

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

上面的代碼生成以下結(jié)果。

更新標(biāo)簽

以下代碼顯示了如何在Button單擊事件中更改Label文本。

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.stage.Stage;

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

    @Override
    public void start(Stage primaryStage) {

        Button btn = new Button();
        final Label lbl = new Label();

        primaryStage.setTitle("Hello World!");

        lbl.setLayoutX(70);
        lbl.setLayoutY(150);

        btn.setLayoutX(100);
        btn.setLayoutY(100);
        btn.setText("Hello, World!");

        btn.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                lbl.setText("'Hello, World'文本被點(diǎn)擊了。");
            }
        });

        Group root = new Group();

        root.getChildren().add(btn);
        root.getChildren().add(lbl);
        primaryStage.setScene(new Scene(root, 300, 250));
        primaryStage.show();
    }
}

上面的代碼生成以下結(jié)果。