📜  JavaFX |标签

📅  最后修改于: 2022-05-13 01:54:56.571000             🧑  作者: Mango

JavaFX |标签

标签是 JavaFX 包的一部分。标签用于显示短文本或图像,是不可编辑的文本控件。它对于显示需要适合特定空间的文本很有用,因此可能需要使用省略号或截断来调整字符串的大小以适合。标签也很有用,因为它们可以具有助记符,如果使用这些助记符,会将焦点发送到列为 labelFor 属性目标的控件。标签只能显示文本或图像,不能获得焦点。
Label 类的构造函数是

  1. Label() : 创建一个空标签
  2. Label(String t) :使用给定文本创建标签。
  3. Label(String t, Node g) :使用给定的文本和图形创建一个标签。

常用方法

methodexplanation
createDefaultSkin()Create a new instance of the default skin for this control.
getLabelFor()Gets the value of the property labelFor.
labelForProperty()A Label can act as a label for a different Control or Node.
setLabelFor(Node v)Sets the value of the property labelFor.

以下程序说明了标签的使用:

  1. 程序 1 :该程序创建一个由名称b指示的标签。进度指示器将在场景中创建,而场景又将托管在舞台(这是顶级 JavaFX 容器)中。函数setTitle() 用于为舞台提供标题。然后创建一个堆栈窗格,调用 addChildren() 方法在场景中附加标签,以及代码中 (200, 200) 指定的分辨率。最后调用 show() 方法显示最终结果。
Java
// Java program to create a label
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Label;
import javafx.stage.Stage;
public class label extends Application {
 
    // launch the application
    public void start(Stage s)
    {
        // set title for the stage
        s.setTitle("creating label");
 
        // create a label
        Label b = new Label("This is a label");
 
        // create a Stack pane
        StackPane r = new StackPane();
 
        // add password field
        r.getChildren().add(b);
 
        // create a scene
        Scene sc = new Scene(r, 200, 200);
 
        // set the scene
        s.setScene(sc);
 
        s.show();
    }
 
    public static void main(String args[])
    {
        // launch the application
        launch(args);
    }
}


Java
// Java program to create a label with images
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Label;
import javafx.stage.Stage;
import javafx.scene.image.*;
import java.io.*;
public class label_1 extends Application {
 
    // launch the application
    public void start(Stage s) throws Exception
    {
        // set title for the stage
        s.setTitle("creating label");
 
        // create a input stream
        FileInputStream input = new FileInputStream("f:\\gfg.png");
 
        // create a image
        Image i = new Image(input);
 
        // create a image View
        ImageView iw = new ImageView(i);
 
        // create a label
        Label b = new Label("", iw);
 
        // create a Stack pane
        StackPane r = new StackPane();
 
        // add password field
        r.getChildren().add(b);
 
        // create a scene
        Scene sc = new Scene(r, 200, 200);
 
        // set the scene
        s.setScene(sc);
 
        s.show();
    }
 
    public static void main(String args[])
    {
        // launch the application
        launch(args);
    }
}


Java
// Java program to create a label with images and text
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Label;
import javafx.stage.Stage;
import javafx.scene.image.*;
import java.io.*;
public class label_2 extends Application {
 
    // launch the application
    public void start(Stage s) throws Exception
    {
        // set title for the stage
        s.setTitle("creating label");
 
        // create a input stream
        FileInputStream input = new FileInputStream("f:\\gfg.png");
 
        // create a image
        Image i = new Image(input);
 
        // create a image View
        ImageView iw = new ImageView(i);
 
        // create a label
        Label b = new Label("Label Text", iw);
 
        // create a Stack pane
        StackPane r = new StackPane();
 
        // add password field
        r.getChildren().add(b);
 
        // create a scene
        Scene sc = new Scene(r, 200, 200);
 
        // set the scene
        s.setScene(sc);
 
        s.show();
    }
 
    public static void main(String args[])
    {
        // launch the application
        launch(args);
    }
}


Java
// Java program to create a labels and textfield and use setLabelFor property
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Label;
import javafx.stage.Stage;
import javafx.scene.image.*;
import java.io.*;
public class label_3 extends Application {
 
    // launch the application
    public void start(Stage s) throws Exception
    {
        // set title for the stage
        s.setTitle("creating label");
 
        // TextField
        TextField b1 = new TextField("textfield");
 
        // create a label
        Label b = new Label("_1 TextField");
 
        // setlabel for
        b.setLabelFor(b1);
 
        // TextField
        TextField b4 = new TextField("textfield");
 
        // create a label
        Label b3 = new Label("_2 TextField");
 
        // setlabel for
        b3.setLabelFor(b4);
 
        // create a Tile pane
        TilePane r = new TilePane();
 
        // setMnemonic
        b.setMnemonicParsing(true);
        b3.setMnemonicParsing(true);
 
        // add password field
        r.getChildren().add(b);
        r.getChildren().add(b1);
        r.getChildren().add(b3);
        r.getChildren().add(b4);
 
        // create a scene
        Scene sc = new Scene(r, 200, 200);
 
        // set the scene
        s.setScene(sc);
 
        s.show();
    }
 
    public static void main(String args[])
    {
        // launch the application
        launch(args);
    }
}


  1. 输出

  1. 用图像创建标签的Java程序:该程序用名称b表示的图像创建一个标签,图像命名为i ,imageview 用名称iw表示。标签将在场景中创建,而场景又将托管在舞台中(这是顶级 JavaFX 容器)。函数setTitle() 用于为舞台提供标题。然后创建一个堆栈窗格,在其上调用 addChildren() 方法以将标签与场景内的图像以及代码中 (200, 200) 指定的分辨率一起附加。最后调用 show() 方法显示最终结果。

Java

// Java program to create a label with images
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Label;
import javafx.stage.Stage;
import javafx.scene.image.*;
import java.io.*;
public class label_1 extends Application {
 
    // launch the application
    public void start(Stage s) throws Exception
    {
        // set title for the stage
        s.setTitle("creating label");
 
        // create a input stream
        FileInputStream input = new FileInputStream("f:\\gfg.png");
 
        // create a image
        Image i = new Image(input);
 
        // create a image View
        ImageView iw = new ImageView(i);
 
        // create a label
        Label b = new Label("", iw);
 
        // create a Stack pane
        StackPane r = new StackPane();
 
        // add password field
        r.getChildren().add(b);
 
        // create a scene
        Scene sc = new Scene(r, 200, 200);
 
        // set the scene
        s.setScene(sc);
 
        s.show();
    }
 
    public static void main(String args[])
    {
        // launch the application
        launch(args);
    }
}
  1. 输出

  1. 创建带有图像和文本的标签的Java程序:该程序创建带有名称b指示的图像和文本的标签,图像命名为i ,图像视图由名称iw指示。要在标签上显示的文本被传递一个参数给标签的构造函数。标签将在场景中创建,而场景又将托管在舞台中(这是顶级 JavaFX 容器)。函数setTitle() 用于为舞台提供标题。然后创建一个堆栈窗格,在其上调用 addChildren() 方法以将标签与场景内的图像以及代码中 (200, 200) 指定的分辨率一起附加。最后调用 show() 方法显示最终结果。

Java

// Java program to create a label with images and text
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Label;
import javafx.stage.Stage;
import javafx.scene.image.*;
import java.io.*;
public class label_2 extends Application {
 
    // launch the application
    public void start(Stage s) throws Exception
    {
        // set title for the stage
        s.setTitle("creating label");
 
        // create a input stream
        FileInputStream input = new FileInputStream("f:\\gfg.png");
 
        // create a image
        Image i = new Image(input);
 
        // create a image View
        ImageView iw = new ImageView(i);
 
        // create a label
        Label b = new Label("Label Text", iw);
 
        // create a Stack pane
        StackPane r = new StackPane();
 
        // add password field
        r.getChildren().add(b);
 
        // create a scene
        Scene sc = new Scene(r, 200, 200);
 
        // set the scene
        s.setScene(sc);
 
        s.show();
    }
 
    public static void main(String args[])
    {
        // launch the application
        launch(args);
    }
}
  1. 输出

  1. 用于创建标签和文本字段并使用 setLabelFor 属性的Java程序:在此程序中,为助记符解析设置标签(如果按下 Alt+1,则焦点转移到第一个文本字段,如果按下 Alt + 2,则焦点转移到第二个文本字段。

Java

// Java program to create a labels and textfield and use setLabelFor property
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Label;
import javafx.stage.Stage;
import javafx.scene.image.*;
import java.io.*;
public class label_3 extends Application {
 
    // launch the application
    public void start(Stage s) throws Exception
    {
        // set title for the stage
        s.setTitle("creating label");
 
        // TextField
        TextField b1 = new TextField("textfield");
 
        // create a label
        Label b = new Label("_1 TextField");
 
        // setlabel for
        b.setLabelFor(b1);
 
        // TextField
        TextField b4 = new TextField("textfield");
 
        // create a label
        Label b3 = new Label("_2 TextField");
 
        // setlabel for
        b3.setLabelFor(b4);
 
        // create a Tile pane
        TilePane r = new TilePane();
 
        // setMnemonic
        b.setMnemonicParsing(true);
        b3.setMnemonicParsing(true);
 
        // add password field
        r.getChildren().add(b);
        r.getChildren().add(b1);
        r.getChildren().add(b3);
        r.getChildren().add(b4);
 
        // create a scene
        Scene sc = new Scene(r, 200, 200);
 
        // set the scene
        s.setScene(sc);
 
        s.show();
    }
 
    public static void main(String args[])
    {
        // launch the application
        launch(args);
    }
}
  1. 输出

注意:以下程序可能无法在在线 IDE 中运行,请使用离线编译器。您应该拥有最新版本的Java才能运行这些程序。
参考:https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/Label.html