Javafx 简明教程
JavaFX - Tooltip
tooltip 是一个小弹出窗口,当用户将鼠标悬停在节点或控件上时,它会显示一些附加信息。它主要用于解释按钮、菜单或图像的功能,或为文本字段提供一些提示。在下图中,我们可以看到说明菜单功能的工具提示 −
The tooltip is a small pop-up window that displays some additional information when the user hovers over a node or a control. It is mainly used for explaining the functionality of a button, menu, or image or to provide some hints for a text field. In the below figure, we can see the tooltip explaining the menu’s functionality −
Tooltip in JavaFX
在 JavaFX 中,工具提示由名为 Tooltip 的类表示,它是 javafx.scene.control 包的一部分。此包的每个 UI 组件都带有名为 setTooltip() 的内置方法,用于关联工具提示。我们可以通过将工具提示文本传递给 setText() 方法或使用下面列出的构造函数来指定工具提示文本 −
In JavaFX, the tooltip is represented by a class named Tooltip which is a part of javafx.scene.control package. Each UI component of this package comes with a built-in method named setTooltip() which is used for associating a tooltip. We can specify the tooltip text either by passing it to the setText() method or by using the constructor listed below −
-
Tooltip() − It is the default constructor that constructs a tooltip without any texts.
-
Tooltip(String str) − It constructs a new tooltip with the predefined text.
Steps to create a tooltip in JavaFX
要在 JavaFX 中创建工具提示,请按照以下步骤操作。
To create a Tooltip in JavaFX, follow the steps given below.
Step 1: Create a node to associate with Tooltip
我们知道工具提示会解释指定节点的功能。此节点可以是任何 JavaFX 组件,如菜单、图像和文本字段。此处,我们将把图像作为节点来使用。要在 JavaFX 中创建图像,实例化 ImageView 类,然后将其构造函数的参数值设为图像的路径。
We know that a tooltip explains the features of specified node. This node can be any JavaFX component such as menu, image and text fields. Here, we are going to use an image as a node. To create an image in JavaFX, instantiate the ImageView class and pass the path of image as a parameter value to its constructor.
//Passing path of an image
Image image = new Image(new FileInputStream("tutorials_point.jpg"));
//Setting the image view
ImageView imageView = new ImageView(image);
Step 2: Instantiate the Tooltip class
要在 JavaFX 中创建工具提示,实例化 Tooltip 类,然后使用以下代码将其构造函数的参数值设为工具提示文本 −
To create a tooltip in JavaFX, instantiate the Tooltip class and pass the tooltip text as a parameter value to its constructor using the below code −
//Creating tool tip for the given image
Tooltip toolTipTxt = new Tooltip("This is the new logo of Tutorialspoint");
Step 3: Associate the tooltip to the node
为将工具提示与指定节点关联,我们使用 install() 方法,它接受 Tooltip 和 ImageView 对象作为参数。
To associate the Tooltip to the specified node, we use the install() mehtod which accepts Tooltip and ImageView objects as arguments.
//Setting the tool tip to the image
Tooltip.install(imageView, toolTipTxt);
Step 4: Launching Application
创建工具提示后,请按照以下给定步骤正确启动应用程序 −
After creating the Tooltip, follow the given steps below to launch the application properly −
-
Firstly, create a VBox that holds the nodes vertically.
-
Next, instantiate the class named Scene by passing the VBox object as a parameter value to its constructor along with the dimensions of the application screen.
.
-
Then, set the title of the stage using the setTitle() method of the Stage class.
-
Now, a Scene object is added to the stage using the setScene() method of the class named Stage.
-
Display the contents of the scene using the method named show().
-
Lastly, the application is launched with the help of the launch() method.
Example
在以下示例中,我们将在 JavaFX 应用程序中创建一个工具提示。将此代码另存为名称为 JavafxTooltip.java 的文件。
In the following example, we are going to create a tooltip in JavaFX application. Save this code in a file with the name JavafxTooltip.java.
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.VBox;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import javafx.stage.Stage;
public class JavafxTooltip extends Application {
@Override
public void start(Stage stage) throws FileNotFoundException {
//Creating a label
Label labeltext = new Label("Hover over Image to see the details....");
//Passing path of an image
Image image = new Image(new FileInputStream("tutorials_point.jpg"));
//Setting the image view
ImageView imageView = new ImageView(image);
//Setting the position of the image
imageView.setX(50);
imageView.setY(25);
//setting the fit height and width of the image view
imageView.setFitHeight(350);
imageView.setFitWidth(350);
//Setting the preserve ratio of the image view
imageView.setPreserveRatio(true);
//Creating tool tip for the given image
Tooltip toolTipTxt = new Tooltip("This is the new logo of Tutorialspoint");
//Setting the tool tip to the image
Tooltip.install(imageView, toolTipTxt);
// to display the content vertically
VBox box = new VBox(5);
box.setPadding(new Insets(25, 5 , 5, 50));
box.getChildren().addAll(labeltext, imageView);
//Setting the stage
Scene scene = new Scene(box, 400, 350);
stage.setTitle("Example of Tooltip in JavaFX");
stage.setScene(scene);
stage.show();
}
public static void main(String args[]){
launch(args);
}
}
要从命令提示符编译并执行已保存的 Java 文件,请使用以下命令−
To compile and execute the saved Java file from the command prompt, use the following commands −
javac --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxTooltip.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxTooltip
执行以上代码后,它将生成一个图像工具提示,如下图所示的输出。
When we execute the above code, it will generate a Tooltip for an image as shown in the following output.
Adding icons to the Tooltip
图标是小图像,可直观地说明应用程序的命令或功能。要向 JavaFX 工具提示添加图标,我们使用 setGraphic() 方法,它接受 ImageView 对象并在工具提示文本的左侧显示图标。
Icons are small images that graphically illustrate a command or function of an application. To add icons to the JavaFX tooltip, we use the setGraphic() method which accepts the ImageView object and displays the icon on the left of tooltip text.
Example
以下是将创建一个带图标工具提示的 JavaFX 程序。将此代码另存为名称为 JavafxTooltip.java 的文件。
Following is the JavaFX program that will create a Tooltip with icon. Save this code in a file with the name JavafxTooltip.java.
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.control.Tooltip;
import javafx.scene.layout.VBox;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import javafx.stage.Stage;
public class JavafxTooltip extends Application {
public void start(Stage stage) throws FileNotFoundException {
// Creating a label
Label labeltext = new Label("Hover over this text to see the tooltip....");
// Instantiating Tooltip class
Tooltip toolT = new Tooltip();
// Passing path of an image
Image icon = new Image(new FileInputStream("faviconTP.png"));
// adding the icon for tooltip
toolT.setGraphic(new ImageView(icon));
// adding the text
toolT.setText("This is the new logo of Tutorialspoint");
// setting the tooltip to the label
labeltext.setTooltip(toolT);
//Setting the stage
Scene scene = new Scene(labeltext, 400, 300);
stage.setTitle("Example of Tooltip in JavaFX");
stage.setScene(scene);
stage.show();
}
public static void main(String args[]){
launch(args);
}
}
使用以下命令从命令提示符编译并执行已保存的 Java 文件 −
Compile and execute the saved Java file from the command prompt using the following commands −
javac --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxTooltip.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxTooltip
执行后,上述程序将生成以下输出 −
On executing, the above program will generate the following output −