解决方案/解决方法
- 使用GridPane而不是TilePane。
- 当用户单击网格中的某个项目时,在该项目上调用toFront。
示例代码
我将以下代码添加到基于网格布局的计算器中,该网格布局看起来类似于示例屏幕截图中的平铺布局。使用修改后的应用程序,我没有遇到计算器按钮重叠的情况。令我惊讶的是,虽然它们看起来有点奇怪,但按比例缩放的按钮使计算器更易于使用......
button.setOnMouseEntered(new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent mouseEvent) {
button.toFront();
button.setScaleX(1.6); button.setScaleY(1.6);
}
});
button.setOnMouseExited(new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent mouseEvent) {
button.setScaleX(1); button.setScaleY(1);
}
});
背景
As you note, the overlapping issue is unresolvable in some stock Panes like TilePane
. This is because the z-order as well as the relative layout position of a node are both defined by the relative position of the node in the TilePane's child list. In such Panes, you cannot change the z-order and bring it to the front without also moving the node to the last layout position in the TilePane
.
A GridPane
does not suffer from the same issues as a TilePane
because it's child node list order only defines the z-order of it's child nodes, not the layout positioning of those child nodes.
Is there a way (a hack?) to implement my own code of retrieving the children order for drawing when JavaFX repaints TilePanel?
Yes, you could hack the TilePane code if you have the skills.