gpt4 book ai didi

JavaFX : How to put ImageView inside ListView

转载 作者:搜寻专家 更新时间:2023-11-01 01:16:13 29 4
gpt4 key购买 nike

我一直在搜索有关 JavaFx ListView 的一些线程,但仍然很难找到一个简单而容易的解决方案来将 JavaFx ImageView 放入 JavaFx ListView 中。我正在尝试使用 JavaFx 框架创建聊天应用程序。在照片上它应该看起来像那样。这不是重复的,因为另一个线程对于像我这样的初学者来说是模糊的。 ListView with imageview inside

我试过这样的事情。我对此完全不熟悉,在此先感谢!

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Main extends Application {
ImageView imageView = new ImageView();
Image image = new Image(Main.class.getResourceAsStream("bell.jpg"));
ListView<String> list = new ListView<String>();
ObservableList<String> data = FXCollections.observableArrayList(
"chocolate", "salmon", "gold", "coral", "darkorchid",
"darkgoldenrod", "lightsalmon", "black", "rosybrown", "blue",
"blueviolet", "brown");
Image pic;

@Override
public void start(Stage stage) {
VBox box = new VBox();
Scene scene = new Scene(box, 200, 200);
stage.setScene(scene);
stage.setTitle("ListViewSample");
box.getChildren().addAll(list);
VBox.setVgrow(list, Priority.ALWAYS);
list.setItems(data);

list.setCellFactory(listView -> new ListCell<String>() {
public void updateItem(String friend, boolean empty) {
super.updateItem(friend, empty);
if (empty) {
setText(null);
setGraphic(null);
} else {

imageView.setImage(image);
setText(friend);
setGraphic(imageView);
}
}

});

stage.show();
}


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

最佳答案

您不需要每次在 ListView 中设置图像时都创建一个新的 HBox。您可以直接使用 ListView 的 setCellFactory() 来实现相同的结果。

这个例子只是答案 Image in JavaFX ListView 的延伸并包含一个 Minimal, Complete, and Verifiable example可以尝试和测试。 如果您想了解这是如何工作的,我建议您先阅读链接的问题。

更新 - 如何调整图片大小

要调整图像大小以适合您想要的大小,请使用 fitWidth()fitHeight() ImageView 的方法。还有其他方法,如 setSmooth()setPreserveRatio()这也可以派上用场。

MCVE

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class ListViewWithImages extends Application {

private final Image IMAGE_RUBY = new Image("https://upload.wikimedia.org/wikipedia/commons/f/f1/Ruby_logo_64x64.png");
private final Image IMAGE_APPLE = new Image("http://findicons.com/files/icons/832/social_and_web/64/apple.png");
private final Image IMAGE_VISTA = new Image("http://antaki.ca/bloom/img/windows_64x64.png");
private final Image IMAGE_TWITTER = new Image("http://files.softicons.com/download/social-media-icons/fresh-social-media-icons-by-creative-nerds/png/64x64/twitter-bird.png");

private Image[] listOfImages = {IMAGE_RUBY, IMAGE_APPLE, IMAGE_VISTA, IMAGE_TWITTER};

@Override
public void start(Stage primaryStage) throws Exception {

ListView<String> listView = new ListView<String>();
ObservableList<String> items =FXCollections.observableArrayList (
"RUBY", "APPLE", "VISTA", "TWITTER");
listView.setItems(items);

listView.setCellFactory(param -> new ListCell<String>() {
private ImageView imageView = new ImageView();
@Override
public void updateItem(String name, boolean empty) {
super.updateItem(name, empty);
if (empty) {
setText(null);
setGraphic(null);
} else {
if(name.equals("RUBY"))
imageView.setImage(listOfImages[0]);
else if(name.equals("APPLE"))
imageView.setImage(listOfImages[1]);
else if(name.equals("VISTA"))
imageView.setImage(listOfImages[2]);
else if(name.equals("TWITTER"))
imageView.setImage(listOfImages[3]);
setText(name);
setGraphic(imageView);
}
}
});
VBox box = new VBox(listView);
box.setAlignment(Pos.CENTER);
Scene scene = new Scene(box, 200, 200);
primaryStage.setScene(scene);
primaryStage.show();
}

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

输出

enter image description here

关于JavaFX : How to put ImageView inside ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33592308/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com