您可以使用-将工具提示添加到选择框:
Install()方法-这是ToolTip类的方法,它接受Node和Tooltip对象作为参数。使用此工具,您可以在指定节点上安装工具提示。
setToolTip()方法-这是javafx.scene.control.Control类(所有用户界面控件的父类)的方法。您还可以使用setTooltip()方法将工具提示设置为控件元素。
以下JavaFX示例创建一个选择框并向其中添加工具提示-
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
public class ChoiceBoxAddingTooltip extends Application {
public void start(Stage stage) {
//Setting the label
Label label = new Label("Select Display Language:");
Font font = Font.font("verdana", FontWeight.BOLD, FontPosture.REGULAR, 12);
label.setFont(font);
//Creating a choice box
ChoiceBox choiceBox = new ChoiceBox();
choiceBox.setValue("English");
//Adding the choices to the observable list
choiceBox.getItems().addAll("English", "Hindi", "Telugu", "Tamil");
//Creating a tool tip
Tooltip toolTip = new Tooltip("Select a Language");
//Adding tool tip to the choice box
choiceBox.setTooltip(toolTip);
//Creating a HBox
HBox box = new HBox(5);
box.setPadding(new Insets(25, 5 , 5, 50));
box.getChildren().addAll(label, choiceBox );
//Setting the stage
Scene scene = new Scene(box, 595, 170, Color.BEIGE);
stage.setTitle("Choice Box Example");
stage.setScene(scene);
stage.show();
}
public static void main(String args[]){
launch(args);
}
}
输出结果