events - 将 EventHandler 添加到包含在 VBox 中的 TilePane 中的 ImageView?

标签 events javafx mouseevent

我有以下架构:

  • 一个 VBox,包含一个 HBox 和一个 TilePane。

  • HBox 中有按钮、标签和文本字段。

    每次单击根 (HBox) 时,我都应该向磁贴 Pane 添加一个 ImageView。此 ImageView 包含一个图像(例如:“2.jpg”)。磁贴 Pane 组件的最大数量为 5。

    每次单击图像时,我都应该将新图像加载到单击的 ImageView,例如“1.jpg”。它不工作。当我点击我的图像时,就像我点击根一样,所以它创建了另一个 TilePane 单元格。这是代码,你能帮我吗?
    /*
     * 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 dadao1;
    
    import java.util.HashSet;
    import javafx.application.Application;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.control.Label;
    import javafx.scene.control.TextField;
    import javafx.scene.image.ImageView;
    import javafx.scene.input.MouseEvent;
    import javafx.scene.layout.BorderPane;
    import javafx.scene.layout.HBox;
    import javafx.scene.layout.StackPane;
    import javafx.scene.layout.TilePane;
    import javafx.scene.layout.VBox;
    import javafx.scene.paint.Color;
    import javafx.stage.Stage;
    
    /**
     *
     * @author Ambra
     */
    public class Dadao1 extends Application {
        VBox root;
        HashSet dadi = new HashSet();
        //static int numeroDadi = 0;
        @Override
        public void start(Stage primaryStage) {       
    
            setGui();
            Scene scene = new Scene(root, 300, 300);
    
            primaryStage.setTitle("Hello World!");
            primaryStage.setScene(scene);
            primaryStage.show();
        } 
        /**
         *This private method sets up the GUI.
         * No parameters are required
         */
        private void setGui(){
            root = new VBox();
            HBox buttons = new HBox();
            final Button newGame = new Button("New Game");
            final Button print = new Button("Print");
            final Button animation = new Button("Moving");
            animation.addEventHandler(ActionEvent.ACTION, new EventHandler<ActionEvent>() {
                // this button is labeled "Moving" at the begin. If pressed it changes its label to "Dissolving" and viceversa.
                @Override
                public void handle(ActionEvent event) {
                    if (animation.getText().equals(new String("Moving")))
                        animation.setText("Dissolving");
                    else animation.setText("Mooving");
                }
            });
            final Label score = new Label("Total");
            final TextField points = new TextField();
            final Label pointsLabel = new Label("Score");
            root.setStyle("-fx-background-color: green");
    
            buttons.getChildren().addAll(newGame,print,animation,score,points,pointsLabel);
            final TilePane dadiPane = new TilePane();
            dadiPane.setVgap(10);
            dadiPane.setHgap(10);
            root.getChildren().addAll(buttons, dadiPane);
    
            root.setOnMouseClicked(new EventHandler<MouseEvent>() {
    
                @Override
                public void handle(MouseEvent event) {
                    if(dadi.size()<5){
                        System.out.println("Adding img");
                        final ImageView img = new ImageView("2.jpg");
                        // should I put final in front of ImageView?
                        img.addEventHandler(ActionEvent.ACTION, new EventHandler<ActionEvent>() {
    
                            @Override
                            public void handle(ActionEvent event) {
                                // I want that when a tile is pressed an event occours.
                                // that event should be "add a new image to the ImageView just clicked",
                                // for example: img is not "2.jpg" but "3.jpj", by the way, I'm not able neither 
                                // to to print the folowing messagge :(
                                // It's like my root is pressed even if my mouse ha clicked at the image in img var.
                                System.out.println("Tile pressed ");
                            }
                        });
                        dadi.add(img);
                        dadiPane.getChildren().add(img);
                    }else System.out.println("You cannot create more than 5 dices");
                }
            });
        }
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            launch(args);
        }
    
    }
    

    最佳答案

    ImageView s 不生成 ActionEvent s;因此,您的事件处理程序从未被调用也就不足为奇了。由于鼠标事件不是由 ImageView 处理的,它传播到容器。

    尝试

    img.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
    
         @Override
         public void handle(MouseEvent event) {
             System.out.println("Tile pressed ");
             event.consume();
         }
    });
    

    关于events - 将 EventHandler 添加到包含在 VBox 中的 TilePane 中的 ImageView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25550518/

    相关文章:

    JavaFX 按钮垃圾邮件 MouseExit 和 MouseEntered

    angular - 如何在纯 Typescript 中生成 Escape 按键(不带 JQuery)

    javascript - 添加/删除事件监听器

    JavaFX 硬焦点丢失在其他窗口区域单击

    java - 如何通过 JavaFX WebEngine 通过 CSS 类名获取 HTML 元素?

    JavaFx 文本字段未在 GUI 上更新

    Java Swing 鼠标点击不工作

    java - 内部 jpanel 上的鼠标事件放置在另一个 jpanel 上

    c# - 捕获所有 Windows 7 触摸事件,而不仅仅是在我的窗体具有焦点时捕获

    c# - 如何制作Unity/C#自定义事件函数