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

這里的添加是直接添加到文件中,為什么不用數(shù)據(jù)庫呢?因為我們老師根本就沒教,所以也不能用.。

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

大致的功能就是上面兩個。
代碼
一、文件讀寫工具
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,用于實現(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");
? ? ? ? ? ? ? ? ? ? //將文本域中的內容寫成一個字符串
? ? ? ? ? ? ? ? ? ? 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("請把所有內容都填寫完整", 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("手機");
? ? ? ? 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);
? ? ? ? }
? ? ? ? //設置布局管理
? ? ? ? this.setLayout(new GridLayout(8, 1));
? ? ? ? //加入各個組件
? ? ? ? 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);
? ? }
}以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Spring Cloud 部署時使用 Kubernetes 作為注冊中心和配置中
Spring Cloud Kubernetes提供了使用Kubernete本地服務的Spring Cloud通用接口實現(xiàn),這篇文章主要介紹了Spring Cloud 部署時如何使用 Kubernetes 作為注冊中心和配置中心,需要的朋友可以參考下2024-05-05
Spring Cloud Alibaba Nacos Config配置中心實現(xiàn)
這篇文章主要介紹了Spring Cloud Alibaba Nacos Config配置中心實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-04-04
Spring Cloud服務入口Gateway的介紹和使用問題小結
Spring Cloud Gateway是Spring Cloud的?個全新的API?關項?, 基于Spring + SpringBoot等技術開發(fā), ?的是為了替換掉Zuul,這篇文章主要介紹了Spring Cloud服務入口Gateway的介紹和使用問題小結,需要的朋友可以參考下2025-03-03
Idea使用插件實現(xiàn)逆向工程搭建SpringBoot項目的圖文教程
這篇文章主要介紹了Idea使用插件實現(xiàn)逆向工程搭建SpringBoot項目,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-06-06
SpringBoot-Admin實現(xiàn)微服務監(jiān)控+健康檢查+釘釘告警
本文主要介紹了SpringBoot-Admin實現(xiàn)微服務監(jiān)控+健康檢查+釘釘告警,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-10-10
Java反射機制如何解決數(shù)據(jù)傳值為空的問題
這篇文章主要介紹了Java反射機制如何解決數(shù)據(jù)傳值為空的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
使用Spring Security和JWT實現(xiàn)安全認證機制
在現(xiàn)代 Web 應用中,安全認證和授權是保障數(shù)據(jù)安全和用戶隱私的核心機制,Spring Security 是 Spring 框架下專為安全設計的模塊,具有高度的可配置性和擴展性,而 JWT則是當前流行的認證解決方案,所以本文介紹了如何使用Spring Security和JWT實現(xiàn)安全認證機制2024-11-11

