我一直在寻找关于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);
}
}`