2014-02-21 27 views
1

我想讓它工作一段時間,我找不出我的代碼有什麼問題。這導致我相信在SubScene Mouse偵聽器中存在一些問題。任何想法是讚賞。JavaFX 2.2 SubScene中的MouseEvent無法正常工作

基本上我有一個場景包含兩個子場景,一個用於工具欄,另一個用於地板,有一堆線條,看起來像瓷磚。我添加了鼠標監聽器,以便當我點擊地板並移動鼠標時,攝像機會像移動到地板上一樣移動。 問題是,當我點擊第一條垂直線和第一條水平線之間的交叉點時(yup,花了我一段時間來弄清楚),地板才識別鼠標事件。鼠標事件應發生在整個樓層的任何地方。

這是代碼。

import javafx.application.Application; 
import javafx.collections.ObservableList; 
import javafx.event.ActionEvent; 
import javafx.event.EventHandler; 
import javafx.scene.Group; 
import javafx.scene.Node; 
import javafx.scene.PerspectiveCamera; 
import javafx.scene.Scene; 
import javafx.scene.SceneAntialiasing; 
import javafx.scene.SubScene; 
import javafx.scene.control.Button; 
import javafx.scene.input.MouseEvent; 
import javafx.scene.shape.Line; 
import javafx.stage.Stage; 

public class FloorTest extends Application { 

double mousex, mousey; 

@Override 
public void start(Stage primaryStage) { 
    Button btn = new Button(); 
    btn.setText("Say 'Hello World'"); 
    btn.setOnAction(new EventHandler<ActionEvent>() { 

     @Override 
     public void handle(ActionEvent event) { 
      System.out.println("Hello World!"); 
     } 
    }); 

    Group bargroup = new Group(); 
    SubScene bar = new SubScene(bargroup, 300, 20, true, SceneAntialiasing.DISABLED); 
    bargroup.getChildren().add(btn); 

    Group floorgroup = new Group(); 
    SubScene floor = new SubScene(floorgroup, 300, 250, true, SceneAntialiasing.DISABLED); 
    ObservableList<Node> list = floorgroup.getChildren(); 
    for(int i = 0; i < (300/20); i++) 
    { 
     double x = i * 20; 
     Line line = new Line(x, 0, x, 250); 
     list.add(line); 
    } 
    for(int i = 0; i < (250/20); i++) 
    { 
     double y = i * 20; 
     Line line = new Line(0, y, 300, y); 
     list.add(line); 
    } 

    PerspectiveCamera camera = new PerspectiveCamera(false); 
    camera.setNearClip(0.1); 
    camera.setFarClip(10000.0); 
    camera.setTranslateZ(-200); 
    floor.setCamera(camera); 

    floor.setOnMousePressed((MouseEvent event) -> { 
     mousex = event.getSceneX(); 
     mousey = event.getSceneY(); 
    }); 

    floor.setOnMouseDragged((MouseEvent event) -> { 
     double x = event.getSceneX(); 
     double y = event.getSceneY(); 
     camera.relocate(camera.getLayoutX() + (x - mousex), camera.getLayoutY() + (y - mousey)); 
    }); 



    Group mainroot = new Group(); 
    mainroot.getChildren().addAll(floor, bar);   

    Scene scene = new Scene(mainroot, 300, 250, true); 

    primaryStage.setTitle("Hello World!"); 
    primaryStage.setScene(scene); 
    primaryStage.show(); 
} 

/** 
* The main() method is ignored in correctly deployed JavaFX application. 
* main() serves only as fallback in case the application can not be 
* launched through deployment artifacts, e.g., in IDEs with limited FX 
* support. NetBeans ignores main(). 
* 
* @param args the command line arguments 
*/ 
public static void main(String[] args) { 
    launch(args); 
} 

} 

回答

0

似乎設置subscene.setPickOnBounds(true)應在適當的承認幫助整個子場景的鼠標事件。使用javafx 8測試。

+0

謝謝。這確實使它工作。我不明白爲什麼你必須這樣做才能讓鼠標工作。如果子場景需要使用鼠標偵聽器,則可能應該將其包含在setOnMouseXXX中 – PhucLy

0

請嘗試在事件處理程序使用的scene代替floor變量

例子:

scene.setOnMousePressed((MouseEvent event) -> { 
    mousex = event.getSceneX(); 
    mousey = event.getSceneY(); 
}); 

scene.setOnMouseDragged((MouseEvent event) -> { 
     double x = event.getSceneX(); 
     double y = event.getSceneY(); 
     camera.relocate(camera.getLayoutX() + (x - mousex), camera.getLayoutY() + (y - mousey)); 
    }); 

,可以幫助我

+0

感謝您的迴應。我試圖利用subscene的好處,在場景中我可以有多個場景,每個場景都可以做不同的事情。我知道使用場景作品。 – PhucLy

+0

我檢查了一些其他的答案。也許你必須使用Mediator模式。這對於那種交流很有用。 –