javafx - JavaFX - 尝试获取 GridPane 列/行索引会给出 InvocationTargetException
问题描述
我在每一列上都有一个2×2 GridPane
,AnchorPane
我想知道用户使用 a 单击了网格的哪一列,MouseEvent
但我得到的每一列和indexe
每一行AnchorPane
都是:null
,完整的项目:
完整的 FXML 代码:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.141" fx:controller="sample.Controller" xmlns:fx="http://javafx.com/fxml/1">
<children>
<GridPane gridLinesVisible="true" onMouseClicked="#mouseclicked" prefHeight="400.0" prefWidth="600.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<AnchorPane prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="0" GridPane.rowIndex="0" />
<AnchorPane prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="1" GridPane.rowIndex="0"/>
<AnchorPane prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="0" GridPane.rowIndex="1" />
<AnchorPane prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="1" GridPane.rowIndex="1" />
</children>
</GridPane>
</children>
</AnchorPane>
FXML 控制器:
package sample;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.GridPane;
public class Controller {
@FXML
public void mouseclicked(MouseEvent ev){
Node source = (Node)ev.getSource() ;
Integer col = GridPane.getColumnIndex(source);
Integer row = GridPane.getRowIndex(source);
System.out.println("Column : "+col);
System.out.println("Row : "+row);
}
}
主要课程:
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
解决方案
我发现出了什么问题,MouseEvent
不应该通过单击GridPane
本身而是通过单击来触发Column
,所以发生的情况是它试图获取indexes
网格而不是列(即使用户单击了列)所以解决方案是网格不应该有任何类型的事件,但列有!
推荐阅读
- php - 如何在php中使用if else条件与Join
- performance - flask_sqlalchemy 用不同的数据更新多行(太慢了)
- haskell - 当 cabal 构建软件包时,“遗留后备”是什么意思?
- javascript - TypeError:无法设置未定义的属性“innerHTML”
- azure - Azure 函数未更新成功计数
- html - 如何使用媒体查询隐藏和显示 div
- google-compute-engine - 如何禁用谷歌计算工程师虚拟机管理器?
- flutter - 我的异步方法没有在“for”迭代之后进行
- javascript - 我在谷歌表格“TypeError:无法读取未定义的searchRecord @ Code.gs:213的属性'0'”上不断收到此错误
- swift - 导入 Swift 包时,Xcode 链接器可以删除未使用的代码吗?