本文介绍了JavaFX在鼠标悬停时显示一个窗格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个JavaFX
应用程序,我想在Button
的mousehover
事件上显示pane
,
I have a JavaFX
application where i want to show a pane
on mousehover
event on a Button
,
我期望的输出类似于Windows任务栏预览样式,其中将鼠标悬停在 TaskBar 图标上时,预览窗格显示在顶部. (如下所示)
The output i am expecting is similar to windows taskbar preview style, where on hovering the TaskBar icons a preview pane is shown on top. (like below)
如何使用JavaFX达到这种效果.
how can i achieve this effect using JavaFX.
推荐答案
代码虽然很原始,但是却得到了您所要求的核心功能(当然,可以通过多种方式实现,取决于需求,我只是发布了对我来说实施最快的解决方案)
Code is very raw but gets the core feature you asked for (of course it can be achived in more than one way, depening on needs, I've just published the solution which was the fastest to implement for me)
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.layout.Region;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
public class StickyNotesApp extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
StackPane notedPane = new StackPane();
notedPane.setPrefSize(20, 20);
notedPane.setMaxSize(Region.USE_PREF_SIZE, Region.USE_PREF_SIZE);
notedPane.setStyle("-fx-background-color: purple;");
StackPane rootPane = new StackPane(notedPane);
rootPane.setPrefSize(400, 400);
StackPane.setAlignment(notedPane, Pos.BOTTOM_CENTER);
stage.setScene(new Scene(rootPane));
stage.show();
Stage stickyNotesStage = new Stage();
stickyNotesStage.initOwner(stage);
stickyNotesStage.initStyle(StageStyle.UNDECORATED);
StackPane stickyNotesPane = new StackPane();
stickyNotesPane.setPrefSize(200, 200);
stickyNotesPane.setStyle("-fx-background-color: yellow;");
stickyNotesStage.setScene(new Scene(stickyNotesPane));
notedPane.hoverProperty().addListener((ChangeListener<Boolean>) (observable, oldValue, newValue) -> {
if (newValue) {
stickyNotesStage.show();
} else {
stickyNotesStage.hide();
}
});
}
}
这篇关于JavaFX在鼠标悬停时显示一个窗格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!