📜  javafx UI标签

📅  最后修改于: 2020-10-14 06:01:26             🧑  作者: Mango

JavaFX标签

javafx.scene.control.Label类表示标签控件。顾名思义,标签是用于在屏幕上放置任何文本信息的组件。它主要用于向用户描述其他组件的用途。您不能使用Tab键将焦点放在标签上。

封装:javafx.scene.control

构造函数:

Label(): creates an empty Label 
Label(String text): creates Label with the supplied text 
Label(String text, Node graphics): creates Label with the supplied text and graphics

将节点添加到场景图

以下代码将Label实现到我们的应用程序中。

package application;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class LabelTest extends Application {

@Override
public void start(Stage primaryStage) throws Exception {
// TODO Auto-generated method stub
Label my_label=new Label("This is an example of Label");
StackPane root = new StackPane(); 
Scene scene=new Scene(root,300,300);
root.getChildren().add(my_label);
primaryStage.setScene(scene);
primaryStage.setTitle("Label Class Example");
primaryStage.show();

}
public static void main(String[] args) {
launch(args);
}
}

输出:

在标签中显示图像

JavaFX允许我们在标签文本旁边显示一些图形。 Label类中有一个构造函数,我们可以在其中传递任何图像以及标签文本。下面给出的示例是在标签中显示图像。

package application;

import java.io.FileInputStream;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class LabelTest extends Application {

@Override
public void start(Stage primaryStage) throws Exception {
// TODO Auto-generated method stub

StackPane root = new StackPane(); 
FileInputStream input= new FileInputStream("/home/javatpoint/Desktop/JavaFX/Images/colored_label.png");
Image image = new Image(input);
ImageView imageview=new ImageView(image);
Label my_label=new Label("Home",imageview);
Scene scene=new Scene(root,300,300);
root.getChildren().add(my_label);
primaryStage.setScene(scene);
primaryStage.setTitle("Label Class Example");
primaryStage.show();

}
public static void main(String[] args) {
launch(args);
}
}

输出: