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); // 設置窗口居中,放在窗口大小后面,null表示桌面 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();//read是我自己封裝的工具類 return vs; } catch (IOException e) { e.printStackTrace(); } return null; }
上面的read是我自己封裝的工具類(專門用來讀文件的) 核心代碼如下:
位置2:/yimiao/src/com/jiefan/tools/Read.java
/** * 從txt文件中讀取疫苗接種信息 * @return * @throws IOException */ 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; }
1.2.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);//write是我自己定義的一個工具類-專門在最后一條記錄追加 }
位置2:write工具類:/yimiao/src/com/jiefan/tools/Write.java
/** * 新增一行疫苗信息數據 * @param v */ public void appendV(Vaccine v) { BufferedWriter out=null; //最后追加一行 try { out=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(Config.filePath, true))); out.write(" "+v.getId()+" "+v.getVname()+" "+v.getUname()+" "+v.getVdate()+" "+v.getPhone()+" "+v.getUnit()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } }
1.3.3運行效果
1.4刪除記錄(含多選刪除并更新txt)
1.5修改記錄(并更新txt)
1.6多條件查詢
總結
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關注服務器之家的更多內容!
原文鏈接:https://blog.csdn.net/qq_32079585/article/details/120241251