Java實(shí)現(xiàn)簡易界面通訊錄
前言
這個(gè)也是Java實(shí)驗(yàn)課程的一個(gè)作業(yè),和Java實(shí)現(xiàn)簡單的圖形界面計(jì)算器一起做的,因?yàn)橐郧皼]有做過GUI編程,所以做的非常簡陋,還有很多BUG,但是感覺當(dāng)個(gè)作業(yè)也夠了。
程序功能和截圖

這里的添加是直接添加到文件中,為什么不用數(shù)據(jù)庫呢?因?yàn)槲覀兝蠋煾揪蜎]教,所以也不能用.。

通過輸入的名字在文件中查找是否有該用戶,如果用,就顯示到界面上。

大致的功能就是上面兩個(gè)。
代碼
一、文件讀寫工具
package Contacts;
import java.io.*;
/**
?* Created by Yifan Jia on 2018/6/10.
?*/
public class FileRW {
? ? private static FileWriter fileWriter;
? ? private static FileReader fileReader;
? ? private static BufferedReader bf;
? ? private static BufferedWriter bw;
? ? private static File file = new File("D:\\dest.txt");
? ? public static void fileWrite(String s) {
? ? ? ? try {
? ? ? ? ? ? fileWriter = new FileWriter(file, true);
? ? ? ? ? ? bw = new BufferedWriter(fileWriter);
? ? ? ? ? ? bw.write(s);
? ? ? ? } catch (IOException e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? } finally {
? ? ? ? ? ? try {
? ? ? ? ? ? ? ? bw.close();
? ? ? ? ? ? ? ? fileWriter.close();
? ? ? ? ? ? } catch (IOException e) {
? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? public static String fileRead(String dest) {
? ? ? ? try {
? ? ? ? ? ? fileReader = new FileReader(file);
? ? ? ? ? ? bf = new BufferedReader(fileReader);
? ? ? ? ? ? String ss;
? ? ? ? ? ? while((ss = bf.readLine()) != null) {
? ? ? ? ? ? ? ? String[] temp = ss.split(",");
? ? ? ? ? ? ? ? if(temp[0].equals(dest)) {
? ? ? ? ? ? ? ? ? ? return ss;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? } catch (FileNotFoundException e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? } catch (IOException e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? } finally {
? ? ? ? ? ? try {
? ? ? ? ? ? ? ? bf.close();
? ? ? ? ? ? ? ? fileReader.close();
? ? ? ? ? ? } catch (IOException e) {
? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? return null;
? ? }
}二、界面程序
package Contacts;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
//定義自已的MyPanel,用于實(shí)現(xiàn)畫圖
class MyPanelone extends JPanel {
? ? private String ss;
? ? private int x;
? ? private int y;
? ? private int size;
? ? public MyPanelone(String ss, int x, int y, int size) {
? ? ? ? this.ss = ss;
? ? ? ? this.x = x;
? ? ? ? this.y = y;
? ? ? ? this.size = size;
? ? }
? ? //覆蓋JPanel的paint方法
? ? @Override
? ? public void paint(Graphics g) {
? ? ? ? super.paint(g);
? ? ? ? g.setColor(Color.BLACK);
? ? ? ? g.setFont(new Font("宋體", Font.BOLD, size));
? ? ? ? g.drawString(ss, x, y);
? ? }
}
public class MyContacts extends JFrame{
? ? private MyPanelone myPaneone;
? ? private JPanel[] jPanels = new JPanel[7];
? ? private JButton[] jButtons = new JButton[4];
? ? private JTextField[] jTextFields = new JTextField[6];
? ? private JLabel[] jLabels = new JLabel[6];
? ? private String[] texts = new String[6];
? ? private class MyActionListener implements ActionListener {
? ? ? ? @Override
? ? ? ? public void actionPerformed(ActionEvent e) {
? ? ? ? ? ? boolean flag = true;
? ? ? ? ? ? StringBuilder s = new StringBuilder();
? ? ? ? ? ? String actionCommand = e.getActionCommand();
? ? ? ? ? ? if(actionCommand == "添加") {
? ? ? ? ? ? ? ? for (int i = 0; i < 6; i++) {
? ? ? ? ? ? ? ? ? ? texts[i] = new String();
? ? ? ? ? ? ? ? ? ? texts[i] = jTextFields[i].getText();
? ? ? ? ? ? ? ? ? ? //System.out.println(texts[i]);
? ? ? ? ? ? ? ? ? ? if(texts[i].equals("") || texts[i] == null) {
? ? ? ? ? ? ? ? ? ? ? ? flag = false;
? ? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? if(i == 0) {
? ? ? ? ? ? ? ? ? ? ? ? s.append(texts[i]);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? else {
? ? ? ? ? ? ? ? ? ? ? ? s.append(",").append(texts[i]);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? if(flag) {
? ? ? ? ? ? ? ? ? ? s.append("\n");
? ? ? ? ? ? ? ? ? ? //將文本域中的內(nèi)容寫成一個(gè)字符串
? ? ? ? ? ? ? ? ? ? String ss = s.toString();
? ? ? ? ? ? ? ? ? ? //將字符串寫入文件
? ? ? ? ? ? ? ? ? ? FileRW.fileWrite(ss);
? ? ? ? ? ? ? ? ? ? for(int i=0;i<6;i++) {
? ? ? ? ? ? ? ? ? ? ? ? jTextFields[i].setText("");
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? //System.out.println(ss);
? ? ? ? ? ? ? ? ? ? JFrame jFrame = new JFrame();
? ? ? ? ? ? ? ? ? ? jFrame.setBounds(500, 300, 300, 300);
? ? ? ? ? ? ? ? ? ? MyPanelone myPanelone = new MyPanelone("添加成功", 100, 100, 20);
? ? ? ? ? ? ? ? ? ? jFrame.add(myPanelone);
? ? ? ? ? ? ? ? ? ? jFrame.addWindowListener(new WindowAdapter() {
? ? ? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? ? ? public void windowClosing(WindowEvent e) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? e.getWindow().dispose();
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? });
? ? ? ? ? ? ? ? ? ? jFrame.setVisible(true);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? else {
? ? ? ? ? ? ? ? ? ? JFrame jFrame = new JFrame();
? ? ? ? ? ? ? ? ? ? jFrame.setBounds(500, 300, 300, 300);
? ? ? ? ? ? ? ? ? ? MyPanelone myPanelone = new MyPanelone("請(qǐng)把所有內(nèi)容都填寫完整", 60, 100, 15);
? ? ? ? ? ? ? ? ? ? jFrame.add(myPanelone);
? ? ? ? ? ? ? ? ? ? jFrame.addWindowListener(new WindowAdapter() {
? ? ? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? ? ? public void windowClosing(WindowEvent e) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? e.getWindow().dispose();
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? });
? ? ? ? ? ? ? ? ? ? jFrame.setVisible(true);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? else if(actionCommand == "清空") {
? ? ? ? ? ? ? ? for(int i=0;i<6;i++) {
? ? ? ? ? ? ? ? ? ? jTextFields[i].setText("");
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? else if(actionCommand == "退出") {
? ? ? ? ? ? ? ? System.exit(0);
? ? ? ? ? ? }
? ? ? ? ? ? else if(actionCommand == "查找") {
? ? ? ? ? ? ? ? JFrame frame = new JFrame("輸入");
? ? ? ? ? ? ? ? JPanel jPanel = new JPanel();
? ? ? ? ? ? ? ? JPanel jPanel1 = new JPanel();
? ? ? ? ? ? ? ? JLabel jLabel = new JLabel("輸入查找人的名字");
? ? ? ? ? ? ? ? JButton jButton = new JButton("確定");
? ? ? ? ? ? ? ? JTextField jTextField = new JTextField(30);
? ? ? ? ? ? ? ? jPanel.add(jLabel);
? ? ? ? ? ? ? ? jPanel.add(jTextField);
? ? ? ? ? ? ? ? jButton.addActionListener(new ActionListener() {
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void actionPerformed(ActionEvent e) {
? ? ? ? ? ? ? ? ? ? ? ? String actionCommand1 = e.getActionCommand();
? ? ? ? ? ? ? ? ? ? ? ? String dest = jTextField.getText();
? ? ? ? ? ? ? ? ? ? ? ? String findresult = FileRW.fileRead(dest);
? ? ? ? ? ? ? ? ? ? ? ? if(findresult == null) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? for(int i=0;i<6;i++) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? jTextFields[i].setText("");
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? JFrame jFrame = new JFrame();
? ? ? ? ? ? ? ? ? ? ? ? ? ? jFrame.setBounds(500, 300, 300, 300);
? ? ? ? ? ? ? ? ? ? ? ? ? ? MyPanelone myPanelone = new MyPanelone("未找到該用戶", 100, 100, 20);
? ? ? ? ? ? ? ? ? ? ? ? ? ? jFrame.add(myPanelone);
? ? ? ? ? ? ? ? ? ? ? ? ? ? jFrame.addWindowListener(new WindowAdapter() {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? public void windowClosing(WindowEvent e) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? e.getWindow().dispose();
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? });
? ? ? ? ? ? ? ? ? ? ? ? ? ? jFrame.setVisible(true);
? ? ? ? ? ? ? ? ? ? ? ? ? ? frame.dispose();
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? else {
? ? ? ? ? ? ? ? ? ? ? ? ? ? String[] tempdest = findresult.split(",");
? ? ? ? ? ? ? ? ? ? ? ? ? ? for(int i=0;i<6;i++) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? jTextFields[i].setText(tempdest[i]);
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? frame.dispose();
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? });
? ? ? ? ? ? ? ? jPanel1.add(jButton);
? ? ? ? ? ? ? ? frame.add(jPanel, BorderLayout.CENTER);
? ? ? ? ? ? ? ? frame.add(jPanel1, BorderLayout.SOUTH);
? ? ? ? ? ? ? ? frame.setBounds(500, 300, 400, 300);
? ? ? ? ? ? ? ? frame.addWindowListener(new WindowAdapter() {
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void windowClosing(WindowEvent e) {
? ? ? ? ? ? ? ? ? ? ? ? e.getWindow().dispose();
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? });
? ? ? ? ? ? ? ? frame.setVisible(true);
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? MyContacts() {
? ? ? ? myPaneone = new MyPanelone("communication", 250, 60, 60);
? ? ? ? //myPaneone.setSize(1000, 150);
? ? ? ? this.add(myPaneone);
? ? ? ? for(int i=0;i<7;i++) {
? ? ? ? ? ? jPanels[i] = new JPanel();
? ? ? ? }
? ? ? ? jLabels[0] = new JLabel("姓名");
? ? ? ? jLabels[1] = new JLabel("郵政編碼");
? ? ? ? jLabels[2] = new JLabel("通信地址");
? ? ? ? jLabels[3] = new JLabel("電話");
? ? ? ? jLabels[4] = new JLabel("手機(jī)");
? ? ? ? jLabels[5] = new JLabel("電子郵件");
? ? ? ? jButtons[0] = new JButton("添加");
? ? ? ? jButtons[1] = new JButton("查找");
? ? ? ? jButtons[2] = new JButton("清空");
? ? ? ? jButtons[3] = new JButton("退出");
? ? ? ? for(int i=0;i<6;i++) {
? ? ? ? ? ? jTextFields[i] = new JTextField(50);
? ? ? ? }
? ? ? ? //設(shè)置布局管理
? ? ? ? this.setLayout(new GridLayout(8, 1));
? ? ? ? //加入各個(gè)組件
? ? ? ? for(int i=0;i<6;i++) {
? ? ? ? ? ? jPanels[i].add(jLabels[i]);
? ? ? ? ? ? jPanels[i].add(jTextFields[i]);
? ? ? ? ? ? this.add(jPanels[i]);
? ? ? ? }
? ? ? ? for(int i=0;i<4;i++) {
? ? ? ? ? ? jButtons[i].addActionListener(new MyActionListener());
? ? ? ? ? ? jPanels[6].add(jButtons[i]);
? ? ? ? }
? ? ? ? this.add(jPanels[6]);
? ? }
? ? public static void main(String[] args) {
? ? ? ? JFrame f = new MyContacts();
? ? ? ? f.setTitle(f.getClass().getSimpleName());
? ? ? ? f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
? ? ? ? f.setBounds(400, 200, 1000, 600);
? ? ? ? f.setVisible(true);
? ? }
}以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Java實(shí)現(xiàn)通訊錄管理系統(tǒng)項(xiàng)目
- Java實(shí)現(xiàn)簡單通訊錄管理系統(tǒng)
- Java實(shí)戰(zhàn)之用Swing實(shí)現(xiàn)通訊錄管理系統(tǒng)
- java實(shí)現(xiàn)通訊錄管理系統(tǒng)
- java實(shí)現(xiàn)簡單控制臺(tái)通訊錄
- java使用集合實(shí)現(xiàn)通訊錄功能
- Java實(shí)現(xiàn)XML文件學(xué)生通訊錄
- 簡單實(shí)現(xiàn)Java通訊錄系統(tǒng)
- java web個(gè)人通訊錄系統(tǒng)設(shè)計(jì)
- java微信企業(yè)號(hào)開發(fā)之通訊錄
相關(guān)文章
Spring Cloud 部署時(shí)使用 Kubernetes 作為注冊(cè)中心和配置中
Spring Cloud Kubernetes提供了使用Kubernete本地服務(wù)的Spring Cloud通用接口實(shí)現(xiàn),這篇文章主要介紹了Spring Cloud 部署時(shí)如何使用 Kubernetes 作為注冊(cè)中心和配置中心,需要的朋友可以參考下2024-05-05
Spring Cloud Alibaba Nacos Config配置中心實(shí)現(xiàn)
這篇文章主要介紹了Spring Cloud Alibaba Nacos Config配置中心實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04
Spring Cloud服務(wù)入口Gateway的介紹和使用問題小結(jié)
Spring Cloud Gateway是Spring Cloud的?個(gè)全新的API?關(guān)項(xiàng)?, 基于Spring + SpringBoot等技術(shù)開發(fā), ?的是為了替換掉Zuul,這篇文章主要介紹了Spring Cloud服務(wù)入口Gateway的介紹和使用問題小結(jié),需要的朋友可以參考下2025-03-03
Idea使用插件實(shí)現(xiàn)逆向工程搭建SpringBoot項(xiàng)目的圖文教程
這篇文章主要介紹了Idea使用插件實(shí)現(xiàn)逆向工程搭建SpringBoot項(xiàng)目,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06
SpringBoot-Admin實(shí)現(xiàn)微服務(wù)監(jiān)控+健康檢查+釘釘告警
本文主要介紹了SpringBoot-Admin實(shí)現(xiàn)微服務(wù)監(jiān)控+健康檢查+釘釘告警,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10
Java反射機(jī)制如何解決數(shù)據(jù)傳值為空的問題
這篇文章主要介紹了Java反射機(jī)制如何解決數(shù)據(jù)傳值為空的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
使用Spring Security和JWT實(shí)現(xiàn)安全認(rèn)證機(jī)制
在現(xiàn)代 Web 應(yīng)用中,安全認(rèn)證和授權(quán)是保障數(shù)據(jù)安全和用戶隱私的核心機(jī)制,Spring Security 是 Spring 框架下專為安全設(shè)計(jì)的模塊,具有高度的可配置性和擴(kuò)展性,而 JWT則是當(dāng)前流行的認(rèn)證解決方案,所以本文介紹了如何使用Spring Security和JWT實(shí)現(xiàn)安全認(rèn)證機(jī)制2024-11-11
微服務(wù)通過Feign調(diào)用進(jìn)行密碼安全認(rèn)證操作
這篇文章主要介紹了微服務(wù)通過Feign調(diào)用進(jìn)行密碼安全認(rèn)證操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06

