1.功能模块
1.1登陆模块
1.1.1思路:利用JFrame弹出一个登陆界面,用户输入admin和123456表示正确否则登陆失败,给登陆按钮绑定一个点击事件(得到用户输入的内容进行比对如果正确就弹出信息展示模块的JFrame界面-VaccineJframe),给取消按钮绑定事件将两个文本框的内容置空。
1.1.2核心代码:
位置:/yimiao/src/com/jiefan/Application.java
package com.jiefan;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import com.jiefan.jframe.VaccineJframe;
public class Application extends JFrame {
private static final long serialVersionUID = 1L;
private JTextField username;
private JPasswordField password;
private JLabel label0;
private JLabel label1;
private JLabel label2;
private JButton loginButton;
private JButton cancelButton;
public Application() {
this.setLayout(null);
Font font = new Font("微软雅黑", Font.BOLD, 25);
Color color = new Color(128, 200, 128);
label0 = new JLabel("登陆界面");
label0.setBounds(200, 50, 150, 50);
label0.setFont(font);
label0.setForeground(color);
label1 = new JLabel("用户名:");
label1.setBounds(110, 110, 100, 20);
label2 = new JLabel("密码:");
label2.setBounds(110, 160, 100, 20);
username = new JTextField();
username.setBounds(180, 110, 200, 20);
password = new JPasswordField();
password.setBounds(180, 160, 200, 20);
loginButton = new JButton("登陆");
loginButton.setBounds(205, 200, 60, 20);
loginButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(username.getText().equals("admin") && String.valueOf(password.getPassword()).equals("123456")) {
JOptionPane.showMessageDialog(null, "登陆成功");
Application.this.setVisible(false);
new VaccineJframe();
}else {
JOptionPane.showMessageDialog(null, "账号密码错误-默认账号admin密码123456");
}
}
});
cancelButton = new JButton("取消");
cancelButton.setBounds(265, 200, 60, 20);
cancelButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
username.setText("");
password.setText("");
}
});
this.add(username);
this.add(password);
this.add(label0);
this.add(label1);
this.add(label2);
this.add(loginButton);
this.add(cancelButton);
this.setTitle("疫苗管理系统");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(500, 300);
this.setUndecorated(false);
this.setResizable(true);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public static void main(String[] args) {
new Application();
}
}
1.1.3运行效果:
1.2信息展示模块(从txt文件中读取)
1.2.1思路:从D盘下的txt文件用io流读取出来,并封装成list用jtable组件展示到界面上
1.2.2核心代码 :
位置1:/yimiao/src/com/jiefan/dao/VaccineDao.java
public List<Vaccine> getAll(){
try {
List<Vaccine> vs=read.getList();
return vs;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
上面的read是我自己封装的工具类(专门用来读文件的) 核心代码如下: 位置2:/yimiao/src/com/jiefan/tools/Read.java
public List<Vaccine> getList() throws IOException{
List<Vaccine> vaccines=new ArrayList<Vaccine>();
FileReader fr = new FileReader(Config.filePath);
BufferedReader br = new BufferedReader(fr);
String line = br.readLine();
for (int i = 0; line != null; i++) {
line = br.readLine();
String[] lineArr = null;
if (line != null) {
lineArr = line.split(" ");
Vaccine v=new Vaccine();
v.setId(Integer.valueOf(lineArr[0]));
v.setVname(lineArr[1]);
v.setUname(lineArr[2]);
v.setVdate(lineArr[3]);
v.setPhone(lineArr[4]);
v.setUnit(lineArr[5]);
vaccines.add(v);
}
}
br.close();
fr.close();
return vaccines;
}
12.3运行效果:
1.3新增记录模块(并更新txt)
1.3.1思路:点击新增按钮弹出一个新的窗体,里面让用户输入一些必要信息(必填的和需要正则验证的都要做)。最后点击添加的时候保存到txt文件中(在文件内容末尾换行追加一行,每个字段之间空格隔开)。隐藏添加界面打开信息展示界面(重新从txt读取一遍)。
1.3.2核心代码
位置1:/yimiao/src/com/jiefan/dao/VaccineDao.java
public void addV(Vaccine v) {
List<Vaccine> vs=getAll();
if(v.getId() ==null) {
if(vs!=null&&vs.size()>0)v.setId(vs.get(vs.size()-1).getId()+1);
else v.setId(1);
}
write.appendV(v);
}
位置2:write工具类:/yimiao/src/com/jiefan/tools/Write.java
public void appendV(Vaccine v) {
BufferedWriter out=null;
try {
out=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(Config.filePath, true)));
out.write("\r\n"+v.getId()+" "+v.getVname()+" "+v.getUname()+" "+v.getVdate()+" "+v.getPhone()+" "+v.getUnit());
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
1.3.3运行效果
在这里插入图片描述
1.4删除记录(含多选删除并更新txt)
1.5修改记录(并更新txt)
1.6多条件查询
|