当前位置: 首页 > 面试题库 >

如何将图像添加到ListView

闽鸿宝
2023-03-14
问题内容

最近8个小时我一直在阅读文档,但没有发现任何可以帮助我的东西。大概是,但是没有代码在工作,因为它一直说“找不到图像URL”并引发异常。但是我还有其他项目,从来没有这个问题。

因此,有一个类包含这样的月份:

public enum Month{JAN(1, "img.png",...DEC(12, "img.png");
private final int monthValue;
private final String imgName;

private Month(int monthValue, String imgName){
this.monthValue = monthValue;
this.imgName = imgName;
}

public int getMonth(){
return monthValue;
}

public String getImage(){
return imgName;} 
}

到目前为止,一切都很好。我什至可以在控制台中对其进行测试,并且效果很好,并且可以按值排序。现在,当我尝试从资源中添加图像时,出现了我之前提到的问题:找不到URL。但是,我只能使用图像的1个值来做一个ImageView,使用的路径来自“
C:\ … \ resources \ monthImg.png”,但我与其他人一起工作,每次我在线发送图像时,他都必须更改图像目录。需要很多时间。

现在,我一直在尝试获取12张图像并将它们设置为主项目类中的该枚举。这样,我便可以分配给节点并工作GUI。

我有两个名为“ main”的包(包含主类和月份类及其构造函数和方法),还有一个资源包,也称为“
main”,位于另一个文件夹中(包含所有图像)。请记住,如果我使用C:\的完整完整路径,它会起作用,但我也想在项目本身中启动它,然后将其发送给我的朋友。

这种方法是什么样的,因此我可以像上面链接中的示例一样,在VBox内的子孙值中使这些图像具有值堆栈。

注意:该项目旨在使这些图像看起来像是带有 拖放选项 的日历(我知道要这样做)。谢谢。


问题答案:

尝试逐步解决问题。
您可以从使列表视图显示所需图像开始。使用热链接图像使代码更加繁琐,并使帮助和测试变得简单而有效:

import java.util.stream.Stream;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Orientation;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.TilePane;
import javafx.stage.Stage;

public class FxMain extends Application {

    @Override
    public void start(Stage primaryStage) {

        MonthListCell[] listCell = Stream.of(Month.values()).map(MonthListCell::new).toArray(MonthListCell[]::new);
        ObservableList<MonthListCell> items =FXCollections.observableArrayList (listCell);
        ListView<MonthListCell> listView = new ListView<>(items);
        primaryStage.setScene(new Scene(listView));
        primaryStage.sizeToScene();
        primaryStage.show();
    }

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

enum Month{

    JAN(1,"https://cdn3.iconfinder.com/data/icons/softwaredemo/PNG/64x64/Circle_Green.png"),
    FEB(2,"https://cdn3.iconfinder.com/data/icons/softwaredemo/PNG/64x64/Circle_Red.png"),
    MAR(3,"https://cdn3.iconfinder.com/data/icons/softwaredemo/PNG/64x64/Circle_Yellow.png"),
    APR(4,"https://cdn3.iconfinder.com/data/icons/softwaredemo/PNG/64x64/Circle_Blue.png"),
    MAY(5,"https://cdn3.iconfinder.com/data/icons/softwaredemo/PNG/64x64/Circle_Orange.png"),
    JUN(6,"https://cdn3.iconfinder.com/data/icons/softwaredemo/PNG/64x64/Circle_Grey.png");

    private final int monthValue;
    private final String imgName;

    private Month(int monthValue, String imgName){
        this.monthValue = monthValue;
        this.imgName = imgName;
    }

    public int getMonth(){
        return monthValue;
    }

    public String getImage(){
        return imgName;
    }
}

class MonthListCell extends ListCell<Month> {

    private final ImageView imageView;
    private final Label text;

    MonthListCell(Month month) {
        Image image = new Image(month.getImage());
        imageView = new ImageView(image);
         //use label for text instead of setText() for better layout control 
        text = new Label(month.name());
        TilePane node = new TilePane(Orientation.VERTICAL, 5, 0, imageView, text);
        setGraphic(node);
    }

    @Override
    public void updateItem(Month month, boolean empty) {
        super.updateItem(month, empty);
        if (empty) {
            setText(null);
            setGraphic(null);
        } else {
            imageView.setImage(new Image(month.getImage()));
            text.setText(month.name());
        }
    }
}

接下来,尝试使用本地资源(图像)而不是链接的资源。



 类似资料:
  • 问题内容: 我有一个JPanel,我想向其中添加即时生成的JPEG和PNG图像。 到目前为止,我在Swing教程中看到的所有示例,特别是在Swing示例中,都使用ImageIcon。 我将这些图像生成为字节数组,它们通常比示例中使用的通用图标大,尺寸为640x480。 使用ImageIcon类在JPanel中显示该大小的图像时是否存在任何(性能或其他)问题? 什么是平常做的呢? 如何不使用Imag

  • 问题内容: 我正在尝试找到一种将图像添加到JavaFx TableView列的方法,该图像具有通过hibernate从H2数据库填充的其他列中的数据。TableView是在JavaFx Scene Builder中设计的。 到目前为止,这是我设法做到的: 控制器类: 我说那是一个错误。 这是我第一次尝试将图像添加到TableView中。 从昨天开始,我一直四处张望,但现在似乎被困住了。我希望能有所

  • 问题内容: 在我们的网站上,我们从各种来源上传了大量照片。 为了减小文件大小,我们使用mogrify从源中剥离所有exif数据: 我们想要做的是将一些基本的exif数据(如版权Initrode等)插回到这个新的“干净”图像上,但是我似乎在文档中找不到能实现此目的的任何东西。 有任何这样做的经验吗? 如果不能通过imagemagick完成,则基于PHP的解决方案将是下一件好事! 谢谢。 问题答案:

  • 问题内容: 我想在标题栏中添加图像(小图标)。 我该怎么做? 问题答案: 由于没有标题栏,因此我假设您是指。话虽这么说,使用。 这是使用的示例。

  • 问题内容: 我有一个基于项目数组创建的。当网格滚动到底部时,我需要添加更多图像,但是我不确定该怎么做。 现在我了解以下内容: 我有一个适配器,可以解析该数组并向该类提供ImageIds,该类将返回 我必须以某种方式更改此数组并让适配器知道它,所以我的问题是,如何获得对该适配器的引用? 这是我的代码: 现在有一些冗余代码,但这是我的测试项目。我所知道的是,适配器的更新必须在何时(即何时到达底部)进行

  • 我试图找到一种方法,将图像添加到JavaFx TableView列中,该列包含通过hibernate从H2数据库填充的其他列中的数据。TableView是在JavaFx场景生成器中设计的。 到目前为止,我一直在努力总结: 控制器类: 我得到一个错误在说. 这是我第一次尝试将图像添加到TableView中。 我从昨天开始四处看看,但现在似乎被卡住了。我希望得到一些帮助。我非常感谢你的帮助。 这是创建