感谢您对我上一篇文章的帮助(这也是我的第一篇文章)。我是新的stackoverflow。我希望我早些时候加入这个组织。人们在这里非常有礼貌和乐于助人。为什么默认的按钮鼠标事件处理程序会消耗一个事件?
无论如何,我一直在努力更好地理解javafx事件。对你们中的一些人来说,这似乎是另一个简单或“愚蠢”的问题。 为什么默认按钮鼠标事件处理程序似乎消耗一个事件?
从the oracle documents开始,位于页面底部,它声明“请注意,JavaFX UI控件的默认处理程序通常会消耗大部分输入事件。”它是连接到按钮的默认处理程序,我不知道?为什么我必须在目标节点上触发事件才能让事件调度链冒出来?
再次,任何反应将不胜感激! :)
public class MouseEventTest extends Application {
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Hello World");
Group root = new Group();
Scene scene = new Scene(root, 300, 250);
Button btn = new Button();
btn.setText("Hello World");
btn.setPrefSize(100, 100);
BorderPane layout = new BorderPane(btn);
layout.setPrefSize(300, 250);
root.getChildren().add(layout);
//This is the event dispatch chain
//primaryStage -> scene -> root -> layout -> btn (capturing phrase)
//btn -> layout -> root -> scene -> primaryStage (bubbling phrase)
btn.setOnMousePressed(e -> {
System.out.println("btn mouse pressed...");
//Why do I need to fire the mouse pressed event
//in order for the event to bubble up the chain?
//It seems like by default that the button setOnMousePressed event hanlder
//has consumed the event. Am I right?
//layout.fireEvent(e);
});
layout.setOnMousePressed(e -> { System.out.println("layout mouse pressed...");});
root.setOnMousePressed(e -> { System.out.println("root mouse pressed...");});
scene.setOnMousePressed(e -> { System.out.println("scene mouse pressed...");});
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
问题是“为什么事件不会冒泡组件层次结构?” –
我认为你需要改写你的问题...... – MeltingDog
@Juan和MeltingDog在oracle文档中,它是这样陈述的:“在达到事件目标并且所有已注册的过滤器处理完事件后,事件将沿着调度链从目标到根节点“。我想我应该改写胡安建议的问题。 – Tania