2016-04-15 68 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我的書,他們不區分堆疊窗格任何不同於任何其他窗格超出你可以有多個圖層。事實上,關於事件處理的書籍,他們說事件遍歷並消耗事件,事件遍歷從頂部到底部的孩子,直到他們達到消耗()。這究竟在哪裏解釋? –