java - 致力于在 JavaFX 中创建图片库。无法正常显示图像

标签 java javafx imageview gallery

我正在使用 javafx 创建图片库。我在互联网上发现了很多与此相关的内容,但无法针对此问题获得任何合适的帮助。我必须创建一个图片库,如 picasa 查看器。所有图像都在我的 ImageView 中以缩略图 View 显示,之后当我选择弹出查看器中的图像时。我为此做了一些代码,但没有得到正确的输出。文件夹中的所有图像都从相同的坐标重新绘制。 下面是我的代码和输出。

@Override
public void initialize(URL url, ResourceBundle rb) {

    String path = "/home/ubuntu/eclipse with liferay/Desktop/imagetest/";

    File folder = new File(path);
    File[] listOfFiles = folder.listFiles();

    for (final File file : listOfFiles) {

            ImageView imageView;
            imageView = createImageView(file);
            imagecontainer.getChildren().addAll(imageView);

    }

}
 private ImageView createImageView(final File imageFile) throws FileNotFoundException, FileNotFoundException, FileNotFoundException, FileNotFoundException {
    // DEFAULT_THUMBNAIL_WIDTH is a constant you need to define  
    // The last two arguments are: preserveRatio, and use smooth (slower) resizing  

    ImageView imageView = null;
    try {

        final Image image;

        image = new Image(new FileInputStream(imageFile), DEFAULT_THUMBNAIL_WIDTH, 0, true, true);
        imageView = new ImageView(image);

    } catch (FileNotFoundException ex) {
        Logger.getLogger(GalleryController.class.getName()).log(Level.SEVERE, null, ex);
    }
    return imageView;

}

}

output

请帮助我解决我的问题。我想将图像一张一张地显示为缩略图。

最佳答案

您需要创建一个 TilePane 并将 ImageView 添加到其中。如果需要,您可以拥有一个 ScrollPane。双击以创建全屏预览的完整示例如下所示。您当然可以为创建 FXML 做必要的更改 :)

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.TilePane;
import javafx.scene.paint.Color;
import javafx.stage.Screen;
import javafx.stage.Stage;

public class ImageGallery extends Application {

    Stage stage;

    @Override
    public void start(Stage primaryStage) throws Exception {
        stage = primaryStage;
        ScrollPane root = new ScrollPane();
        TilePane tile = new TilePane();
        root.setStyle("-fx-background-color: DAE6F3;");
        tile.setPadding(new Insets(15, 15, 15, 15));
        tile.setHgap(15);

        String path = "/home/ubuntu/eclipse with liferay/Desktop/imagetest/";

        File folder = new File(path);
        File[] listOfFiles = folder.listFiles();

        for (final File file : listOfFiles) {
                ImageView imageView;
                imageView = createImageView(file);
                tile.getChildren().addAll(imageView);
        }


        root.setHbarPolicy(ScrollPane.ScrollBarPolicy.NEVER); // Horizontal
        root.setVbarPolicy(ScrollPane.ScrollBarPolicy.AS_NEEDED); // Vertical scroll bar
        root.setFitToWidth(true);
        root.setContent(tile);

        primaryStage.setWidth(Screen.getPrimary().getVisualBounds().getWidth());
        primaryStage.setHeight(Screen.getPrimary().getVisualBounds()
                .getHeight());
        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        primaryStage.show();

    }

    private ImageView createImageView(final File imageFile) {
        // DEFAULT_THUMBNAIL_WIDTH is a constant you need to define
        // The last two arguments are: preserveRatio, and use smooth (slower)
        // resizing

        ImageView imageView = null;
        try {
            final Image image = new Image(new FileInputStream(imageFile), 150, 0, true,
                    true);
            imageView = new ImageView(image);
            imageView.setFitWidth(150);
            imageView.setOnMouseClicked(new EventHandler<MouseEvent>() {

                @Override
                public void handle(MouseEvent mouseEvent) {

                    if(mouseEvent.getButton().equals(MouseButton.PRIMARY)){

                        if(mouseEvent.getClickCount() == 2){
                            try {
                                BorderPane borderPane = new BorderPane();
                                ImageView imageView = new ImageView();
                                Image image = new Image(new FileInputStream(imageFile));
                                imageView.setImage(image);
                                imageView.setStyle("-fx-background-color: BLACK");
                                imageView.setFitHeight(stage.getHeight() - 10);
                                imageView.setPreserveRatio(true);
                                imageView.setSmooth(true);
                                imageView.setCache(true);
                                borderPane.setCenter(imageView);
                                borderPane.setStyle("-fx-background-color: BLACK");
                                Stage newStage = new Stage();
                                newStage.setWidth(stage.getWidth());
                                newStage.setHeight(stage.getHeight());
                                newStage.setTitle(imageFile.getName());
                                Scene scene = new Scene(borderPane,Color.BLACK);
                                newStage.setScene(scene);
                                newStage.show();
                            } catch (FileNotFoundException e) {
                                e.printStackTrace();
                            }

                        }
                    }
                }
            });
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        }
        return imageView;
    }

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

}

输出

enter image description here

这在您调整窗口大小时效果很好,如果需要,使 ScrollPane 可见。

关于java - 致力于在 JavaFX 中创建图片库。无法正常显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27182323/

相关文章:

JavaFX:如何禁用 TableView 中的行?

java - JAVAFX 骰子的最后值

JavaFX 警报按钮栏大小调整

Android:在 ImageView 中使用大图像时性能低下

java - 如何在java类中使用logback为特定类和方法实现不同的记录器模式?

java - GWT Place Class 增强可行性

java - 如何设置 SWT ScrollBar 上滚动条 slider 的最小尺寸?

Java Tic Tac Toe 错误

android - android :scaleType ="fitCenter" only work with fix Layout_width and Layout_height attributes?

java - 无法在 android :onClick attribute defined on view class android. widget.ImageView 的父级或祖先上下文中找到方法