java8内でjavafxを使用するプロジェクトに取り組んでいます。ボタン (javafx.scene.control.Button) と別のペインを含む (javafx.scene.layout.Pane クラスの) パネル。マウスクリックイベントが親にバブルアップされることを期待しています。しかし、これはペインをクリックした場合にのみ発生し、ボタンをクリックした場合には発生しません。以下は、この動作の非常に単純な例のコードです。この問題を解決するための提案はありますか? ペインに基づいて独自のボタンを作成できることはわかっていますが、これは厄介な回避策です。乾杯
package application;
import javafx.animation.TranslateTransition;
import javafx.application.Application;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.util.Duration;
public class Main extends Application {
double curScale;
@Override
public void start(Stage primaryStage) {
try {
BorderPane root = new BorderPane();
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
Button btn = new Button("test");
btn.setLayoutX(50);
Pane p2 = new Pane();
p2.setPrefSize(20,20);
p2.setStyle("-fx-background-color: #440000");
Pane p = new Pane();
p.getChildren().add(btn);
p.getChildren().add(p2);
root.setTop(p);
p.setOnMouseClicked(new EventHandler<Event>() {
@Override
public void handle(Event event) {
System.out.println("event source " + event.getSource() + " target : " + event.getTarget());
}
});
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}