如何向Tile窗格中的各个元素添加动作侦听器

时间:2014-05-14 07:32:28

标签: java javafx actionlistener flowpane

是否可以向Tile或Flow Pane中的单个元素添加动作侦听器?

例如,在下面的示例中,如果用户单击图像,系统将打印出位置/文件路径。

谢谢大家。

public class FlowTileExample extends Application {

    @Override
    public void start(Stage primaryStage) {
        VBox root = new VBox(30);

        //loading images
        Image[] im = new Image[8];
        im[0] = new Image(getClass().getResourceAsStream("facebook.png"));
        im[1] = new Image(getClass().getResourceAsStream("faviicon.png"));
        im[2] = new Image(getClass().getResourceAsStream("jquery-logo.png"));
        im[3] = new Image(getClass().getResourceAsStream("linkedin_32.png"));
        im[4] = new Image(getClass().getResourceAsStream("loading1.png"));
        im[5] = new Image(getClass().getResourceAsStream("twitter.png"));
        im[6] = new Image(getClass().getResourceAsStream("twitter_32.png"));
        im[7] = new Image(getClass().getResourceAsStream("wp.png"));

        VBox up = new VBox(20);
        Text text4flow = new Text("Images in FlowPane");
        text4flow.setFont(Font.font("Calibri", FontWeight.BOLD, 30));
        text4flow.setUnderline(true);
        VBox.setMargin(text4flow, new Insets(10, 0, 0, 10));
        //creating Flow Pane
        FlowPane flowpane = new FlowPane();
        flowpane.setHgap(5);
        flowpane.setVgap(5);
        for (int i = 0; i < 8; i++) {
            flowpane.getChildren().add(new ImageView(im[i]));
        }

        up.getChildren().addAll(text4flow, flowpane);

        VBox down = new VBox(20);
        Text text4tile = new Text("Images in TilePane");
        text4tile.setFont(Font.font("Calibri", FontWeight.BOLD, 30));
        text4tile.setUnderline(true);
        VBox.setMargin(text4tile, new Insets(10, 0, 0, 10));

        //creating Tile Pane
        TilePane tilepane = new TilePane();
        tilepane.setHgap(5);
        flowpane.setVgap(5);
        for (int i = 0; i < 8; i++) {
            tilepane.getChildren().add(new ImageView(im[i]));
        }
        down.getChildren().addAll(text4tile, tilepane);
        root.getChildren().addAll(up, down);
        primaryStage.setTitle("Flow And Tile Panes Example");
        Scene scene = new Scene(root, 500, 600);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

2 个答案:

答案 0 :(得分:1)

您需要向ImageView实例添加某种处理程序,例如......

ImageView imageView = new ImageView(im[i]);
imageView.setOnMouseClicked(new EventHandler<MouseEvent>() {
    @Override
    public void handle(MouseEvent t) {
        System.out.println("Oh dear lord, they clicked me!?");
    }
});

或者你可以创建一个通用的处理程序......

EventHandler mouseHandler = new EventHandler<MouseEvent>() {
    @Override
    public void handle(MouseEvent t) {
        ImageView imageView = (ImageView) t.getSource();
        System.out.println("You clicked " + imageView.getImage());
    }
};

for (int i = 0; i < 8; i++) {
    ImageView imageView = new ImageView(im[i]);
    imageView.setOnMouseClicked(mouseHandler);
    flowpane.getChildren().add(imageView);
}

例如......

您应该仔细查看Handling JavaFX Events了解更多详情

答案 1 :(得分:1)

作为MadProgrammer的补充,为了快速管理自定义数据,您可以利用节点的userData属性,而不是构建自己的数据模型。 userData可以在不同的情况下使用,例如将一些上下文数据附加到相关节点:

VBox root = new VBox();
final Glow glow = new Glow();

EventHandler<MouseEvent> handlerMouseClicked = (MouseEvent t) -> {
    System.out.println("url = " + ((ImageView) t.getSource()).getUserData());
};

EventHandler<MouseEvent> handlerMouseEntered = (MouseEvent t) -> {
    ((ImageView) t.getSource()).setEffect(glow);
};

EventHandler<MouseEvent> handlerMouseExited = (MouseEvent t) -> {
    ((ImageView) t.getSource()).setEffect(null);
};

String[] imageNames = {"rere.png", "rere_1.png", "rere_2.png"};
for (String imageName : imageNames) {
    String url = getClass().getResource(imageName).toExternalForm();
    ImageView iv = new ImageView(new Image(url));
    iv.setUserData(url);
    iv.setOnMouseEntered(handlerMouseEntered);
    iv.setOnMouseExited(handlerMouseExited);
    iv.setOnMouseClicked(handlerMouseClicked);
    root.getChildren().add(iv);
}