编辑:已修复!
似乎我的问题是某些图层和z索引。也许聪明的人可以澄清一下。无论如何,我没有使用在scenebuilder中制作的GridPane,而是添加了一个新的GridPane来滚动窗格的锚点窗格。然后,我将座位图像注入新鲜的网格中。你可以在我的GitHub上找到它。
有点复杂和具体,但我会尽力解释。我遇到了将Click侦听器绑定到ImageView的问题。
我的JavaFX应用程序以图形方式在列车中的客舱中随机生成座位。目标是在变量中保存所选座位的索引(通过单击它)以用于以后的目的。如果要克隆,此项目位于my GitHub。下面是GUI的图片。
每列火车都有五个舱室,您可以通过它们进行切换,一个舱室的座椅每次都以图形方式打印。第一个出现的舱室有可点击的功能座椅。每次更换机舱时,都会调用此功能。
问题:
当我更换机舱并重新渲染座椅的新图像时,我再也无法点击它们了。这里有一个功能片段:
/**
* This method loads the cabin to the gridpane and also sets the indicator (label)
* Seat numbering is from up to down, left to right as follows:
* 1 5 9 ...
* 2 6 10 ...
*
* 3 7 11 ...
* 4 8 12 ...
*
* and seat index = number - 1
*
* @param cabinIndex number of cabinet to present as index
*/
private void loadGraphicalCabin(int cabinIndex) {
cabinIndexIndicator.setText(cabinIndex+1 + "");
Cabinet currentCabinet = selectedTrain.getCabinetList().get(cabinIndex);
currentCabinet.printCabin(); // Prints cabin to terminal
int currentSeatIndex;
// Iterate through cabin
for (int i = 0; i < 15; i++) { // column
for (int j = 0; j < 4; j++) { // row
currentSeatIndex = i*4 + j; // keep track of seat index
Seat currentSeat = currentCabinet.getSeatList().get(currentSeatIndex);
if (currentSeat.isReserved()) {
cabinSeatGridpane.add(new ImageView(new Image("FrontEnd/RES/Seats/taken.png")), i, j);
} else {
switch (currentSeat.getSeatType().substring(0,1)) {
case "a":
cabinSeatGridpane.add(new ImageView(new Image("FrontEnd/RES/Seats/allergy.png")), i, j);
break;
case "d":
cabinSeatGridpane.add(new ImageView(new Image("FrontEnd/RES/Seats/disabled.png")), i, j);
break;
case "e":
cabinSeatGridpane.add(new ImageView(new Image("FrontEnd/RES/Seats/economy.png")), i, j);
break;
case "f":
cabinSeatGridpane.add(new ImageView(new Image("FrontEnd/RES/Seats/family.png")), i, j);
break;
case "p":
cabinSeatGridpane.add(new ImageView(new Image("FrontEnd/RES/Seats/pet.png")), i, j);
break;
case "q":
cabinSeatGridpane.add(new ImageView(new Image("FrontEnd/RES/Seats/quiet.png")), i, j);
break;
}
// Attach click listener to current node
ImageView node = (ImageView) getNodeByRowColumnIndex(j, i, cabinSeatGridpane);
System.out.println("we reached seat "+ currentSeatIndex + " and node is: " + node);
int finalCurrentSeatIndex = currentSeatIndex; // Expressions in lambda must be final
// This lambda is only accessed the first time this method is called.. Why? Or is it..?
node.setOnMouseClicked((MouseEvent e) -> {
seatSelectorIndex = finalCurrentSeatIndex;
System.out.println("Seat " + seatSelectorIndex + " selected!");
selectedSeatNumberIndicator.setText((seatSelectorIndex + 1) + "");
});
}
}
}
cabinSeatGridpane.addRow(2);
cabinSeatGridpane.getRowConstraints().get(2).setPrefHeight(50);
}
在另一篇文章here中,问题是有一层拦截了clickevent,但我不相信这是我的问题。
注意 我发现有趣的是,在调试打印行(“我们到达座位......”+节点)中,节点(ImageView)与每个座位上的预备舱相同! F.ex:
小屋1:
we reached seat 0 and node is: ImageView@114e0495[styleClass=image-view]
小屋2:
we reached seat 0 and node is: ImageView@114e0495[styleClass=image-view]
照片管理
在这里您可以看到如何找到火车的设置(运行FrontEnd / Portal / portalTester.java)
这是选择座位时的gui!