用Java代码设计简单通讯录的设计与实现 设计要求: 1.用图形用户界面实现输入,界面美观操作方便。 2.系统功能的基本要求: (1)查看功能:选择此功能时,列出下列三类选择。 A 办公类;B个人类;C 商务类; 当选中某类时,显示出此类所有数据中的姓名和电话号码) (2)增加功能:能录入新数据(一个结点包括:姓名、电话号码、分类(可选项有:A办公类;B个人类;C商务类; 当录入了重复的姓名和电话号码时,则提示数据录入重复并取消录入;当通信录中超过15条信息时,存储空间已满,不能再录入新数据;录入的新数据能按速增的顺序自动进行条目编号。 (3)拔号功能:能显示出通信录中所有人的姓名,当选中某个姓名时,屏幕上模拟打字机的效果依次显示出此人的电话号码中的各个数字。 (4)修改功能:选中某个人的姓名时,可对此人的相应数据进行修改。 (5)删除功能:选中某个人的姓名时,可对此人的相应数据进行删除,并自动调整后续条目的编号。。

时间: 2024-03-17 07:45:04 浏览: 17
好的,这个问题我可以回答。以下是一个简单的通讯录的 Java 代码实现,可以满足你的要求: ```java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class AddressBook extends JFrame implements ActionListener { private JTextField nameField, phoneField; private JComboBox<String> categoryBox; private JTextArea addressBookArea; private JButton addButton, removeButton, modifyButton, dialButton; private static final int MAX_ENTRIES = 15; // 最大条目数 private int currentEntries = 0; // 当前条目数 // 通讯录条目 private class Entry { public String name; public String phone; public String category; public Entry(String name, String phone, String category) { this.name = name; this.phone = phone; this.category = category; } public String toString() { return String.format("%s\t%s\t%s\n", name, phone, category); } } // 存储通讯录数据的数组 private Entry[] entries = new Entry[MAX_ENTRIES]; public AddressBook() { super("通讯录"); // 创建输入框和下拉框 JPanel inputPanel = new JPanel(new GridLayout(2, 2)); inputPanel.add(new JLabel("姓名:")); nameField = new JTextField(); inputPanel.add(nameField); inputPanel.add(new JLabel("电话:")); phoneField = new JTextField(); inputPanel.add(phoneField); inputPanel.setBorder(BorderFactory.createTitledBorder("输入")); JPanel categoryPanel = new JPanel(new FlowLayout()); categoryPanel.add(new JLabel("分类:")); String[] categories = {"办公类", "个人类", "商务类"}; categoryBox = new JComboBox<String>(categories); categoryPanel.add(categoryBox); categoryPanel.setBorder(BorderFactory.createTitledBorder("分类")); // 创建按钮 addButton = new JButton("增加"); removeButton = new JButton("删除"); modifyButton = new JButton("修改"); dialButton = new JButton("拨号"); // 创建通讯录显示区域 addressBookArea = new JTextArea(10, 30); addressBookArea.setEditable(false); JScrollPane scrollPane = new JScrollPane(addressBookArea); // 创建按钮面板 JPanel buttonPanel = new JPanel(new GridLayout(2, 2)); buttonPanel.add(addButton); buttonPanel.add(removeButton); buttonPanel.add(modifyButton); buttonPanel.add(dialButton); // 将所有组件添加到窗口中 Container contentPane = getContentPane(); contentPane.setLayout(new BorderLayout()); contentPane.add(inputPanel, BorderLayout.NORTH); contentPane.add(categoryPanel, BorderLayout.CENTER); contentPane.add(scrollPane, BorderLayout.SOUTH); contentPane.add(buttonPanel, BorderLayout.EAST); // 添加事件监听器 addButton.addActionListener(this); removeButton.addActionListener(this); modifyButton.addActionListener(this); dialButton.addActionListener(this); // 设置窗口大小和可见性 setSize(500, 400); setVisible(true); } // 处理按钮点击事件 public void actionPerformed(ActionEvent e) { if (e.getSource() == addButton) { addEntry(); } else if (e.getSource() == removeButton) { removeEntry(); } else if (e.getSource() == modifyButton) { modifyEntry(); } else if (e.getSource() == dialButton) { dialEntry(); } } // 增加通讯录条目 private void addEntry() { String name = nameField.getText(); String phone = phoneField.getText(); String category = (String) categoryBox.getSelectedItem(); // 检查是否有重复的姓名和电话号码 for (int i = 0; i < currentEntries; i++) { if (entries[i].name.equals(name) && entries[i].phone.equals(phone)) { JOptionPane.showMessageDialog(this, "数据录入重复!", "错误", JOptionPane.ERROR_MESSAGE); return; } } // 检查通讯录是否已满 if (currentEntries >= MAX_ENTRIES) { JOptionPane.showMessageDialog(this, "存储空间已满,不能再录入新数据!", "错误", JOptionPane.ERROR_MESSAGE); return; } // 将新数据插入到正确的位置 int index = currentEntries; for (int i = 0; i < currentEntries; i++) { if (entries[i].name.compareTo(name) > 0) { index = i; break; } } for (int i = currentEntries; i > index; i--) { entries[i] = entries[i - 1]; } entries[index] = new Entry(name, phone, category); currentEntries++; // 更新通讯录显示区域 updateAddressBookArea(); } // 删除通讯录条目 private void removeEntry() { String name = JOptionPane.showInputDialog(this, "请输入要删除的姓名:"); if (name == null) { return; } // 查找要删除的条目 int index = -1; for (int i = 0; i < currentEntries; i++) { if (entries[i].name.equals(name)) { index = i; break; } } // 如果找到了,将后面的条目向前移动一个位置 if (index != -1) { for (int i = index + 1; i < currentEntries; i++) { entries[i - 1] = entries[i]; } currentEntries--; updateAddressBookArea(); } else { JOptionPane.showMessageDialog(this, "未找到指定的姓名!", "错误", JOptionPane.ERROR_MESSAGE); } } // 修改通讯录条目 private void modifyEntry() { String name = JOptionPane.showInputDialog(this, "请输入要修改的姓名:"); if (name == null) { return; } // 查找要修改的条目 int index = -1; for (int i = 0; i < currentEntries; i++) { if (entries[i].name.equals(name)) { index = i; break; } } // 如果找到了,修改条目的信息 if (index != -1) { String phone = JOptionPane.showInputDialog(this, "请输入新的电话号码:", entries[index].phone); String category = (String) JOptionPane.showInputDialog(this, "请选择新的分类:", "分类", JOptionPane.PLAIN_MESSAGE, null, new String[]{"办公类", "个人类", "商务类"}, entries[index].category); entries[index].phone = phone; entries[index].category = category; updateAddressBookArea(); } else { JOptionPane.showMessageDialog(this, "未找到指定的姓名!", "错误", JOptionPane.ERROR_MESSAGE); } } // 拨号 private void dialEntry() { StringBuilder sb = new StringBuilder(); for (int i = 0; i < currentEntries; i++) { sb.append(entries[i].name); sb.append("\n"); } String name = JOptionPane.showInputDialog(this, "请选择要拨号的姓名:\n" + sb.toString()); if (name == null) { return; } // 查找要拨号的条目 int index = -1; for (int i = 0; i < currentEntries; i++) { if (entries[i].name.equals(name)) { index = i; break; } } // 如果找到了,模拟拨号的效果 if (index != -1) { String phone = entries[index].phone; for (int i = 0; i < phone.length(); i++) { JOptionPane.showMessageDialog(this, phone.charAt(i), name, JOptionPane.PLAIN_MESSAGE); } } else { JOptionPane.showMessageDialog(this, "未找到指定的姓名!", "错误", JOptionPane.ERROR_MESSAGE); } } // 更新通讯录显示区域 private void updateAddressBookArea() { addressBookArea.setText(""); for (int i = 0; i < currentEntries; i++) { addressBookArea.append(String.format("%d\t%s", i + 1, entries[i].toString())); } } public static void main(String[] args) { new AddressBook(); } } ``` 这个程序使用了 Swing 库来创建图形用户界面,并使用了一个简单的数组来存储通讯录数据。功能实现方面,程序使用了多个方法来实现不同的功能,例如 `addEntry()` 方法用来增加通讯录条目,`removeEntry()` 方法用来删除条目,`modifyEntry()` 方法用来修改条目信息,`dialEntry()` 方法用来模拟拨号的效果,`updateAddressBookArea()` 方法用来更新通讯录显示区域。 希望这个程序能够满足你的要求。如果你有任何问题,可以随时问我。

相关推荐

最新推荐

recommend-type

基于Android手机通讯录的设计与实现_2018.doc

基于Android手机通讯录的设计与实现,基于Android手机通讯录的设计与实现
recommend-type

学生通讯录管理系统-数据结构课程设计.doc

学生通讯录管理系统-数据结构课程设计.doc 纸质通讯录已经不能满足大家的要求,容易丢失、查找困难等问题是纸质通讯录所有能克服的缺点。“学生通讯录管理系统”是为了帮助老师、同学等管理和分析的一种应用程序。
recommend-type

模拟通讯录-数据结构(顺序表实现基本功能).doc

模拟通讯录,设计相关数据结构,用菜单选择方式实现:通讯录的建立、通讯联系人的插入、删除、修改、查找等功能。
recommend-type

个人通讯录管理系统课程设计-C++.docx

计算机 电子信息工程 通信工程 实验 课程设计 工程项目 资源 必过 已过 好用 答辩简单 按着来就行 大学生关注我 以后所有我的课设都会更新 心得体会 参考文献自己写哈
recommend-type

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

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

list根据id查询pid 然后依次获取到所有的子节点数据

可以使用递归的方式来实现根据id查询pid并获取所有子节点数据。具体实现可以参考以下代码: ``` def get_children_nodes(nodes, parent_id): children = [] for node in nodes: if node['pid'] == parent_id: node['children'] = get_children_nodes(nodes, node['id']) children.append(node) return children # 测试数
recommend-type

JSBSim Reference Manual

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