栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何将图像添加到ListView

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何将图像添加到ListView

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

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());        }    }}

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



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/414331.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号