2016-04-15 91 views
0

我有一个复杂场景的javafx8应用程序。它由一个分割面板组成,在顶部分段中,我有一个具有多层级别(子级)的堆叠面板。最后面的是只需要重新调整大小的背景,另一个是网格线,纬度/经度。另一个用于绘制,最后是另一个用滑动复合控制。当复合物(按钮,标签,组合框和文本框在栈帧的最后一个子窗口中,它被正确截取并在鼠标和键盘事件上作用时),但是在顶部没有事件传播到下面的子窗格。切换顺序,现在按钮和轴控制工作,但现在没有任何东西传播到下面的复合控制按钮看不到鼠标动作Javafx8 stackpane儿童阻止鼠标事件

这似乎与Stackpane - MouseClick to be listened by both its children类似,但它不是真的很清楚它是不是它清楚地说有一个正确的答案

我简化了我的方案,用一个非常简单的测试,源代码跟随在这里我有一个StackPane有两个孩子,两个VBox,一个如果左上对齐,右上角,只有第二个按钮,右上方响应被按下。

为什么。我有我的应用程序与多个窗格的原因是性能。两个绘图操作都很昂贵且频繁,我不希望不断重绘静态或接近静态的布局。

FXML

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

<?import javafx.scene.control.*?> 
<?import java.lang.*?> 
<?import javafx.scene.layout.*?> 

<StackPane fx:id="containerPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="mouseeventdemo.MouseEventDemoController"> 
    <children> 
     <VBox fx:id="container1" prefHeight="200.0" prefWidth="100.0"> 
     <children> 
      <Button fx:id="leftButton" mnemonicParsing="false" onAction="#leftButtonDown" text="left button" /> 
     </children> 
     </VBox> 
     <VBox fx:id="container2" alignment="TOP_RIGHT" prefHeight="200.0" prefWidth="100.0"> 
     <children> 
      <Button fx:id="rightButton" mnemonicParsing="false" onAction="#rightButtonDown" text="Right Button" /> 
     </children> 
     </VBox> 
    </children> 
</StackPane> 

控制器:

package mouseeventdemo; 
/** 
* Sample Skeleton for 'FXMLDocument.fxml' Controller Class 
*/ 

import java.net.URL; 
import java.util.ResourceBundle; 
import javafx.event.ActionEvent; 
import javafx.fxml.FXML; 
import javafx.scene.control.Button; 
import javafx.scene.layout.StackPane; 
import javafx.scene.layout.VBox; 

public class MouseEventDemoController { 

    @FXML // ResourceBundle that was given to the FXMLLoader 
    private ResourceBundle resources; 

    @FXML // URL location of the FXML file that was given to the FXMLLoader 
    private URL location; 

    @FXML // fx:id="containerPane" 
    private StackPane containerPane; // Value injected by FXMLLoader 

    @FXML // fx:id="container1" 
    private VBox container1; // Value injected by FXMLLoader 

    @FXML // fx:id="leftButton" 
    private Button leftButton; // Value injected by FXMLLoader 

    @FXML // fx:id="container2" 
    private VBox container2; // Value injected by FXMLLoader 

    @FXML // fx:id="rightButton" 
    private Button rightButton; // Value injected by FXMLLoader 

    @FXML 
    void leftButtonDown(ActionEvent event) { 
     System.out.println("leftButtonPressed"); 
    } 

    @FXML 
    void rightButtonDown(ActionEvent event) { 
     System.out.println("rightButtonPressed"); 
    } 

    @FXML // This method is called by the FXMLLoader when initialization is complete 
    void initialize() { 
     assert containerPane != null : "fx:id=\"containerPane\" was not injected: check your FXML file 'FXMLDocument.fxml'."; 
     assert container1 != null : "fx:id=\"container1\" was not injected: check your FXML file 'FXMLDocument.fxml'."; 
     assert leftButton != null : "fx:id=\"leftButton\" was not injected: check your FXML file 'FXMLDocument.fxml'."; 
     assert container2 != null : "fx:id=\"container2\" was not injected: check your FXML file 'FXMLDocument.fxml'."; 
     assert rightButton != null : "fx:id=\"rightButton\" was not injected: check your FXML file 'FXMLDocument.fxml'."; 

    } 
} 

主要

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package mouseeventdemo; 

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

/** 
* 
* @author walt 
*/ 
public class MouseEventDemo 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); 
    } 

} 

在我的应用程序后最窗格是一个背景画布。它需要绘制,但一旦重新调整窗口大小时重绘。

接下来是一个具有纬度/长度网格的画布。只需在窗口大小更改或垂直或水平缩放更改时重新绘制它。

其次是主绘图画布在以下

<vbox> 
<hbox> 
    drawing canvas 
    vertical scale 
</hbox> 
    horizontal scale 
</vbox> 

下携带一个或多个旗杆其中垂直杆是像光罩计算尺。这些需要是可拖动的。如果这个图层位于顶部,则它完美地滑动,但两个比例无法获得焦点。如果顶部的两个窗格是相反的,那么这些标尺完美地工作,但旗杆对象从不接收事件。

这两个按钮示例非常简单地重复了我所看到的内容。

谢谢!

enter image description here

回答

1

JavaFX中只有最上面的节点(在你的例子container2的)的mouseposition下eventhandling将被确定为目标的鼠标点击事件,这就是为什么container1没有收到该事件。

目标节点是事件调度链中的末端节点。当您单击leftButton时,鼠标单击事件从场景图的根节点传播到目标节点(container2),如果没有消耗,则返回。Container1如不可正对分发链,将不会收到事件: http://docs.oracle.com/javafx/2/events/processing.htm

你可以尝试使用container.setPickOnBounds(false)

+0

我添加了应用程序的屏幕截图。该fxml应该是该截图的正确一个。否则,这不是这个问题的要点。 –

+0

要部分重现问题,它会出现在Linux和Windows上,顶部窗格中只有控件显示接收事件。这两个按钮的应用程序是一个最小的应用程序来演示问题。鼠标右键工作,鼠标左键不行。如果孩子以其他顺序加载,则左按钮将起作用,右按钮则不会。 –

+0

,同时我愿意为您回答此问题而提供信用。我不明白为什么。我确实找到http://stackoverflow.com/questions/24607969/mouse-events-get-ignored-on-the-underlying-layer。这意味着这实际上是一个dup.Of我的书,他们不区分堆叠窗格任何不同于任何其他窗格超出你可以有多个图层。事实上,关于事件处理的书籍,他们说事件遍历并消耗事件,事件遍历从顶部到底部的孩子,直到他们达到消耗()。这究竟在哪里解释? –