Javafx - 确定相对于gridpane右键单击光标的位置 [英] Javafx - Determine position of cursor on right click relative to gridpane

查看:267
本文介绍了Javafx - 确定相对于gridpane右键单击光标的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个40x40的网格窗格,显示一张地图。目前,我有以下方法收听右键点击:

I have a 40x40 gridpane, displaying a map. Currently, i have the following methods listening for a right click:

    //detect right click + display menu, select if you want to place item 1 or item 2
    final ContextMenu cm = new ContextMenu();
    cm.addEventFilter(MouseEvent.MOUSE_RELEASED, new EventHandler<MouseEvent>() 
    {
        @Override
        public void handle(MouseEvent event) 
        {
            if (event.getButton() == MouseButton.SECONDARY) 
                event.consume();

        }
    });
    cm.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {
            System.out.println("Following item selected: "+
                    ((MenuItem)event.getTarget()).getText());
        }
    });
    //two placeable items
    MenuItem item1Place = new MenuItem("Item 1");
    MenuItem item2Place = new MenuItem("Item 2");
    cm.getItems().addAll(item1Place, item2Place);

    primaryStage.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() 
    {
        @Override
        public void handle(MouseEvent e)
        {
            if (e.getButton() == MouseButton.SECONDARY) 
            {
              cm.show(primaryStage, e.getScreenX(), e.getScreenY());
            } 
        }
    });

我想实施的内容如下:


如果在右键单击后选择了一个菜单项,我想保存
右键单击变量中的位置

[32,12]如果
右键单击格子窗格的32个块,并且12个区块向上。

If a menu item is selected after a right click, i'd like to save the position of the right click in a variable
e.g. [32,12] if right click was 32 blocks of the gridpane across, and 12 blocks up.


有关如何进行此操作的任何建议?

提前致谢


Any suggestions on how to go about doing this?
Thanks in advance

// first edit code entered below. I realise the indentation is very off however fixing it seemed to turn it back to plain text
placeItem1.setOnAction(evt -> System.out.println("Item 1 placed ["+col+", "+row+"]"));
                placeItem2.setOnAction(evt -> System.out.println(Item 2 placed["+col+", "+row+"]"));
                menu.show(cell, e.getScreenX(), e.getScreenY());
            });
            root.add(cell, x, y);


推荐答案

为网格中的每个单元格添加一个监听器,而不是向舞台添加一个鼠标监听器。用于上下文菜单处理的正确监听器是a contextMenuRequested 处理程序:

Add a listener to each cell in the grid, instead of adding one mouse listener to the stage. The proper listener to use for context menu handling is a contextMenuRequested handler:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.input.MouseButton;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

public class ClicksInGridPane extends Application {

    @Override
    public void start(Stage primaryStage) {
        GridPane root = new GridPane();

        final int NUM_ROWS = 40 ;
        final int NUM_COLS = 40 ;

        ContextMenu menu = new ContextMenu();
        MenuItem menuItem1 = new MenuItem("Item 1");
        MenuItem menuItem2 = new MenuItem("Item 2");
        menu.getItems().addAll(menuItem1, menuItem2);

        for (int x = 0 ; x < NUM_COLS ; x++) {
            for (int y = 0 ; y < NUM_ROWS ; y++) {
                Pane cell = new Pane();
                cell.setPrefSize(20, 20);
                // add style just to draw grid:
                cell.setStyle("-fx-background-color: black, white; -fx-background-insets: 0, 0 0 1 1;");

                // context menu listener:
                final int col = x ;
                final int row = y ;

                cell.setOnContextMenuRequested(e -> {
                    menuItem1.setOnAction(evt -> System.out.println("Item 1 selected in cell ["+col+", "+row+"]"));
                    menuItem2.setOnAction(evt -> System.out.println("Item 2 selected in cell ["+col+", "+row+"]"));
                    menu.show(cell, e.getScreenX(), e.getScreenY());
                });

                root.add(cell, x, y);
            }
        }

        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

这篇关于Javafx - 确定相对于gridpane右键单击光标的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆