JavaFX-如何通过鼠标点击获取信息

JavaFX-如何通过鼠标点击获取信息,javafx,onclick,imageview,fxml,gridpane,Javafx,Onclick,Imageview,Fxml,Gridpane,我希望这是我最后一次发表这篇文章。。我已经尝试了所有的方法,但是我在GridPane上的coord返回总是null而不是整数 这不是这样的线程,这是一个特定的问题,所以请不要关闭或执行其他操作。我必须解决这个问题 我的实际情况: 这是我的密码。我的问题是。。为什么当我点击myGrid(GridPane)时,它总是返回“null”?谢谢 这里是我在FXML中的控制器: 是否将某种类型的节点添加到网格窗格?在此示例应用程序中,StackPanes被添加到每个网格中。在所有StackPanes上注册

我希望这是我最后一次发表这篇文章。。我已经尝试了所有的方法,但是我在GridPane上的coord返回总是null而不是整数

这不是这样的线程,这是一个特定的问题,所以请不要关闭或执行其他操作。我必须解决这个问题

我的实际情况: 这是我的密码。我的问题是。。为什么当我点击myGrid(GridPane)时,它总是返回“null”?谢谢

这里是我在FXML中的控制器:


是否将某种类型的
节点
添加到
网格窗格
?在此示例应用程序中,
StackPane
s被添加到每个
网格中。在所有
StackPane
s上注册相同的事件处理程序

主要

控制器

FXML



请取出图像并张贴相关方法的代码片段。图像可能会在一段时间后消失,难以阅读。将代码作为文本而不是图像发布。图像中没有代码无法提供的信息。。。此外,还发布了包含控制器字段的部分以及fxml文件的相关部分(作为代码)…解释这与。事件处理程序现在是否已注册到其他节点,还是仍在网格窗格中注册?它工作正常!我看到了你的FXML文件,我明白了!谢谢,伙计
package javafxapplication147;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

/**
 *
 * @author blj0011
 */
public class JavaFXApplication147 extends Application
{

    @Override
    public void start(Stage stage) throws Exception
    {
        Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));

        Scene scene = new Scene(root);

        stage.setScene(scene);
        stage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        launch(args);
    }

}
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.GridPane;

/**
 *
 * @author blj0011
 */
public class FXMLDocumentController implements Initializable
{

    @FXML
    private Label lblMain;

    @FXML
    private void handleOnMouseClicked(MouseEvent event)
    {
        Node source = (Node)event.getSource();
        Integer colIndex = (GridPane.getColumnIndex(source) == null) ?  0 : (GridPane.getColumnIndex(source));
        Integer colRow = (GridPane.getRowIndex(source) == null) ? 0 : (GridPane.getRowIndex(source));
        lblMain.setText(colIndex + " : " + colRow);
    }

    @Override
    public void initialize(URL url, ResourceBundle rb)
    {
        // TODO
    }    

}
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.layout.VBox?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafxapplication147.FXMLDocumentController">
   <children>
      <VBox layoutX="48.0" layoutY="14.0" prefHeight="200.0" prefWidth="100.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="50.0" AnchorPane.rightAnchor="50.0" AnchorPane.topAnchor="0.0">
         <children>
            <GridPane gridLinesVisible="true" style="-fx-background-color: green;">
              <columnConstraints>
                <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
              </columnConstraints>
              <rowConstraints>
                  <RowConstraints minHeight="100.0" prefHeight="100.0" vgrow="SOMETIMES" />
                <RowConstraints minHeight="100.0" prefHeight="100.0" vgrow="SOMETIMES" />
                <RowConstraints minHeight="100.0" prefHeight="100.0" vgrow="SOMETIMES" />
                <RowConstraints minHeight="100.0" prefHeight="100.0" vgrow="SOMETIMES" />
                  <RowConstraints minHeight="100.0" prefHeight="100.0" vgrow="SOMETIMES" />
              </rowConstraints>
               <VBox.margin>
                  <Insets top="10.0" />
               </VBox.margin>
               <children>
                  <StackPane onMouseClicked="#handleOnMouseClicked" prefHeight="150.0" prefWidth="200.0" />
                  <StackPane onMouseClicked="#handleOnMouseClicked" prefHeight="150.0" prefWidth="200.0" GridPane.columnIndex="1" />
                  <StackPane onMouseClicked="#handleOnMouseClicked" prefHeight="150.0" prefWidth="200.0" GridPane.columnIndex="2" />
                  <StackPane onMouseClicked="#handleOnMouseClicked" prefHeight="150.0" prefWidth="200.0" GridPane.columnIndex="3" />
                  <StackPane onMouseClicked="#handleOnMouseClicked" prefHeight="150.0" prefWidth="200.0" GridPane.rowIndex="1" />
                  <StackPane onMouseClicked="#handleOnMouseClicked" prefHeight="150.0" prefWidth="200.0" GridPane.columnIndex="1" GridPane.rowIndex="1" />
                  <StackPane onMouseClicked="#handleOnMouseClicked" prefHeight="150.0" prefWidth="200.0" GridPane.columnIndex="2" GridPane.rowIndex="1" />
                  <StackPane onMouseClicked="#handleOnMouseClicked" prefHeight="150.0" prefWidth="200.0" GridPane.columnIndex="3" GridPane.rowIndex="1" />
                  <StackPane onMouseClicked="#handleOnMouseClicked" prefHeight="150.0" prefWidth="200.0" GridPane.rowIndex="2" />
                  <StackPane onMouseClicked="#handleOnMouseClicked" prefHeight="150.0" prefWidth="200.0" GridPane.columnIndex="1" GridPane.rowIndex="2" />
                  <StackPane onMouseClicked="#handleOnMouseClicked" prefHeight="150.0" prefWidth="200.0" GridPane.columnIndex="2" GridPane.rowIndex="2" />
                  <StackPane onMouseClicked="#handleOnMouseClicked" prefHeight="150.0" prefWidth="200.0" GridPane.columnIndex="3" GridPane.rowIndex="2" />
                  <StackPane onMouseClicked="#handleOnMouseClicked" prefHeight="150.0" prefWidth="200.0" GridPane.rowIndex="3" />
                  <StackPane onMouseClicked="#handleOnMouseClicked" prefHeight="150.0" prefWidth="200.0" GridPane.columnIndex="1" GridPane.rowIndex="3" />
                  <StackPane onMouseClicked="#handleOnMouseClicked" prefHeight="150.0" prefWidth="200.0" GridPane.columnIndex="2" GridPane.rowIndex="3" />
                  <StackPane onMouseClicked="#handleOnMouseClicked" prefHeight="150.0" prefWidth="200.0" GridPane.columnIndex="3" GridPane.rowIndex="3" />
                  <StackPane onMouseClicked="#handleOnMouseClicked" prefHeight="150.0" prefWidth="200.0" GridPane.rowIndex="4" />
                  <StackPane onMouseClicked="#handleOnMouseClicked" prefHeight="150.0" prefWidth="200.0" GridPane.columnIndex="1" GridPane.rowIndex="4" />
                  <StackPane onMouseClicked="#handleOnMouseClicked" prefHeight="150.0" prefWidth="200.0" GridPane.columnIndex="2" GridPane.rowIndex="4" />
                  <StackPane onMouseClicked="#handleOnMouseClicked" prefHeight="150.0" prefWidth="200.0" GridPane.columnIndex="3" GridPane.rowIndex="4" />
               </children>
            </GridPane>
            <StackPane prefHeight="150.0" prefWidth="200.0">
               <children>
                  <Label fx:id="lblMain" text="Label" />
               </children>
            </StackPane>
         </children>
      </VBox>
   </children>
</AnchorPane>