Java圖形化編程之JFrame疫苗接種系統(tǒng)詳解
1.功能模塊
1.1登陸模塊
1.1.1思路:
利用JFrame彈出一個(gè)登陸界面,用戶輸入admin和123456表示正確否則登陸失敗,給登陸按鈕綁定一個(gè)點(diǎn)擊事件(得到用戶輸入的內(nèi)容進(jìn)行比對(duì)如果正確就彈出信息展示模塊的JFrame界面-VaccineJframe),給取消按鈕綁定事件將兩個(gè)文本框的內(nèi)容置空。
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;//標(biāo)題 private JLabel label1;//用戶名 private JLabel label2;//密碼 private JButton loginButton;//登陸按鈕 private JButton cancelButton;//取消按鈕 /** * 初始化窗口 */ public Application() { // 設(shè)置容器為空布局,絕對(duì)定位 this.setLayout(null); // 創(chuàng)建標(biāo)題字體對(duì)象 Font font = new Font("微軟雅黑", Font.BOLD, 25); // 創(chuàng)建顏色對(duì)象 Color color = new Color(128, 200, 128); // 登陸界面標(biāo)簽 label0 = new JLabel("登陸界面"); label0.setBounds(200, 50, 150, 50); label0.setFont(font); label0.setForeground(color); // 用戶名標(biāo)簽 label1 = new JLabel("用戶名:"); label1.setBounds(110, 110, 100, 20); // 密碼標(biāo)簽 label2 = new JLabel("密碼:"); label2.setBounds(110, 160, 100, 20); // 創(chuàng)建組件 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, "登陸成功"); //跳轉(zhuǎn)到查看所有的疫苗界面(隱藏當(dāng)前窗體) Application.this.setVisible(false);//隱藏當(dāng)前窗體 new VaccineJframe(); }else { JOptionPane.showMessageDialog(null, "賬號(hào)密碼錯(cuò)誤-默認(rèn)賬號(hào)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); // 設(shè)置標(biāo)題 this.setTitle("疫苗管理系統(tǒng)"); // 設(shè)置窗口的關(guān)閉策略 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 設(shè)置窗口大小 this.setSize(500, 300); // 隱藏標(biāo)題欄 this.setUndecorated(false); // 設(shè)置不可調(diào)整窗口大小 this.setResizable(true); // 設(shè)置窗口居中,放在窗口大小后面,null表示桌面 this.setLocationRelativeTo(null); // 將窗口設(shè)置為顯示,要寫(xiě)在最后一句 this.setVisible(true); } public static void main(String[] args) { new Application(); } }
1.1.3運(yùn)行效果:
1.2信息展示模塊(從txt文件中讀?。?/h3>
1.2.1思路:
從D盤(pán)下的txt文件用io流讀取出來(lái),并封裝成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是我自己封裝的工具類(專門(mén)用來(lái)讀文件的) 核心代碼如下:
位置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運(yùn)行效果:
1.3新增記錄模塊(并更新txt)
1.3.1思路:
點(diǎn)擊新增按鈕彈出一個(gè)新的窗體,里面讓用戶輸入一些必要信息(必填的和需要正則驗(yàn)證的都要做)。最后點(diǎn)擊添加的時(shí)候保存到txt文件中(在文件內(nèi)容末尾換行追加一行,每個(gè)字段之間空格隔開(kāi))。隱藏添加界面打開(kāi)信息展示界面(重新從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是我自己定義的一個(gè)工具類-專門(mén)在最后一條記錄追加 }
位置2:write工具類:/yimiao/src/com/jiefan/tools/Write.java
/** * 新增一行疫苗信息數(shù)據(jù) * @param v */ 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) { // TODO Auto-generated catch block e.printStackTrace(); }finally { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } }
1.3.3運(yùn)行效果
1.4刪除記錄(含多選刪除并更新txt)
1.5修改記錄(并更新txt)
1.6多條件查詢
總結(jié)
本篇文章就到這里了,希望能夠給你帶來(lái)幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
詳細(xì)分析Java內(nèi)部類——局部?jī)?nèi)部類
這篇文章主要介紹了Java局部?jī)?nèi)部類的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)Java 內(nèi)部類的知識(shí),感興趣的朋友可以了解下2020-08-08spring中@Autowire和@Resource的區(qū)別在哪里(推薦)
這篇文章主要介紹了spring中@Autowire和@Resource的區(qū)別在哪里?本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-02-02idea啟動(dòng)springboot報(bào)錯(cuò): 找不到或無(wú)法加載主類問(wèn)題
這篇文章主要介紹了idea啟動(dòng)springboot報(bào)錯(cuò): 找不到或無(wú)法加載主類問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12MyBatis批量插入/修改/刪除MySql數(shù)據(jù)
這篇文章主要給大家介紹了關(guān)于MyBatis批量插入/修改/刪除MySql數(shù)據(jù)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05詳解用Kotlin寫(xiě)一個(gè)基于Spring Boot的RESTful服務(wù)
這篇文章主要介紹了詳解用Kotlin寫(xiě)一個(gè)基于Spring Boot的RESTful服務(wù) ,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05