Java 如何将图像添加到ListView

Java 如何将图像添加到ListView,java,image,listview,javafx,Java,Image,Listview,Javafx,在过去的8个小时里,我一直在阅读文件,没有发现任何对我有帮助的东西。模糊地说是的,但没有代码工作,因为它一直说“未找到图像url”,并抛出异常。然而,我有其他项目,从来没有这个问题 有一个类包含这样的月份: public enum Month{JAN(1, "img.png",...DEC(12, "img.png"); private final int monthValue; private final String imgName; private Month(int monthValu

在过去的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。但是,我可以使用“C:\…\resources\monthimm.png”中的路径,仅使用图像的1个值进行ImageView,但我与其他人一起工作,每次我在线发送图像时,他也必须更改图像目录。要花很多时间

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

我的方法是遵循这个思路:但是,它使用了setCellFactory,我非常确信,在没有这个方法和更少的代码行的情况下,您可以做到这一点

我有两个名为“main”的包(其中包含main类和month类及其构造函数和方法),还有一个资源包,也被称为“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());
        }
    }
}
import java.util.stream.stream;
导入javafx.application.application;
导入javafx.collections.FXCollections;
导入javafx.collections.ObservableList;
导入javafx.geometry.Orientation;
导入javafx.scene.scene;
导入javafx.scene.control.Label;
导入javafx.scene.control.ListCell;
导入javafx.scene.control.ListView;
导入javafx.scene.image.image;
导入javafx.scene.image.ImageView;
导入javafx.scene.layout.TilePane;
导入javafx.stage.stage;
公共类FxMain扩展了应用程序{
@凌驾
公共无效开始(阶段primaryStage){
MonthListCell[]listCell=Stream.of(Month.values()).map(MonthListCell::new).toArray(MonthListCell[]::new);
ObservableList items=FXCollections.observableArrayList(listCell);
ListView ListView=新的ListView(项目);
primaryStage.setScene(新场景(listView));
primaryStage.sizeToScene();
primaryStage.show();
}
公共静态void main(字符串[]args){
发射(空);
}
}
枚举月{
1月1日https://cdn3.iconfinder.com/data/icons/softwaredemo/PNG/64x64/Circle_Green.png"),
2月2日https://cdn3.iconfinder.com/data/icons/softwaredemo/PNG/64x64/Circle_Red.png"),
三月三日https://cdn3.iconfinder.com/data/icons/softwaredemo/PNG/64x64/Circle_Yellow.png"),
四月四日https://cdn3.iconfinder.com/data/icons/softwaredemo/PNG/64x64/Circle_Blue.png"),
五月五日https://cdn3.iconfinder.com/data/icons/softwaredemo/PNG/64x64/Circle_Orange.png"),
六月六日https://cdn3.iconfinder.com/data/icons/softwaredemo/PNG/64x64/Circle_Grey.png");
私人最终国际货币价值;
私有最终字符串imgName;
私人月份(int monthValue,字符串imgName){
this.monthValue=monthValue;
this.imgName=imgName;
}
公共整数getMonth(){
返回monthValue;
}
公共字符串getImage(){
返回imgName;
}
}
类MonthListCell扩展了ListCell{
私人最终影像视图;
专用最终标签文本;
月(月){
Image Image=新映像(month.getImage());
imageView=新的imageView(图像);
//使用文本标签而不是setText()以实现更好的布局控制
text=新标签(month.name());
TilePane节点=新的TilePane(Orientation.VERTICAL、5、0、imageView、text);
设置图形(节点);
}
@凌驾
public void updateItem(月,布尔值为空){
super.updateItem(月,空);
if(空){
setText(空);
设置图形(空);
}否则{
setImage(新图像(month.getImage());
text.setText(month.name());
}
}
}

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

尝试用一步一步的方法,以较小的部分解决问题。
您可以从让列表视图显示所需图像开始。使用热链接图像 使代码更加简单,并使帮助和测试变得简单高效:

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());
        }
    }
}
import java.util.stream.stream;
导入javafx.application.application;
导入javafx.collections.FXCollections;
导入javafx.collections.ObservableList;
导入javafx.geometry.Orientation;
导入javafx.scene.scene;
导入javafx.scene.control.Label;
导入javafx.scene.control.ListCell;
导入javafx.scene.control.ListView;
导入javafx.scene.image.image;
导入javafx.scene.image.ImageView;
导入javafx.scene.layout.TilePane;
导入javafx.stage.stage;
公共类FxMain扩展了应用程序{
@凌驾
公共无效开始(阶段primaryStage){
MonthListCell[]listCell=Stream.of(Month.values()).map(MonthListCell::new).toArray(MonthListCell[]::new);
ObservableList items=FXCollections.observableArrayList(listCell);
ListView ListView=新的ListView(项目);
primaryStage.setScene(新场景(listView));
primaryStage.sizeToScene();
primaryStage.show();