如何在JavaFX中将图像显示为工具提示?

我想将图像显示为工具提示。 它工作正常,但在一些随机点它显示波动。 我希望在没有波动的情况下正常显示它。

在此处输入图像描述

我在鼠标输入事件中显示一个新场景(我在其中添加了带图像的图像视图)并在鼠标离开事件事件中关闭它

// 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; } 

/****************************** function ****************** ************* /

 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) { } } 

在此处输入图像描述

如果您只是想在Node使用工具提示(在您的情况下为Button ),则使用Tooltip及其graphicProperty而不是显示不同的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);