简体   繁体   English

在JavaFX中右键单击窗格或ImageView时如何显示上下文菜单

[英]How do i show a context menu when right click on a Pane or an ImageView in javaFX

I am trying to show a context menu i created, when i right click on a pane or an image view ((on Context Menu Requested)). 我尝试显示在右键单击窗格或图像视图时((在请求的上下文菜单上))创建的上下文菜单。 The problem is they don't seem to have a setContextMenue method, unlike labels and buttons...etc. 问题是它们似乎没有setContextMenue方法,与标签和按钮...等不同。 How can associate a context menu to a node that doesn't seem to accept context menus? 如何将上下文菜单与似乎不接受上下文菜单的节点相关联?

@FXML
Button button1, button2;

@FXML
Pane mainPane;

@FXML
ImageView image;
private void initContextMenu() {
    final ContextMenu contextMenu = new ContextMenu();
    final MenuItem item1 = new MenuItem("open a file");
    final MenuItem item2 = new MenuItem("quit");

    contextMenu.getItems().addAll(item1, item2);

    // not possible
    image.setContextMenu(contextMenu);
    // possible
    button1.setContextMenu(contextMenu)

You can do 你可以做

image.setOnContextMenuRequested(e -> 
    contextMenu.show(image, e.getScreenX(), e.getScreenY()));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM