在 Borland VCL 库中,几乎所有控件都有一个提示属性。在运行期间,当您将鼠标放在相应控件上时,当您移动鼠标时,一个带有提示文本的小框会弹出并再次消失,就像 Windows 资源管理器和其他程序中的帮助消息一样,当鼠标光标悬停在按钮上时.
JavaFX中是否有类似的概念(实际上,我正在使用ScalaFX)?
当然,我可以创建一个没有装饰的新舞台,添加一些鼠标侦听器等,但它不是已经在某个地方可用了吗?
您可以使用Tooltip控件。
使用示例
如果您想要控件上的工具提示,例如按钮,请设置工具提示:
button.setTooltip(
new Tooltip("Button of doom")
);
否则,对于形状等其他节点类型,请安装工具提示:
Circle circle = new Circle(15, 15, 42);
Tooltip.install(
circle,
new Tooltip("Circle of light")
);
教程
Oracle 有专门针对工具提示的教程。
正如您在上面看到的,您可以在工具提示上设置“图形”,它可以是图像(或任何其他节点),它非常灵活。
工具提示样式
其他选项
如果 Tooltip 不是您要查找的内容,还有其他显示弹出窗口的方法:
此代码创建一个基于图形的工具提示。看一下注释的 htmlStr .....您可以使用它以及 thisToolTip.setStyle ..... 看看会发生什么。您可以更改 htmlStr 中的样式和 setStyle 的字符串。但是,我无法使工具提示和窗格的大小匹配。所以有一个边框,但我将两种背景颜色的颜色都设置为玉米丝。它给人一种没有边界的错觉。但事实并非如此。查看代码,如果您觉得有用,请使用它。
private Tooltip createToolTip(String htmlStr) {
Tooltip thisToolTip = new Tooltip();
// String htmlStr = "<body style=\"background-color:cornsilk; "
// + "border-style: none;\"> <u><b><font color=\"red\">Click Mouse's right button to see options</font></b></u><br><br>(3) Subha Jawahar of Chennai<br> now @ Chennai<br>Female <-> Married <-> Alive<br>Period : 1800 to 2099<br>D/o Dr. Subbiah [2] - <br> <b>Spouse :</b> Jawahar Rajamanickam [7] <br><br><b>Children :</b><br><br>Rudhra Jawahar [9]<br>Mithran Jawahar [10]<br><br></body>\n";
WebView browser = new WebView();
WebEngine webEngine = browser.getEngine();
webEngine.loadContent(htmlStr);
thisToolTip.setStyle("\n"
+ " -fx-border-color: black;\n"
+ " -fx-border-width: 1px;\n"
+ " -fx-font: normal bold 12pt \"Times New Roman\" ;\n"
+ " -fx-background-color: cornsilk;\n"
+ " -fx-text-fill: black;\n"
+ " -fx-background-radius: 4;\n"
+ " -fx-border-radius: 4;\n"
+ " -fx-opacity: 1.0;");
thisToolTip.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
thisToolTip.setGraphic(browser);
thisToolTip.setAutoHide(false);
thisToolTip.setMaxWidth(300);
thisToolTip.setGraphicTextGap(0.0);
return thisToolTip;
}