聊天室项目的进展

目录

1.将数据库中的信息展示在界面上。

2.使用javafx中的ListViem,命名为friendlistveiw,User为包装好的类。

3.解释创建的类ImageCellFactoryAddFriend()

类定义和构造函数

call方法实现

更新单元格内容

设置右键菜单

设置鼠标点击事件

处理空单元格情况

效果如下:


1.将数据库中的信息展示在界面上。

    @FXML//通过上一界面获取用户数据
    void initialize() throws IOException, ClassNotFoundException {
        System.out.println("正在获取输入账号的信息");
        String Id = this.id;//
        System.out.println(user);
        System.err.println(user.id);
        System.err.println(user.name);
        this.name.setText(user.name);
        this.signature.setText(user.signature);
        this.birthday.setText(user.birthday.toString());
        this.sex.setText(user.sex);
        File imageFile = new File(user.image);
        Image imagess = new Image(imageFile.toURI().toString());
        this.image.setImage(imagess);
    }

将AddFriends类的user传递给FrindPersonalData(上面的)。第59,60行

这里我没设置性别和签名,所有默认为空,头像则默认为企鹅,这里我设置了头像。

2.使用javafx中的ListViem<User>,命名为friendlistveiw,User为包装好的类。

ImageCellFactoryAddFriend()为一个类。待会会有解释

 this.friendlistveiw.setItems(this.friendList);
                this.friendList.add(user);
                this.friendlistveiw.setCellFactory(new ImageCellFactoryAddFriend());

friendlistveiw为javafx使用ListView的命名,还需要用到如下的最后一行,即可完成。

 @FXML
    private ListView<User> friendlistveiw;
    public static String ids;
    public static User user = null;//user类需要的所有
    ObservableList<User> friendList = FXCollections.observableArrayList();

3.解释创建的类ImageCellFactoryAddFriend()

全部代码在最下方,现在来逐一分析

类定义和构造函数
public class ImageCellFactoryAddFriend implements Callback<ListView<User>, ListCell<User>> {
    public ImageCellFactoryAddFriend() {
    }
  • ImageCellFactoryAddFriend类实现了Callback<ListView<User>, ListCell<User>>接口,用于为ListView<User>提供ListCell<User>实例。
  • 构造函数为空,没有额外的初始化逻辑。

call方法实现
public ListCell<User> call(ListView<User> param) {
    return new ListCell<User>() {
        private ImageView imageView = new ImageView();

        protected void updateItem(User listviewmember, boolean empty) {
            super.updateItem(listviewmember, empty);
            if (!empty && listviewmember != null) {
  • call方法是Callback接口的方法,它返回一个ListCell<User>对象。在这里,创建了一个匿名内部类继承自ListCell<User>
  • updateItem方法中,根据单元格的内容是否为空来设置单元格的显示内容。
更新单元格内容
                String username = listviewmember.name;
                String imagePath = listviewmember.image;
                File imageFile = new File(imagePath);
                Image images = new Image(imageFile.toURI().toString());
                this.imageView.setFitWidth(50.0);
                this.imageView.setFitHeight(50.0);
                this.imageView.setImage(images);
                this.setGraphic(this.imageView);
                this.setText(username);
                this.setPrefHeight(-1.0);
  • 如果单元格非空且listviewmember不为null,从User对象中获取用户名和图像路径。
  • 创建一个Image对象并设置到ImageView中,然后设置ImageView为单元格的图形内容。
  • 设置用户名为单元格的文本内容,设置单元格的高度为默认值。

设置右键菜单
                ContextMenu contextMenu = new ContextMenu();
                MenuItem option1 = new MenuItem("查看资料");
                contextMenu.getItems().addAll(new MenuItem[]{option1});
                this.setContextMenu(contextMenu);

                option1.setOnAction((event) -> {
                    URL url = this.getClass().getResource("FriendPersonalData.fxml");
                    if (url == null) {
                        System.err.println("无法找到FriendPersonalData.fxml文件");
                    } else {
                        Parent root = null;
                        try {
                            root = FXMLLoader.load(url);
                        } catch (IOException e) {
                            System.err.println("加载FXML文件时出错:" + e.getMessage());
                            e.printStackTrace();
                        }
                        Stage stage = new Stage();
                        stage.setTitle("个人界面");
                        stage.initStyle(StageStyle.UTILITY);
                        Scene scene = new Scene(root, 800.0, 640.0);
                        stage.setScene(scene);
                        stage.show();
                    }
                });
  • 创建ContextMenu和一个菜单项option1,并添加到ContextMenu中。
  • 设置单元格的右键菜单为刚创建的ContextMenu
  • 当用户点击菜单项"查看资料"时,加载名为FriendPersonalData.fxml的FXML文件,并在新的舞台(Stage)中显示。

设置鼠标点击事件
                this.setOnMouseClicked((event) -> {
                    if (event.getButton() == MouseButton.SECONDARY) {
                        contextMenu.show(this, event.getScreenX(), event.getScreenY());
                    }
                });
  • 设置单元格的鼠标点击事件,当右键点击时,显示上述创建的ContextMenu,位置在鼠标点击的位置。

处理空单元格情况
            } else {
                this.setText((String)null);
                this.setGraphic((Node)null);
                this.setPrefHeight(0.0);
            }
  • 如果单元格为空或listviewmember为null,清除单元格的文本内容和图形内容,并将单元格高度设置为0,使其不可见。

总结:以上代码的主要功能是

ListView<User>中的每个User对象创建一个带有用户头像和右键菜单的自定义单元格。它通过Callback接口实现了call方法来生成ListCell<User>,并使用updateItem方法来更新单元格的显示内容和行为。右键菜单支持“查看资料”功能,点击后加载一个FXML文件并在新的窗口中显示用户的个人信息界面。

效果如下:

像一个集合一样,可以滚轮向下移动

完整代码如下:User为我包装的类

import Talk.User;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.control.MenuItem;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseButton;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.util.Callback;


public class ImageCellFactoryAddFriend implements Callback<ListView<User>, ListCell<User>> {
    public ImageCellFactoryAddFriend() {
    }

    public ListCell<User> call(ListView<User> param) {
        return new ListCell<User>() {
            private ImageView imageView = new ImageView();
            protected void updateItem(User listviewmember, boolean empty) {
                super.updateItem(listviewmember, empty);
                if (!empty && listviewmember != null) {
                    //更新单元格内容
                    String username = listviewmember.name;
                    String imagePath = listviewmember.image;
                    File imageFile = new File(imagePath);
                    Image images = new Image(imageFile.toURI().toString());
                    this.imageView.setFitWidth(50.0);
                    this.imageView.setFitHeight(50.0);
                    this.imageView.setImage(images);
                    this.setGraphic(this.imageView);
                    this.setText(username);
                    this.setPrefHeight(-1.0);
                    //设置右键菜单
                    ContextMenu contextMenu = new ContextMenu();
                    MenuItem option1 = new MenuItem("查看资料");
                    contextMenu.getItems().addAll(new MenuItem[]{option1});
                    this.setContextMenu(contextMenu);
                    option1.setOnAction((event) -> {
                        URL url = this.getClass().getResource("FriendPersonalData.fxml");
                        if (url == null) {
                            url = this.getClass().getResource("E:/java.xmu/demo/src/main/resources/org/example/demo/FriendPersonalData.fxml");
                            System.err.println("无法找到FriendPersonalData.fxml文件");
                        } else {
                            Parent root = null;
                            try {
                                root = (Parent) FXMLLoader.load(url);
                            } catch (IOException var6) {
                                System.out.println("var6有问题");
                                IOException e = var6;
                                e.printStackTrace();
                                return;
                            }
                            Stage stage = new Stage();
                            stage.setTitle("个人界面");
                            stage.initStyle(StageStyle.UTILITY);
                            Scene scene = new Scene(root, 800.0, 640.0);
                            stage.setScene(scene);
                            stage.show();
                        }
                    });
                    //设置鼠标点击事件,当右键点击时,显示上述创建的ContextMenu
                    this.setOnMouseClicked((event) -> {
                        if (event.getButton() == MouseButton.SECONDARY) {
                            contextMenu.show(this, event.getScreenX(), event.getScreenY());
                        }
                    });
                } else {
                    this.setText((String)null);
                    this.setGraphic((Node)null);
                    this.setPrefHeight(0.0);
                }

            }
        };
    }
}

  • 23
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值