Events 是否将EventHandler添加到VBox中TilePane中包含的ImageView?
我有以下模式:Events 是否将EventHandler添加到VBox中TilePane中包含的ImageView?,events,javafx,mouseevent,Events,Javafx,Mouseevent,我有以下模式: 一个VBox,包含一个HBox和一个TilePane HBox中有按钮、标签和文本字段 每次单击根目录(HBox),我都应该向平铺窗格添加一个ImageView。这个ImageView应该包含一个图像(例如:“2.jpg”)。平铺窗格组件的最大值为5 每次单击图像时,我都应该将一个新图像加载到单击的ImageView,例如“1.jpg”。它不起作用。当我点击我的图像时,就像我点击根一样,它会创建另一个TilePane单元。这是密码,你能帮我吗 /* * To change
- 一个VBox,包含一个HBox和一个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);
}
}
/*
*要更改此许可证标题,请在“项目属性”中选择“许可证标题”。
*要更改此模板文件,请选择工具|模板
*然后在编辑器中打开模板。
*/
包装大刀1;
导入java.util.HashSet;
导入javafx.application.application;
导入javafx.event.ActionEvent;
导入javafx.event.EventHandler;
导入javafx.scene.scene;
导入javafx.scene.control.Button;
导入javafx.scene.control.Label;
导入javafx.scene.control.TextField;
导入javafx.scene.image.ImageView;
导入javafx.scene.input.MouseEvent;
导入javafx.scene.layout.BorderPane;
导入javafx.scene.layout.HBox;
导入javafx.scene.layout.StackPane;
导入javafx.scene.layout.TilePane;
导入javafx.scene.layout.VBox;
导入javafx.scene.paint.Color;
导入javafx.stage.stage;
/**
*
*@作者Ambra
*/
公共类Dadao1扩展应用程序{
VBox根;
HashSet dadi=新的HashSet();
//静态int numeroDadi=0;
@凌驾
公共无效开始(阶段初始阶段){
setGui();
场景=新场景(根,300,300);
setTitle(“你好,世界!”);
初级阶段。场景(场景);
primaryStage.show();
}
/**
*这个私有方法设置GUI。
*不需要任何参数
*/
私有void setGui(){
root=新的VBox();
HBox按钮=新的HBox();
最终按钮newGame=新按钮(“新游戏”);
最终按钮打印=新按钮(“打印”);
最终按钮动画=新按钮(“移动”);
animation.addEventHandler(ActionEvent.ACTION,neweventhandler()){
//此按钮在开始时标记为“移动”。如果按下此按钮,其标签将更改为“溶解”,反之亦然。
@凌驾
公共无效句柄(ActionEvent事件){
if(animation.getText().equals(新字符串(“移动”))
animation.setText(“解散”);
else animation.setText(“Mooving”);
}
});
最终标签分数=新标签(“总”);
最终文本字段点=新文本字段();
最终标签点标签=新标签(“分数”);
root.setStyle(“-fx背景色:绿色”);
buttons.getChildren().addAll(新游戏、打印、动画、分数、积分、pointsLabel);
最终TilePane数据烷=新TilePane();
dadiPane.setVgap(10);
dadiPane.setHgap(10);
root.getChildren().addAll(按钮,数据平面);
setOnMouseClicked(新的EventHandler(){
@凌驾
公共无效句柄(MouseeEvent事件){
如果(dadi.size()
试一试
img.addEventHandler(MouseEvent.MOUSE_单击,新建EventHandler()){
@凌驾
公共无效句柄(MouseeEvent事件){
System.out.println(“压瓦”);
event.consume();
}
});
@James\u D是正确的;但请注意,对于Java 8,您可以使用更简单的语法:
img.addEventHandler(MouseEvent.MOUSE_CLICKED, event -> {
System.out.println("Tile pressed ");
event.consume();
});
请注意,MouseEvent.MOUSE\u CLICKED
是来自javafx的类,而不是来自java.awt.event
包
imgView. addEventHandler(javafx.scene.input.MouseEvent.MOUSE_CLICKED, event -> {
//event clicked here
});
对于那些错误地导入java.awt.event.MouseEvent
class的人,尝试将EventListNet添加到ImageView中,而不是添加到root中。我必须将EventHandlet添加到两者中,因为它们都必须执行不同的操作:root必须在单击时将ImageView添加到平铺窗格中,ImageView必须在单击时更改图像。这不起作用它说类节点中的方法addEventHandler不能应用于给定的类型;必需:EventType,EventHandler我想我用'neweventhandler()'代替neweventhandler()解决了这个问题
.Edit:它可以工作,但它同时调用:img
的事件和root
的事件。是的,输入错误…很抱歉。为了防止事件传播到HBox
,一旦处理完毕,就调用event.consume();
。我修复了代码(输入错误和附加调用)。
imgView. addEventHandler(javafx.scene.input.MouseEvent.MOUSE_CLICKED, event -> {
//event clicked here
});