如何在JavaFX中将图像显示为工具提示? [英] How to show Image as tooltip in JavaFX?
问题描述
我想将图片显示为工具提示。它工作正常,但在一些随机点它显示波动。我希望在没有波动的情况下正常显示。
I would like to show an image as a tooltip. It works OK but at some random points it shows fluctuation. I want to show it normally without getting fluctuate.
我在鼠标输入事件中显示一个新场景(我在其中添加了带图像的图像视图)并在鼠标离开事件事件中关闭它
I show a new scene (in which i added my image-view with image) on mouse enter event and close it on mouse leave event event
// MOUSE ENTER PHOTO CORRECTIO
@FXML
private void mouseEnterPhotoCorrection(MouseEvent event) {
if (f_ShowToolTip) {
Stage stg = funShowImageTooltip();
double x, y;
x = event.getScreenX();
y = event.getScreenY();
stg.setX(x);
stg.setY(y);
stg.show();
f_ShowToolTip = false;
}
}
// MOUSE LEAVE PHOTO CORRECTIO
@FXML
private void mouseLeavePhotoCorrection(MouseEvent event) {
funHideImageTooltip();
f_ShowToolTip = true;
}
/ *************** ***************职能******************************* /
/****************************** FUNCTIONS *******************************/
Stage s;
boolean f_ShowToolTip;
// FUNCTION TO SET INITAL STATE OF PHOTOS AND CORRECTION
private void funInitPhotosCorrection()
{
f_ShowToolTip = true;
}
private Stage funShowImageTooltip()
{
try {
s = new Stage();
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("frmImageToolTip.fxml"));
Parent root = (Parent) fxmlLoader.load();
Scene scene = new Scene(root);
s.setScene(scene);
s.setResizable(false);
s.initModality(Modality.WINDOW_MODAL);
s.initStyle(StageStyle.UNDECORATED);
s.setResizable(false);
double x, y;
//x = btn_Red.
s.show();
}catch(Exception e1)
{
}
return s;
}
private void funHideImageTooltip()
{
try {
s.close();
} catch (Exception e) {
}
}
推荐答案
如果您只是想要一个工具提示通过节点
(在您的情况下为按钮
),使用 工具提示
及其 graphicProperty
而不是显示不同的 Stage
。
If you just simply want to have a tooltip over a Node
(a Button
in your case), it is reasonable to use a Tooltip
and its graphicProperty
rather than showing a different Stage
.
// Load the image with the needed size
Image image = new Image("...", 150, 150, true, true);
// Place it over a Button
Button button = new Button();
Tooltip tooltip = new Tooltip();
tooltip.setGraphic(new ImageView(image));
button.setTooltip(tooltip);
这篇关于如何在JavaFX中将图像显示为工具提示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!