目录
1. 原始问题描述
模拟设计一个手机通讯录管理系统
(1) 显示功能:按名字的汉字首字母归类显示,并提供右侧字母导航条,当点击其中一个字母时,可 快速跳转到该字母分类的联系人列表。
(2) 新增功能:可以录入新联系人,包括:姓名、电话号码、电子邮件等。录入的新联系人能按照名字首字母自动进行归类。
(3) 修改功能:选中某联系人后可以进行修改。
(4) 删除功能:可以删除联系人,并自动调整显示。
(5) 查询功能:可以按名字、电话号码、电子邮件等进行模糊查询。
2. 实现效果
3.部分源码
每一个联系人的实体类
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import java.util.Arrays;
public class TelephoneBook implements Comparable<TelephoneBook> {
private int block;
private String name;
private String[] telephone;
private String mark;
private String email;
private TelephoneBook next;
public String getName() {
return name;
}
public TelephoneBook(String name, String[] telephone, String mark, String email) {
this.name = name;
this.telephone = telephone;
this.mark = mark;
this.email = email;
}
public void setName(String name) {
this.name = name;
}
public String[] getTelephone() {
return telephone;
}
public void setTelephone(String[] telephone) {
this.telephone = telephone;
}
public String getMark() {
return mark;
}
public void setMark(String mark) {
this.mark = mark;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getBlock() {
return getBookBlock();
}
public void setBlock(int block) {
this.block = block;
}
public TelephoneBook getNext() {
return next;
}
public void setNext(TelephoneBook next) {
this.next = next;
}
@Override
public String toString() {
return "TelephoneBook{" +
"name='" + name + '\'' +
", telephone=" + Arrays.toString(telephone) +
", mark='" + mark + '\'' +
", email='" + email + '\'' +
'}';
}
//计算获得联系人所在分块
public int getBookBlock() {
int block = 0;
for (int i = 0; i < 26; i++) {
if (TelephoneBook.getFirstSpell(getName().substring(0, 1)).compareTo(String.valueOf((char) ('a' + i))) == 0) {
block = i;
break;
}
}
return block;
}
//获取拼音首字母
public static String getFirstSpell(String chinese) {
StringBuffer pybf = new StringBuffer();
char[] arr = chinese.toCharArray();
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
//输出全部小写
defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
//不带音调
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
for (int i = 0; i < arr.length; i++) {
if (arr[i] > 128) { //不属于ASCII字符
try {
String[] temp = PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat);
if (temp != null) {
pybf.append(temp[0].charAt(0));
}
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
}
} else {
pybf.append(arr[i]);
}
}
//去除空白字符
return pybf.toString().replaceAll("\\W", "").trim();
}
@Override
public int compareTo(TelephoneBook o) {
if (getPingYin(this.getName()).compareTo(getPingYin(o.getName())) > 1) {
return 1;
} else if (getPingYin(this.getName()).compareTo(getPingYin(o.getName())) < 1) {
return -1;
} else {
return 0;
}
}
public static String getPingYin(String inputString) {
HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
format.setVCharType(HanyuPinyinVCharType.WITH_V);
char[] input = inputString.trim().toCharArray();
String output = "";
try {
for (int i = 0; i < input.length; i++) {
if (Character.toString(input[i]).matches("[\\u4E00-\\u9FA5]+")) {
String[] temp = PinyinHelper.toHanyuPinyinStringArray(input[i], format);
output += temp[0];
} else
output += Character.toString(input[i]);
}
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
}
return output;
}
}
详细信息页面
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.*;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import java.util.Optional;
public class DetailInformation extends BorderPane {
public DetailInformation(TelephoneBook telephoneBook, BorderPane borderPane, int index){
this.setTop(getBorderTop(telephoneBook.getName(),borderPane));
this.setCenter(getBorderBody(telephoneBook));
this.setBottom(getBorderBottom(borderPane,telephoneBook,index));
}
public static VBox getBorderTop(String name, BorderPane borderPane){
VBox vBox = new VBox();
HBox back = new HBox();
HBox hBox = new HBox();
hBox.setPrefHeight(150);
hBox.setAlignment(Pos.BOTTOM_LEFT);
back.setAlignment(Pos.CENTER_LEFT);
hBox.setPadding(new Insets(0,0,25,15));
back.setPadding(new Insets(25,0,25,15));
// 返回
Label backTo = new Label("返回");
Label label = new Label(name);
// 设置字体和背景
backTo.setFont(new Font(24));
label.setFont(new Font(24));
backTo.setTextFill(Color.WHITE);
label.setTextFill(Color.WHITE);
hBox.setStyle("-fx-background-color: #3D5A80");
back.setStyle("-fx-background-color: #3D5A80");
hBox.getChildren().add(label);
back.getChildren().add(backTo);
vBox.getChildren().addAll(back,hBox);
// 返回
backTo.setOnMouseClicked(event -> {
borderPane.getChildren().clear();
borderPane.setTop(TelephoneBookListPane.getBorderTop(borderPane));
ScrollPane scrollPane = TelephoneBookListPane.getBorderBody(borderPane);
borderPane.setCenter(scrollPane);
borderPane.setBottom(TelephoneBookListPane.getBorderBottom(borderPane));
borderPane.setLeft(TelephoneBookListPane.getSideBar(scrollPane));
});
return vBox;
}
//获取主体
public static VBox getBorderBody(TelephoneBook telephoneBook){
VBox vBox = new VBox();
String[] phones = telephoneBook.getTelephone();
if(phones.length == 1){
vBox.getChildren().add(getRecord("电话",telephoneBook.getTelephone()[0]));
}else{
for(int i = 0; i < phones.length; i++){
vBox.getChildren().add(getRecord("电话" + (i + 1),telephoneBook.getTelephone()[i]));
}
}
vBox.getChildren().addAll(getRecord("邮件",telephoneBook.getEmail()),getRecord("备注",telephoneBook.getMark()));
vBox.setPrefHeight(400);
return vBox;
}
//获取单条记录
public static HBox getRecord(String label, String mark){
HBox hBox = new HBox();
Label myLabel = new Label(label + ": ");
myLabel.setFont(new Font(20));
Text myMark = new Text(mark);
myMark.setFont(new Font(20));
hBox.getChildren().addAll(myLabel,myMark);
hBox.setPadding(new Insets(10,10,10,10));
hBox.setSpacing(10);
hBox.setStyle("-fx-border-width: 0 0 1 0;-fx-border-color: #e0e0e0");
return hBox;
}
//获取底部操作
public static HBox getBorderBottom(BorderPane borderPane,TelephoneBook telephoneBook,int index){
HBox hBox = new HBox();
Label edit = new Label("编辑联系人");
Label delete = new Label("删除联系人");
edit.setFont(new Font(18));
delete.setFont(new Font(18));
hBox.setPadding(new Insets(20,40,20,40));
hBox.setSpacing(100);
hBox.getChildren().addAll(edit,delete);
hBox.setStyle("-fx-background-color: #98C1D9");
// 编辑
edit.setOnMouseClicked(event -> {
borderPane.getChildren().clear();
TelephoneBook book = TelephoneBookListPane.getList().get(index);
borderPane.setCenter(new AddAndEditPane("编辑联系人",borderPane,book,2,index));
});
// 删除
delete.setOnMouseClicked(event -> {
Alert _alert = new Alert(Alert.AlertType.CONFIRMATION,"",new ButtonType("否", ButtonBar.ButtonData.NO),
new ButtonType("是", ButtonBar.ButtonData.YES));
//设置窗口的标题
_alert.setTitle("提示");
_alert.setHeaderText("您将删除该联系人,是否继续?");
//showAndWait() 将在对话框消失以前不会执行之后的代码
Optional<ButtonType> _buttonType = _alert.showAndWait();
//根据点击结果返回
if(_buttonType.get().getButtonData().equals(ButtonBar.ButtonData.YES)){
borderPane.getChildren().clear();
TelephoneBookListPane.getList().remove(index);
borderPane.setTop(TelephoneBookListPane.getBorderTop(borderPane));
ScrollPane scrollPane = TelephoneBookListPane.getBorderBody(borderPane);
borderPane.setCenter(scrollPane);
borderPane.setBottom(TelephoneBookListPane.getBorderBottom(borderPane));
borderPane.setLeft(TelephoneBookListPane.getSideBar(scrollPane));
}
});
return hBox;
}
}
4.参考博文
5.完整代码
博主已上传至优快云文件
未经允许不得转载,只能作学习用途!
未经允许不得转载,只能作学习用途!
未经允许不得转载,只能作学习用途!