我在TilePane中列出了图像,每个图像在单击时都应打印出其文件路径。但是,我得到类似的东西:
javafx.scene.image.Image@1a3d58b
如何获得以正常格式打印的路径,例如D:\ Xampp \
谢谢。
这是班级:
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);
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);
}
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);
}
}
答案 0 :(得分:0)
@durron597是正确的,Image对象没有加载图像的文件的Path详细信息,它不应该!
如果您将它用于演示程序或小型项目,将来不考虑使用/升级,这对您来说是个小技巧。
尝试使用
imageView.getImage().impl_getUrl()
这会让你回头
file:<your file path>
因此,为了过滤它,您可以使用
imageView.getImage().impl_getUrl().substring(5)
答案 1 :(得分:0)
您可以创建一个封装您拥有的信息的类以及图像,然后再引用该类的实例。或者,更简单地说,为什么不这样做:
String[] imageResources = new String[] {
"facebook.png",
"faviicon.png",
// etc
};
// ...
for (final String imageResource : imageResources) {
Image image = new Image(getClass().getResourceAsStream(imageResource));
ImageView imageView = new ImageView(image);
imageView.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
System.out.println("You clicked: "+imageResource);
}
});
flowPane.getChildren().add(imageView);
}