当前位置: 首页 > 面试题库 >

Javafx-确定光标相对于网格窗格的位置

蓬英逸
2023-03-14
问题内容

我有一个40x40的网格,显示地图。当前,我有以下方法监听右键单击:

    //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块)。

关于如何执行此操作的任何建议?
提前致谢

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

问题答案:

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

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


 类似资料:
  • 我目前有一个GridPane作为布局的根,我有另一个GridPane里面包含2个标签。我试图让这些标签中的一个左对齐,另一个右对齐。 我尝试使用GridPane.halignment="右",但这对标签没有影响。 我的布局代码如下: 如何按照我想要的方式对齐这些标签?GridPane对我想做的事情来说是理想的吗?我对JavaFX相当缺乏经验,但对Android的布局管理器很熟悉。

  • 我无法在SceneBuilder(eclipse)中使用GridPane,当我尝试在项目中插入GridPane时,SceneBuilder会在没有提示任何消息的情况下退出。 这就是我在日志文件中找到的:

  • 我创建了一个显示图像的JavaFX项目。我在JavaFX中使用画布绘制此图像并将其显示在锚窗格上。我在图像顶部放了一个网格,基本上我需要做的是当用户单击网格中的特定框时,找出单击的位置,以便我可以将坐标存储在文本文件中。 我曾考虑在每个框中放置一个按钮,但这需要一段时间,是否有一种更简单的方法可以使用gridpane实现这一点?如何获得该点击的位置/坐标? 谢谢你!

  • 我需要相对于鼠标位置放大/缩小滚动窗格。 我目前通过将我的内容包装在一个组中并缩放组本身来实现缩放功能。我创建了一个带有自定义透视的新Scale对象。(枢轴设置为鼠标位置) 如何在不同级别的缩放中获得正确的鼠标位置?是否有一种完全不同的方法来缩放滚动窗格更容易?

  • 我在fxml中有一个GridPane,它有一个文本标题和4个按钮。GridPane本身是居中对齐的,但所有按钮都在网格的列内左对齐。我知道如何使用Java代码更改项目的对齐方式,但这不是理想的情况,因为我希望所有样式都使用FXML和CSS处理。有谁能建议在JavaFX的GridPane视图中居中对齐单元格元素的最佳方法吗?

  • 我试图将乘法VBox添加到scrollpane中的gridpane(在下面的codesnippet中称为refPane)。 它在一行中添加不超过ITEMS_PER_ROW的Vbox,并在下一行中继续。也不应该有更多的行,然后ITEMS_PER_COLUM可见。问题是,如果我添加更多的ITEMS_PER_ROW*ITEMS_PER_COLUMN到网格中,而不是obingbeingscrollable