首页 > 编程语言 >仿微信聊天程序 - 06. 好友列表

仿微信聊天程序 - 06. 好友列表

时间:2023-07-16 13:44:17浏览次数:37  
标签:06 fx button list 列表 color background 仿微信

本文是仿微信聊天程序专栏的第六篇文章,主要记录了【好友列表】的界面实现。

界面设计

好友列表在主界面左边,界面UI风格跟聊天列表类似,只不过相比聊天列表更加简单,不需要显示聊天信息,时间等,总体界面设计如下:

界面布局

跟好友列表一样,聊天列表的布局也分为两部,列表布局和列表中的每一行的布局,其中列表布局的完整fxml如下:

<HBox prefHeight="640.0"
      prefWidth="250.0" xmlns:fx="http://javafx.com/fxml"
      stylesheets="@ContactsListController.css"
      fx:controller="michong.javafx.wx.view.contacts.ContactsListController">
    <children>
        <VBox prefWidth="250.0">
            <children>
                <HBox prefHeight="70.0" prefWidth="200.0" spacing="10.0">
                    <children>
                        <TextField prefHeight="30.0" HBox.hgrow="ALWAYS" promptText="好友搜索"/>
                        <Button prefHeight="30.0" prefWidth="30.0" text="+" onAction="#onApplyClick"/>
                    </children>
                    <padding>
                        <Insets left="10.0" right="10.0" top="20.0"/>
                    </padding>
                </HBox>
                <ListView fx:id="contactsListView" VBox.vgrow="ALWAYS"/>
            </children>
        </VBox>
    </children>
</HBox>

列表项的布局完整fxml如下:

<HBox stylesheets="@ContactsRowController.css" alignment="CENTER" prefHeight="60.0" prefWidth="235.0" spacing="5.0"
      fx:id="contactsRowHBox"
      xmlns:fx="http://javafx.com/fxml/">
    <children>
        <ImageView fx:id="avatarImageView" fitHeight="40.0" fitWidth="40.0"/>
        <VBox alignment="CENTER_LEFT" prefWidth="160.0" spacing="4.0" HBox.hgrow="ALWAYS">
            <children>
                <Label fx:id="nicknameLabel" styleClass="name-label"/>
            </children>
            <padding>
                <Insets bottom="5.0" left="5.0" right="5.0" top="5.0"/>
            </padding>
        </VBox>
    </children>
    <padding>
        <Insets left="5.0"/>
    </padding>
</HBox>

构建控件

从上面的界面布局中可以看到列表项并没有直接关联Controller,因为这里我实现动态构建,所以在初始化的时候绑定Controller,具体实现如下:

/**
 * @author michong
 */
public class ContactsRowController extends ListCell<ContactsVO> {

    public HBox contactsRowHBox;
    public ImageView avatarImageView;
    public Label nicknameLabel;

    private FXMLLoader loader;

    @Override
    protected void updateItem(ContactsVO item, boolean empty) {
        super.updateItem(item, empty);
        setText(null);

        if (empty || item == null) {
            setGraphic(null);
            return;
        }

        if (loader == null) {
            loader = new FXMLLoader(getClass().getResource("/" + getClass().getName().replace(".", "/") + ".fxml"));
            loader.setController(this);
            try {
                loader.load();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        avatarImageView.setImage(FXAvatar.load(item.getAvatar()));
        nicknameLabel.setText(item.getNickname());

        setGraphic(contactsRowHBox);
    }
}

样式美化

好友列表使用的是JavaFX的ListView控件,默认情况下,这个控件不是很美观,所以需要对其进行样式美化,这里的css跟好友列表的css是完全一样的,因为都是针对ListView的,完整的css如下:

* {
  -fx-font-family: "微软雅黑", "sans-serif";
  -fx-font-size: 14px;
  -fx-border-radius: 0;
  -fx-background-radius: 0;
}

.list-view {
  -fx-background-color: transparent;
}

.list-cell {
  -fx-background-color: #f4f4f4;
  -fx-text-fill: black;
}

.list-cell:empty {
  visibility: hidden;
}

.list-cell:hover {
  -fx-background-color: derive(#c8c8c8, 50%);
}

.list-cell:filled:selected:focused {
  -fx-background-color: #c8c8c8;
  -fx-text-fill: black;
}

.split-pane > .split-pane-divider {
  -fx-background-color: transparent;
}

.menu-button > .arrow-button {
  -fx-padding: 0;
}

.menu-button > .arrow-button > .arrow {
  -fx-padding: 0;
}

.track {
  -fx-background-color: transparent;
}

.thumb {
  -fx-pref-width: 10px;
  -fx-background-color: derive(#969696, 50%);
  -fx-background-radius: 0em;
}

.increment-arrow,
.decrement-arrow {
  -fx-padding: 0;
}

.increment-button,
.decrement-button {
  -fx-padding: 0 0 0 0;
}

.scroll-bar {
  -fx-opacity: 0;
}

:hover .scroll-bar {
  -fx-opacity: 1;
}

.list-view .scroll-bar,
.text-area .scroll-bar {
  -fx-background-color: transparent;
}

.list-view .decrement-button {
  -fx-padding: 0 10 0 0;
}

事件处理

重新实现第四篇【主界面】中的切换列表事件:

public void onContactsClick(ActionEvent actionEvent) {
    listVBox.getChildren().clear();
    listVBox.getChildren().add(FXComponent.contactsListController());
    contactsButton.setDisable(true);
    chatButton.setDisable(false);
}

缓存控件逻辑:

public static Parent contactsListController() {
    return cache.computeIfAbsent("contactsListController", k -> FX.fxml(ContactsListController.class));
}

标签:06,fx,button,list,列表,color,background,仿微信
From: https://www.cnblogs.com/michong2022/p/17557756.html

相关文章

  • 仿微信聊天程序 - 08. 聊天窗口
    本文是仿微信聊天程序专栏的第八篇文章,主要记录了【聊天窗口】的界面实现。界面设计聊天窗口是整个聊天程序的核心控件,比较复杂,大致可以分为上中下三个部分,上面显示用户昵称以及一些操作菜单,中间是聊天内容显示区域,下面的信息发送的区域,总体界面设计如下:界面布局根据界面设计......
  • 求一个不重复的列表
    #求不重复的1个列表importrandom#print(num)获取10以内的随机数#定义一个列表list1=[]#循环5次,count=0whilecount<5:#循环5次直到获得5个不重复的随机数num=random.randint(1,10)#判断随机数是否已存在,如果不存在放到目标列表中ifnum......
  • vue--day16---列表排序
    <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"/><metaname="viewport"content="width=device-width,initial-scale=1.0"/><title>watch与computed实现列表过滤</title>......
  • 使用列表并且 IDENTITY_INSERT 为 ON 时,才能为表
    原因:因为表中含有自增标识,无法直接为制定的序号做插入操作,需要更改标识(先开启,执行后SQL后,在关上)setidentity_insert  C_User_Registeron--设置标识列可以显示添加数据insertintoC_User_Register(PCId,PCUse,PCNote) values(100,1,'aaa')setidentity_insert C_User_Regist......
  • vue-day16---watch与computed实现列表过滤
    <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"/><metaname="viewport"content="width=device-width,initial-scale=1.0"/><title>watch与computed实现列表过滤</title>......
  • 【雕爷学编程】Arduino动手做(06)---KY-038声音传感器模块4
    37款传感器与执行器的提法,在网络上广泛流传,其实Arduino能够兼容的传感器模块肯定是不止这37种的。鉴于本人手头积累了一些传感器和执行器模块,依照实践出真知(一定要动手做)的理念,以学习和交流为目的,这里准备逐一动手尝试系列实验,不管成功(程序走通)与否,都会记录下来—小小的进步或是搞......
  • 【雕爷学编程】Arduino动手做(06)---KY-038声音传感器模块2
    37款传感器与执行器的提法,在网络上广泛流传,其实Arduino能够兼容的传感器模块肯定是不止这37种的。鉴于本人手头积累了一些传感器和执行器模块,依照实践出真知(一定要动手做)的理念,以学习和交流为目的,这里准备逐一动手尝试系列实验,不管成功(程序走通)与否,都会记录下来—小小的进步或是搞......
  • 106.nullptr和NULL
    106.nullptr和NULL1.NULL是什么在《NULL,0,'\0',"0","\0"的区别》一文中,我们已经知道了在C中NULL是什么,在C的头文件中,通常定义如下:#defineNULL((void*)0)但是在C++中,它是这样定义的:#defineNULL0可以在stddef.h看到完整的这段:#undefNULL#ifdefined(__cplusplus)#d......
  • Spartacus search box 里显示的产品列表数据是从哪里进行搜索的
    如下图所示,selector:cx-searchboxComponent名称:Search-box.component.ts点击searchbar之后:添加css类:在断点停下来的地方,查看搜索结果列表:抛出ProductSearch的action:最后调用ProductSearchConnector进行搜索:dispatch到adapter:ProductListComponent......
  • Angular 应用里产品列表行项目点击后跳转到产品明细页面的实现
    需求如标题所示,下面是详细步骤介绍。首先,你需要确保你的环境中已经安装了AngularCLI。如果没有,可以通过以下命令安装:npminstall-g@angular/cli然后你可以创建一个新的Angular项目:ngnewproduct-appcdproduct-app创建一个名为product的组件来显示产品列表:nggenerat......