今天就接着昨天把好友列表补全了一下,但是还是展示不出来,因为还要请求添加好友,对方同意申请等操作,还要判断对方在不在线,又是两种操作,有点麻烦,准备明天再写
public class ImageCellFactoryFriendList implements Callback<ListView<User>, ListCell<User>> {
public ImageCellFactoryFriendList() {
}
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;//获取用户头像
int online = listviewmember.online; // 获取用户在线状态
//显示头像
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);
// 设置用户名
setText(username);
// 设置在线状态的颜色
if (online==1) {
setTextFill(Color.GREEN); // 在线状态为绿色
setText(username + " (在线)");
} else {
setTextFill(Color.RED); // 不在线状态为红色
setText(username + " (离线)");
}
this.setPrefHeight(-1.0);
//设置右键菜单
ContextMenu contextMenu = new ContextMenu();
MenuItem option1 = new MenuItem("查看资料");
MenuItem option2 = new MenuItem("删除好友");
contextMenu.getItems().addAll(new MenuItem[]{option1,option2});
this.setContextMenu(contextMenu);
//查看资料
option1.setOnAction((event) -> {
System.out.println("查看资料按钮!!");
LookPersonalData.id = listviewmember.id;
LookPersonalData.user=listviewmember;
FriendPersonalData.user = listviewmember;//???
URL url = this.getClass().getResource("LookPersonalData.fxml");
if (url == null) {
System.err.println("无法找到LookPersonalData.fxml资源文件");
} else {
Parent root = null;
try {
root = (Parent)FXMLLoader.load(url);
} catch (IOException var7) {
IOException e = var7;
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();
}
});
//删除好友
option2.setOnAction((event) -> {
System.out.println("删除好友按钮!!");
try {
String id = listviewmember.id;
String friendid = listviewmember.friendid;
User u = new User(id, friendid);
String Operation = MarkTool.DeleteFriend;
Connection.oos.writeObject(Operation);
Connection.oos.writeObject(u);
String response = Connection.ois.readObject().toString();
System.out.println(response + "删除成功与否结果已收到");//103 yes
if (response.equals(MarkTool.DeleteFriendfail)) {
Alert alertxx = new Alert(Alert.AlertType.INFORMATION);
alertxx.setTitle("错误");
alertxx.setHeaderText((String)null);
alertxx.setContentText("删除失败,看样子他不想失去你呢!");
alertxx.showAndWait();
}else {
Alert alertx = new Alert(Alert.AlertType.INFORMATION);
alertx.setTitle("正确");
alertx.setHeaderText((String) null);
alertx.setContentText("删除成功,减少一位损友!");
alertx.showAndWait();
}
} catch (IOException var15) {
IOException exx = var15;
throw new RuntimeException(exx);
} catch (ClassNotFoundException var16) {
ClassNotFoundException ex = var16;
throw new RuntimeException(ex);
}
});
//设置鼠标点击事件,当右键点击时,显示上述创建的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);
}
}
};
}
}