JavaFX API的javafx.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();
}
}
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));
創(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
。
如果未設(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é)果。
我們可以對(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)進(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é)果。
以下代碼顯示了如何在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é)果。