Java实现简易界面通讯录
作者:Jivan2233 时间:2023-02-09 20:12:59
前言
这个也是Java实验课程的一个作业,和Java实现简单的图形界面计算器一起做的,因为以前没有做过GUI编程,所以做的非常简陋,还有很多BUG,但是感觉当个作业也够了。
程序功能和截图
这里的添加是直接添加到文件中,为什么不用数据库呢?因为我们老师根本就没教,所以也不能用.。
通过输入的名字在文件中查找是否有该用户,如果用,就显示到界面上。
大致的功能就是上面两个。
代码
一、文件读写工具
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,用于实现画图
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);
}
}
来源:https://blog.csdn.net/q982151756/article/details/80647801
标签:java,通讯录
0
投稿
猜你喜欢
Spring Boot Thymeleaf实现国际化的方法详解
2023-11-24 06:01:01
Android APP存活检测方式
2023-10-14 10:02:41
新手初学Java数组
2023-12-01 11:18:40
SpringBoot实现简单文件上传功能
2021-07-22 21:52:49
关于Android 6.0权限的动态适配详解
2021-09-16 07:56:34
SpringBoot整合Shiro框架,实现用户权限管理
2021-10-27 00:03:02
Android GestureDetector手势滑动使用实例讲解
2022-01-18 01:34:12
android 动态控制状态栏显示和隐藏的方法实例
2023-07-08 10:11:43
Android开发中判断手机是否安装了QQ或者微信
2022-01-09 08:44:24
android实现文件下载功能
2022-07-29 18:03:10
Android中GridView和ArrayAdapter用法实例分析
2021-06-24 05:12:34
java 使用POI合并两个word文档
2022-09-30 12:22:54
java状态机方案解决订单状态扭转示例详解
2022-01-10 02:55:43
Android自定义view利用PathEffect实现动态效果
2023-08-17 09:35:00
Android 进程间通信实现原理分析
2022-01-05 10:27:35
ObjectMapper 如何忽略字段大小写
2023-07-31 07:07:18
Java 并发编程之线程挂起、恢复与终止
2022-02-18 11:51:51
C#中txt数据写入的几种常见方法
2022-01-14 01:25:47
详解Java并发编程之原子类
2022-05-28 01:36:06
Java实现添加文字水印&图片水印的方法详解
2023-01-28 08:11:39