如何使用 JavaFX 建立工具提示?
工具提示是一個 UI 元素,使用該元素可以提供有關 GUI 應用程式中元素的其他資訊。
每當您將滑鼠指標懸停在應用程式中的元素(例如按鈕、標籤等)上時,工具提示都會顯示一條有關該元素的提示。
在 JavaFX 中,工具提示由 javafx.scene.control.Tooltip 類表示,您可以透過例項化它來建立一個工具提示。
建立工具提示
此類別的文字屬性儲存了當前工具提示要顯示的文字或提示。您可以使用 setText() 方法將值設定到此屬性。或者,您只需將要顯示的文字(字串)作為引數傳遞給 toolTip 類別的建構函式。
建立工具提示後,可以使用 install() 方法將它安裝在任何節點上。(引數-節點和工具提示)。
您還可以在 javafx.scene.control.Control 類別(所有使用者介面控制器的父類別)的 setTooltip() 方法中設定一個工具提示。
示例
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class TooltipExample extends Application {
public void start(Stage stage) {
//Creating labels
Label label1 = new Label("User Name: ");
Label label2 = new Label("Password: ");
//Creating text and password fields
TextField textField = new TextField();
PasswordField pwdField = new PasswordField();
//Creating tool tip for text field
Tooltip toolTipTxt = new Tooltip("Enter your user name");
//Setting the tool tip to the text field
Tooltip.install(textField, toolTipTxt);
//Creating tool tip for password field
Tooltip toolTipPwd = new Tooltip("Enter your password");
//Setting the tool tip to the text field
Tooltip.install(pwdField, toolTipPwd);
//Adding labels for nodes
HBox box = new HBox(5);
box.setPadding(new Insets(25, 5 , 5, 50));
box.getChildren().addAll(label1, textField, label2, pwdField);
//Setting the stage
Scene scene = new Scene(box, 595, 150, Color.BEIGE);
stage.setTitle("Tooltip Example");
stage.setScene(scene);
stage.show();
}
public static void main(String args[]){
launch(args);
}
}輸出

廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP