2016-10-13 8 views
2

상단의 장식 창은 마우스 이벤트를 처리/사전 처리하고 싶지만 소비하지 않아야합니다. 즉, 모든 겹친 창은 장식 창과 겹치지 않는 것처럼 작동해야합니다.JavaFX의 모든 기본 겹친 창에 마우스 이벤트를 위임하는 방법은 무엇입니까?

어떻게 하시겠습니까? 몇 번 시도해 보았지만 실패했습니다.

다음은 3 개의 창으로 구성된 코드입니다. 녹색 하나는 "꾸미기"입니다. 작업은 마우스 이벤트를 투명하게 만드는 것입니다. 노란색 및 파란색 창은 작업자 창입니다. 그들은 녹색 판으로 겹쳐지지 않은 것처럼 작동해야합니다.

그러나 녹색 창은 마우스 이벤트를 수신해야합니다.

댓글 라인은 I 시도와 의견이 잘못 등장하는 말을 나타냅니다

import javafx.application.Application; 
import javafx.event.Event; 
import javafx.event.EventHandler; 
import javafx.event.EventType; 
import javafx.geometry.Insets; 
import javafx.scene.Scene; 
import javafx.scene.input.MouseEvent; 
import javafx.scene.layout.*; 
import javafx.scene.paint.Color; 
import javafx.stage.Stage; 

/** 
* Created by dims on 13.10.2016. 
*/ 
public class DelegateEventsToOverlappedNodes extends Application{ 


    @Override 
    public void start(Stage primaryStage) throws Exception { 

     StackPane root = new StackPane(); 
     root.setBackground(new Background(new BackgroundFill(Color.RED, CornerRadii.EMPTY, Insets.EMPTY))); 


     AnchorPane yellowPane = new AnchorPane(); 
     yellowPane.setBackground(new Background(new BackgroundFill(Color.YELLOW, CornerRadii.EMPTY, Insets.EMPTY))); 
     yellowPane.setOnMouseClicked(new EventHandler<MouseEvent>() { 
     @Override 
     public void handle(MouseEvent event) { 
      System.out.println("yellowPane clicked"); 
     } 
     }); 


     root.getChildren().add(yellowPane); 


     Pane bluePane = new Pane(); 
     bluePane.setBackground(new Background(new BackgroundFill(Color.BLUE, CornerRadii.EMPTY, Insets.EMPTY))); 
     bluePane.setOnMouseClicked(new EventHandler<MouseEvent>() { 
     @Override 
     public void handle(MouseEvent event) { 
      System.out.println("bluePane clicked"); 
     } 
     }); 

     AnchorPane.setLeftAnchor(bluePane, 200.); 
     AnchorPane.setRightAnchor(bluePane, 200.); 
     AnchorPane.setTopAnchor(bluePane, 200.); 
     AnchorPane.setBottomAnchor(bluePane, 200.); 


     yellowPane.getChildren().add(bluePane); 




     AnchorPane greenPane = new AnchorPane(); 
     greenPane.setBackground(new Background(new BackgroundFill(Color.rgb(0, 255, 0, 0.9), CornerRadii.EMPTY, Insets.EMPTY))); 
     greenPane.setOnMouseClicked(new EventHandler<MouseEvent>() { 
     @Override 
     public void handle(MouseEvent event) { 
      System.out.println("greenPane clicked"); 
     } 
     }); 


     // greenPane.setVisible(false); // green pane invisible at all 

     // greenPane.setMouseTransparent(true); // green clicked doesn't occur 

     // greenPane.addEventHandler(Event.ANY, event -> yellowPane.fireEvent(event)); // works for yellow pane, but not for blue sub pane 



     root.getChildren().add(greenPane); 


     Scene scene = new Scene(root, 800, 600); 

     primaryStage.setScene(scene); 
     primaryStage.setTitle("DelegateEventsToOverlappedNodes"); 
     primaryStage.show(); 

    } 

    public static void main(String[] args) { 
     DelegateEventsToOverlappedNodes.launch(args); 
    } 
} 

답변

0

당신은 다시 greenPane 마우스 투명 설정하고 여기에 마우스 이벤트 전처리 root에 이벤트 필터를 추가 할 수 있습니다

greenPane.setMouseTransparent(true); 
root.addEventFilter(MouseEvent.MOUSE_CLICKED, event -> System.out.println(event)); 

필터는 이벤트 처리 단계에서 이벤트를 수신하는 반면 처리기는 이벤트 버블 링 단계에서 트리거됩니다. 이벤트 처리기를 사용할 수도 있지만,이 경우에는 필터가 더 관용적이라고 생각합니다 (자세한 내용은 here 참조).

귀하의 코드 :

import javafx.application.Application; 
import javafx.geometry.Insets; 
import javafx.scene.Scene; 
import javafx.scene.input.MouseEvent; 
import javafx.scene.layout.*; 
import javafx.scene.paint.Color; 
import javafx.stage.Stage; 

public class DelegateEventsToOverlappedNodes extends Application { 

    public static void main(String[] args) { 
     DelegateEventsToOverlappedNodes.launch(args); 
    } 

    @Override 
    public void start(Stage primaryStage) throws Exception { 
     // Yellow pane. 
     AnchorPane yellowPane = new AnchorPane(); 
     yellowPane.setBackground(new Background(new BackgroundFill(Color.YELLOW, CornerRadii.EMPTY, Insets.EMPTY))); 
     yellowPane.setOnMouseClicked(event -> System.out.println("yellowPane clicked")); 

     // Blue pane. 
     AnchorPane bluePane = new AnchorPane(); 
     bluePane.setBackground(new Background(new BackgroundFill(Color.BLUE, CornerRadii.EMPTY, Insets.EMPTY))); 
     bluePane.setOnMouseClicked(event -> System.out.println("bluePane clicked")); 
     AnchorPane.setLeftAnchor(bluePane, 200.0); 
     AnchorPane.setRightAnchor(bluePane, 200.0); 
     AnchorPane.setTopAnchor(bluePane, 200.0); 
     AnchorPane.setBottomAnchor(bluePane, 200.0); 

     // Green pane. 
     AnchorPane greenPane = new AnchorPane(); 
     greenPane.setBackground(new Background(new BackgroundFill(Color.rgb(0, 255, 0, 0.9), CornerRadii.EMPTY, Insets.EMPTY))); 
     greenPane.setMouseTransparent(true); 

     // Root pane. 
     StackPane rootPane = new StackPane(); 
     rootPane.setBackground(new Background(new BackgroundFill(Color.RED, CornerRadii.EMPTY, Insets.EMPTY))); 
     rootPane.addEventFilter(MouseEvent.MOUSE_CLICKED, event -> System.out.println(event)); 

     // Layout and scene. 
     yellowPane.getChildren().add(bluePane); 
     rootPane.getChildren().addAll(yellowPane, greenPane); 

     primaryStage.setScene(new Scene(rootPane, 800.0, 600.0)); 
     primaryStage.setTitle("DelegateEventsToOverlappedNodes"); 
     primaryStage.show(); 
    } 

} 

만 전처리 이벤트에 greenPane을 만든 경우는 더 이상 필요 없다.