package Dome;
import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.*;
import java.security.Key;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Load {
//定义成员变量
JFrame frame;
JTextField jt1;
JTextField jt2;
JTextField jt3;
JTextField jt4;
JButton j1;
JButton j2;
String s1;
String s2;
String s3;
String s4;
File file;
List<People> list;
public Load() {
//定义写入文件夹的目录
file = new File("src\\Demo\\TXL.txt");
list = new ArrayList<>();
openFile();
//创建初始窗口
frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setBounds(800, 300, 600, 600);
JMenuBar menuBar = new JMenuBar();
frame.addWindowListener(new myWindow());
//添加按钮
JMenu menu = new JMenu("文件");
menuBar.add(menu);
//添加功能按钮,并加入到窗口中
JMenuItem item = new JMenuItem("新建");
JMenuItem item1 = new JMenuItem("删除");
JMenuItem item2 = new JMenuItem("修改");
JMenuItem item3 = new JMenuItem("查找");
menu.add(item);
menu.add(item1);
menu.add(item2);
menu.add(item3);
frame.setJMenuBar(menuBar);
//为按钮创建监听器
item.addActionListener(e -> {
panle1();
});
item1.addActionListener(e -> {
panle2();
});
item2.addActionListener(e -> {
panel3();
});
item3.addActionListener(e -> {
panel4();
});
frame.setVisible(true);
}
//添加新建的方法
public void panle1() {
JDialog dialog = new JDialog(frame, "新建", true);
dialog.setBounds(800, 300, 600, 400);
dialog.setLayout(null);
//设置窗口
//添加姓名,信箱,电话,地址等单行文本输入框
JLabel l1 = new JLabel("姓名:");
l1.setFont(new Font("微软雅黑", Font.BOLD, 15));
l1.setBounds(130, 60, 50, 30);
dialog.add(l1);
jt1 = new JTextField(24);
jt1.setBounds(200, 65, 200, 24);
jt1.addActionListener(e -> {
s1 = jt1.getText();
});
JLabel l2 = new JLabel("信箱:");
l2.setFont(new Font("微软雅黑", Font.BOLD, 15));
l2.setBounds(130, 100, 50, 24);
dialog.add(l2);
jt2 = new JTextField(15);
jt2.setBounds(200, 105, 200, 24);
jt2.addActionListener(e -> {
s2 = jt2.getText();
});
JLabel l3 = new JLabel("电话:");
l3.setFont(new Font("微软雅黑", Font.BOLD, 15));
l3.setBounds(130, 140, 50, 30);
dialog.add(l3);
jt3 = new JTextField(15);
jt3.setBounds(200, 145, 200, 24);
jt3.addActionListener(e -> {
s3 = jt3.getText();
});
JLabel l4 = new JLabel("地址:");
l4.setFont(new Font("微软雅黑", Font.BOLD, 15));
l4.setBounds(130, 180, 50, 30);
dialog.add(l4);
jt4 = new JTextField(15);
jt4.setBounds(200, 185, 200, 24);
jt4.addActionListener(e -> {
s4 = jt4.getText();
});
//添加按钮并且添加监听器
j1 = new JButton("添加");
j1.setBounds(320, 250, 80, 30);
j1.addActionListener(e -> {
add();
});
//将四个当行文本框添加至窗口
dialog.add(jt1);
dialog.add(jt2);
dialog.add(jt3);
dialog.add(jt4);
dialog.add(j1);
dialog.setVisible(true);
}
public void add() {//判断输入的信息是否重复
for (People people : list) {
if (people.getName().equals(s1)) {
JOptionPane.showMessageDialog(frame, "姓名重复");
return;
}
}
People p = new People();//将输入的信息转化并写入文件
p.setName(s1);
p.setEmil(s2);
p.setNO(s3);
p.setAdress(s4);
list.add(p);
JOptionPane.showMessageDialog(frame, "添加成功");
jt1.setText("");
jt2.setText("");
jt3.setText("");
jt4.setText("");
return;
}
//添加删除功能
public void panle2() {//添加窗口
JDialog dialog = new JDialog(frame, "删除", true);
dialog.setBounds(800, 300, 600, 400);
dialog.setLayout(null);
//设置字体字号
JLabel l1 = new JLabel("姓名:");
l1.setFont(new Font("微软雅黑", Font.BOLD, 15));
l1.setBounds(130, 60, 50, 30);
dialog.add(l1);
jt1 = new JTextField(24);
jt1.setBounds(200, 65, 200, 24);
jt1.addActionListener(e -> {
s1 = jt1.getText();//文本转化
});
//添加删除按钮
j1 = new JButton("删除");
j1.setBounds(320, 250, 80, 30);
//设置监听器
j1.addActionListener(e -> {
delete();
});
dialog.add(jt1);
dialog.add(j1);
dialog.setVisible(true);
}
//将输入的信息与文件中的想照应,进行删除
public void delete() {
int a = 0;
Iterator<People> it = list.iterator();
while (it.hasNext()) {
People people = (People) it.next();
if (people.getName().equals(s1)) {
it.remove();
JOptionPane.showMessageDialog(frame, "DELECT");
a = 1;
break;
}
}
//判断是否存在此人
if (a == 0) {
JOptionPane.showMessageDialog(frame, "查无此人");
}
}
//创建修改功能
public void panel3() {
Dialog dialog = new JDialog(frame, "修改", true);
dialog.setBounds(800, 300, 600, 400);
dialog.setLayout(null);
JLabel l1 = new JLabel("姓名:");
l1.setFont(new Font("微软雅黑", Font.BOLD, 15));
l1.setBounds(130, 60, 50, 30);
dialog.add(l1);
jt1 = new JTextField(24);
jt1.setBounds(200, 65, 200, 24);
jt1.addActionListener(e -> {
s1 = jt1.getText();//转化输入的信息
});
JLabel l2 = new JLabel("信箱:");
l2.setFont(new Font("微软雅黑", Font.BOLD, 15));
l2.setBounds(130, 100, 50, 24);
dialog.add(l2);
jt2 = new JTextField(15);
jt2.setBounds(200, 105, 200, 24);
jt2.addActionListener(e -> {
s2 = jt2.getText();//转化输入的信息
});
JLabel l3 = new JLabel("电话:");
l3.setFont(new Font("微软雅黑", Font.BOLD, 15));
l3.setBounds(130, 140, 50, 30);
dialog.add(l3);
jt3 = new JTextField(15);
jt3.setBounds(200, 145, 200, 24);
jt3.addActionListener(e -> {
s3 = jt3.getText();//转化输入的信息
});
JLabel l4 = new JLabel("地址:");
l4.setFont(new Font("微软雅黑", Font.BOLD, 15));
l4.setBounds(130, 180, 50, 30);
dialog.add(l4);
jt4 = new JTextField(15);
jt4.setBounds(200, 185, 200, 24);
jt4.addActionListener(e -> {
s4 = jt4.getText();//转化输入的信息
});
//添加修改的按钮,并设置监听器
j1 = new JButton("修改");
j1.setBounds(320, 250, 80, 30);
j1.addActionListener(e -> {
set();
});
dialog.add(jt1);
dialog.add(jt2);
dialog.add(jt3);
dialog.add(jt4);
dialog.add(j1);
dialog.setVisible(true);
}
//将修改的信息写入文件中
public void set() {
int a = 0;
for (People p : list) {
if (p.getName().equals(s1)) {
p.setName(s1);
p.setEmil(s2);
p.setNO(s3);
p.setAdress(s4);
JOptionPane.showMessageDialog(frame, "修改成功");
a = 1;
jt1.setText("");
jt2.setText("");
jt3.setText("");
jt4.setText("");
break;
}
}//判断信息输入在文件中能否被找到
if (a == 0) {
JOptionPane.showMessageDialog(frame, "查无此人");
}
}
//添加查找信息的功能
public void panel4() {
JDialog dialog = new JDialog(frame, "查找");
dialog.setBounds(800, 300, 600, 400);
dialog.setLayout(null);
//添加单行文本输入框
JLabel l1 = new JLabel("姓名:");
l1.setFont(new Font("微软雅黑", Font.BOLD, 15));
l1.setBounds(130, 60, 50, 30);
dialog.add(l1);
jt1 = new JTextField(24);
jt1.setBounds(200, 65, 200, 24);
jt1.addActionListener(e -> {
s1 = jt1.getText();//将输入的信息进行转化
});
//添加查找按钮
j1 = new JButton("查找");
j1.setBounds(320, 250, 80, 30);
j1.addActionListener(e -> {
search();
});
//设置显示全部信息的按钮,并添加监听器
j2 = new JButton("全部");
j2.setBounds(200, 250, 80, 30);
j2.addActionListener(e->{
search2();
});
dialog.add(jt1);
dialog.add(j1);
dialog.add(j2);
dialog.setVisible(true);
}
//文本框信息显示
public void search() {
int a = 0;
JTextArea area = null;
if (list.size() != 0){
area = panel("姓名\t\t邮箱\t\t电话\t\t地址\n");
}
for (People p : list) {
if (p.getName().equals(s1)) {
area.append(p.toString() + "\n");
a = 1;
break;
}
}//判断联系人是否存在
if (a == 0) {
JOptionPane.showMessageDialog(frame, "查无此人");
}
}
public void search2(){
int a = 0;
JTextArea area = null;
if (list.size() != 0){
area = panel("姓名\t\t邮箱\t\t电话\t\t地址\n");
}
for (People p : list){
area.append(p.toString() + "\n");
a = 1;
}//判断联系人目录是否为空
if (a == 0){
JOptionPane.showMessageDialog(frame,"无任何联系人");
}
}
public JTextArea panel(String s) {
JDialog dialog1 = new JDialog();
dialog1.setBounds(700, 200, 800, 600);
dialog1.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
//将输入的信息进行转化,写入文件夹中
JTextArea area = new JTextArea();
area.setEnabled(false);
Font x = new Font("Serif", 3, 20);
area.setFont(x);
JScrollPane sp = new JScrollPane(area);
area.append(s);
// area.add(jLabel2);
dialog1.add(sp);
dialog1.setVisible(true);
return area;
}
@SuppressWarnings("null")
public void closeFile() {
ObjectOutputStream out = null;
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
try {
out = new ObjectOutputStream(new FileOutputStream(file));
out.writeObject(list);
out.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (out == null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//打开文件夹对信息进行查找
@SuppressWarnings({ "unchecked", "null" })
public void openFile() {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
ObjectInputStream in = null;
try {
in = new ObjectInputStream(new FileInputStream(file));
list = (List<People>) in.readObject();
} catch (Exception e) {
e.getMessage();
} finally {
if (in == null) {
try {
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
class myWindow implements WindowListener{
@Override
public void windowOpened(WindowEvent e) {
}
@Override
public void windowClosing(WindowEvent e) {
closeFile();
}
@Override
public void windowClosed(WindowEvent e) {
}
@Override
public void windowIconified(WindowEvent e) {
}
@Override
public void windowDeiconified(WindowEvent e) {
}
@Override
public void windowActivated(WindowEvent e) {
}
@Override
public void windowDeactivated(WindowEvent e) {
}
}
public static void main(String[] args) {
new Load();
}
}
package Dome;
import java.io.Serializable;
public class People implements Serializable {
private static final long serialVersionUID = 2921549567825356084L;
private String name;
private String Emil;
private String adress;
private String NO;
@Override
public String toString() {
return name + "\t\t" + Emil + "\t\t" + NO + "\t\t" + adress;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmil() {
return Emil;
}
public void setEmil(String emil) {
Emil = emil;
}
public String getAdress() {
return adress;
}
public void setAdress(String adress) {
this.adress = adress;
}
public String getNO() {
return NO;
}
public void setNO(String NO) {
this.NO = NO;
}
}
图片这么黑,看不清楚哦,能不能直接贴代码。
看不清楚,用电脑截屏上传报错消息看看,
您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps: 问答会员年卡【8折】购 ,限时加赠IT实体书,即可 享受50次 有问必答服务,了解详情>>>https://t.csdnimg.cn/RW5m