提问者:小点点

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


我有以下模式:

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

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

每次单击根(HBox)时,我都应该向磁贴窗格添加一个ImageView。此ImageView应包含一个图像(例如:“2. jpg”)。磁贴窗格组件的最大值为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);
    }

}

共3个答案

匿名用户

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

试试看

img.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {

     @Override
     public void handle(MouseEvent event) {
         System.out.println("Tile pressed ");
         event.consume();
     }
});

匿名用户

@James_D是正确的;但是注意Java8可以使用更简单的语法:

img.addEventHandler(MouseEvent.MOUSE_CLICKED, event -> {
         System.out.println("Tile pressed ");
         event.consume();
     });

匿名用户

注意MouseEvent.MOUSE_CLICKED是一个来自javafx的类,而不是来自java. awt.event包

imgView. addEventHandler(javafx.scene.input.MouseEvent.MOUSE_CLICKED, event -> {
    //event clicked here
});

对于错误导入java. awt.event.MouseEvent类的人