如何使用fxml设计出和网页版微信样式类似的页面?请给出具体代码

时间: 2024-03-18 11:44:49 浏览: 19
设计一个和网页版微信样式类似的页面可以使用JavaFX中的FXML来实现。以下是一个简单的示例代码,用于演示如何使用FXML实现一个类似的页面: FXML文件(wechat.fxml): ``` <?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.control.Label?> <?import javafx.scene.control.ScrollPane?> <?import javafx.scene.control.SplitPane?> <?import javafx.scene.control.TextField?> <?import javafx.scene.image.ImageView?> <?import javafx.scene.layout.AnchorPane?> <?import javafx.scene.layout.BorderPane?> <?import javafx.scene.layout.HBox?> <?import javafx.scene.layout.VBox?> <BorderPane fx:id="root" prefHeight="600.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/11.0.2" xmlns:fx="http://javafx.com/fxml/1"> <top> <VBox prefHeight="60.0" prefWidth="800.0" style="-fx-background-color: #00bfff;"> <HBox spacing="10.0"> <ImageView fitHeight="40.0" fitWidth="40.0" pickOnBounds="true" preserveRatio="true"> <image> <Image url="@../images/avatar.jpg" /> </image> </ImageView> <Label text="John Smith" textFill="white" /> </HBox> </VBox> </top> <center> <SplitPane dividerPositions="0.3" prefHeight="540.0" prefWidth="800.0" style="-fx-border-color: #d9d9d9;"> <AnchorPane> <ScrollPane fitToWidth="true" prefHeight="540.0" prefWidth="240.0" vbarPolicy="ALWAYS"> <VBox prefHeight="540.0" prefWidth="240.0" spacing="10.0"> <HBox spacing="10.0"> <ImageView fitHeight="40.0" fitWidth="40.0" pickOnBounds="true" preserveRatio="true"> <image> <Image url="@../images/avatar.jpg" /> </image> </ImageView> <Label text="Group 1" /> </HBox> <HBox spacing="10.0"> <ImageView fitHeight="40.0" fitWidth="40.0" pickOnBounds="true" preserveRatio="true"> <image> <Image url="@../images/avatar.jpg" /> </image> </ImageView> <Label text="Group 2" /> </HBox> <HBox spacing="10.0"> <ImageView fitHeight="40.0" fitWidth="40.0" pickOnBounds="true" preserveRatio="true"> <image> <Image url="@../images/avatar.jpg" /> </image> </ImageView> <Label text="Group 3" /> </HBox> </VBox> </ScrollPane> </AnchorPane> <AnchorPane> <ScrollPane fitToWidth="true" prefHeight="540.0" prefWidth="560.0" vbarPolicy="ALWAYS"> <VBox prefHeight="540.0" prefWidth="560.0" spacing="10.0"> <HBox spacing="10.0"> <ImageView fitHeight="40.0" fitWidth="40.0" pickOnBounds="true" preserveRatio="true"> <image> <Image url="@../images/avatar.jpg" /> </image> </ImageView> <Label text="John Smith" /> <Label text="11:30 AM" /> </HBox> <HBox spacing="10.0"> <Label style="-fx-background-color: #d9d9d9; -fx-border-radius: 10px;" text="Hello World!" wrapText="true" /> </HBox> <HBox spacing="10.0"> <Label style="-fx-background-color: #00bfff; -fx-text-fill: white; -fx-border-radius: 10px;" text="Hi there!" wrapText="true" /> </HBox> </VBox> </ScrollPane> <HBox prefHeight="60.0" prefWidth="560.0" style="-fx-background-color: #f5f5f5;"> <ImageView fitHeight="40.0" fitWidth="40.0" pickOnBounds="true" preserveRatio="true"> <image> <Image url="@../images/avatar.jpg" /> </image> </ImageView> <TextField prefHeight="40.0" prefWidth="480.0" promptText="Type a message..." /> </HBox> </AnchorPane> </SplitPane> </center> </BorderPane> ``` Java文件(Main.java): ``` import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.stage.Stage; public class Main extends Application { @Override public void start(Stage primaryStage) throws Exception{ Parent root = FXMLLoader.load(getClass().getResource("wechat.fxml")); primaryStage.setTitle("WeChat"); primaryStage.setScene(new Scene(root, 800, 600)); primaryStage.show(); } public static void main(String[] args) { launch(args); } } ``` 在这个示例中,我们使用了BorderPane作为根节点,将页面分为了顶部和中心两个部分。顶部使用VBox和HBox布局来显示当前用户的头像和用户名。中心部分使用SplitPane将聊天列表和聊天记录分为了两个区域。聊天列表使用ScrollPane和VBox来显示所有的聊天组,聊天记录使用ScrollPane和VBox来显示所有的聊天消息,并使用HBox和Label来显示每条聊天消息的发送者、内容和时间。最后,我们使用HBox和TextField来实现聊天输入框。

相关推荐

最新推荐

recommend-type

uniapp 仿微信的右边下拉选择弹出框的实现代码

主要介绍了uniapp 仿微信的右边下拉选择弹出框的实现代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
recommend-type

vue+web端仿微信网页版聊天室功能

主要介绍了vue+web端仿微信网页版聊天室功能,非常不错,具有一定的参考借鉴价值 ,需要的朋友可以参考下
recommend-type

微信小程序左右滑动切换页面详解及实例代码

主要介绍了微信小程序左右滑动切换页面详解及实例代码的相关资料,需要的朋友可以参考下
recommend-type

JS中静态页面实现微信分享功能

小编使用ajax实现静态页面也能实现微信分享功能,今天小编给大家分享实现代码,对js静态页面微信分享功能感兴趣的朋友参考下本
recommend-type

Java实现微信网页授权的示例代码

主要介绍了Java实现微信网页授权的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB柱状图在信号处理中的应用:可视化信号特征和频谱分析

![matlab画柱状图](https://img-blog.csdnimg.cn/3f32348f1c9c4481a6f5931993732f97.png) # 1. MATLAB柱状图概述** MATLAB柱状图是一种图形化工具,用于可视化数据中不同类别或组的分布情况。它通过绘制垂直条形来表示每个类别或组中的数据值。柱状图在信号处理中广泛用于可视化信号特征和进行频谱分析。 柱状图的优点在于其简单易懂,能够直观地展示数据分布。在信号处理中,柱状图可以帮助工程师识别信号中的模式、趋势和异常情况,从而为信号分析和处理提供有价值的见解。 # 2. 柱状图在信号处理中的应用 柱状图在信号处理
recommend-type

hive中 的Metastore

Hive中的Metastore是一个关键的组件,它用于存储和管理Hive中的元数据。这些元数据包括表名、列名、表的数据类型、分区信息、表的存储位置等信息。Hive的查询和分析都需要Metastore来管理和访问这些元数据。 Metastore可以使用不同的后端存储来存储元数据,例如MySQL、PostgreSQL、Oracle等关系型数据库,或者Hadoop分布式文件系统中的HDFS。Metastore还提供了API,使得开发人员可以通过编程方式访问元数据。 Metastore的另一个重要功能是跟踪表的版本和历史。当用户对表进行更改时,Metastore会记录这些更改,并且可以让用户回滚到
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。