是否可以将动作侦听器添加到平铺或流窗格中的单个元素?
例如,在下面的示例中,如果用户单击图像,系统将打印出位置/文件路径。
谢谢你们。
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);
}
}