2014-08-28 56 views
4

我有以下架構:將EventHandler添加到VBox中包含的TilePane中包含的ImageView中?

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

在HBox中有按鈕,標籤和文本字段。

每當我點擊根(HBox),我應該添加一個ImageView到瓷磚窗格。這ImageView shold包含一個圖像(例如:「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); 
    } 

} 
+0

嘗試將eventListenet添加到ImageView的,而不是剷除 – ItachiUchiha 2014-08-29 02:26:17

+0

我有一個EventHandlet增加一方面是因爲他們都需要做不同的事情:根必須添加單擊時將ImageView添加到「平鋪」窗格,並且單擊時ImageView必須更改圖像。 – bogALT 2014-08-29 09:11:35

回答

20

ImageView S不生成ActionEvent秒;所以毫不奇怪你的事件處理程序從不被調用。由於鼠標事件沒有被ImageView處理,所以它傳播到容器。

嘗試

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

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

它不起作用。它說'類Node中的方法addEventHandler不能被應用於給定的類型;要求:EventType ,EventHandler ;發現:事件類型,<匿名EventHandler >' – bogALT 2014-08-29 08:22:44

+0

我想我解決了它把'新的EventHandler ()'代替'新的EventHandler ()'。 編輯:它可以工作,但它同時調用:'img'的事件和'root'的事件。 – bogALT 2014-08-29 08:28:45

+0

是的,錯字...對不起。爲了防止事件一旦處理就傳播到'HBox',就調用'event.consume();'。我修復了代碼(包括錯字和額外的電話)。 – 2014-08-29 11:52:15

1

@James_D是正確的;但要注意對Java 8,你可以使用簡單的語法:

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