


版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、小型倉庫管理系統(tǒng)的詳細設計及實現(xiàn)1. 系統(tǒng)設計概述(1)問題域部分,設計構造一組為底層應用建立模型的類和對象,細化分析結果;由 建立數(shù)據(jù)對象部分實現(xiàn)。( 2)人機交互部分, 設計一組有關類接口視圖的用戶模型的類和對象,設計用戶界面;由建立商品數(shù)據(jù)維護界面部分實現(xiàn)。(3)任務管理部分,確定系統(tǒng)資源的分配,設計用于系統(tǒng)中類的行為控制的對象/ 類;由建立商品類別顯示模型和商品類型模型部分以及建立商品數(shù)據(jù)維護界面部分共同實現(xiàn)。( 4)數(shù)據(jù)管理部分,確定持久對象的存儲,將對象轉換成數(shù)據(jù)庫記錄或表格;由連接 數(shù)據(jù)庫和建立商品數(shù)據(jù)訪問對象部分實現(xiàn)。2. 連接數(shù)據(jù)庫要通過 Java 訪問數(shù)據(jù)庫,必須要使用JD
2、BC( Java Data Base Connectivity,java數(shù)據(jù)庫連接),訪問不同的數(shù)據(jù)庫,采用的JDBC驅動程序也不同。連接Access采用Jdbc-odbc橋的方式比較方便,不需要引入額外的驅動程序包。主流的數(shù)據(jù)庫都提供了專門的JDBC驅動,女口 ORCALESql Server、Sybase等等,對于微軟的Access和Excel以及其他的一些小型的桌面數(shù)據(jù)庫,可以通過JDBC-ODBC接的方式來訪問。通過編碼,我們不需要在環(huán)境中配置ODB(數(shù)據(jù)源,就可以直接訪問這些小型桌面數(shù)據(jù)庫。在應用系統(tǒng)中, 數(shù)據(jù)庫的連接參數(shù)一般都是寫在配置文件當中, 以防止數(shù)據(jù)庫的屬性的 變化導致程序
3、的修改。 對于本任務來說, Access 是桌面數(shù)據(jù)庫, 我們需要知道的只是 Access 數(shù)據(jù)庫文件的路徑,因此我們可以將 Access 數(shù)據(jù)庫文件拷貝到發(fā)布路徑下,從而可以通過 編碼獲得數(shù)據(jù)庫文件路徑,而不需要使用配置文件。package test.sample.dao;import .URL;import java.sql.Connection;import java.sql.DriverManager;public class DaoFactory static public Connection getConnection() throws Exception Class.forNa
4、me("sun.jdbc.odbc.JdbcOdbcDriver"); / 加載數(shù)據(jù)庫驅動 URL dbUrl =DaoFactory.class.getClassLoader().getResource("Mystock.mdb"); String dbPath=dbUrl.getPath(); / 通過 Java 的類加載機制,獲得數(shù)據(jù)庫文件路徑 if (dbPath.charAt(0)='/') / 去掉路徑的第一個字符 /dbPath=dbPath.substring(1);String url = "jdbc:odbc
5、:driver=Microsoft Access Driver (*.mdb);DBQ="+dbPath;Connection con = DriverManager.getConnection(url, "sa", "");return con;Class.forName 方法的作用是通過類的完整路徑名獲得一個類的實例, 在本例子中的作 用是加載我們要使用的數(shù)據(jù)庫驅動。 在使用 DriverManager 獲得數(shù)據(jù)庫連接前, 必須通過此 語句加載數(shù)據(jù)庫驅動。ClassLoader 類負責管理 Java 編譯后代碼的加載,因此能夠獲取 clas
6、s 文件的加載路 徑,也就是應用的發(fā)布路徑。 我們把數(shù)據(jù)庫文件放到發(fā)布路徑下 (即 class 文件所在路徑) , 即可以通過 ClassLoader 類的 getResource 方法獲得該文件的絕對路徑。 由于獲得的絕對路 徑的第一個字符是“ / ”,這是為了兼容不同的操作系統(tǒng)而導致的,這個符號在 Access 的數(shù) 據(jù)庫路徑上是不合法的,需要去掉。通過數(shù)據(jù)庫 url ,提供數(shù)據(jù)庫的驅動描述,以及數(shù)據(jù)庫的路徑,就 可以使用 DriverManager 類來獲得數(shù)據(jù)庫連接。3. 建立商品數(shù)據(jù)訪問對象從數(shù)據(jù)庫中提取數(shù)據(jù),以及向數(shù)據(jù)庫中插入記錄和刪除數(shù)據(jù),都必須使用SQL語句來完成。因此數(shù)據(jù)訪問
7、對象的方法,實際上就是通過 Java 數(shù)據(jù)訪問對象,在數(shù)據(jù)庫數(shù)據(jù)和數(shù)據(jù) 對象之間進行轉換。在數(shù)據(jù)庫的訪問中,我們需要用到幾個數(shù)據(jù)訪問對象:1 java.sql.Connection:用來獲得數(shù)據(jù)庫連接。2. java.sql.PreparedStatement和 java.sql.Statement:都可以用來執(zhí)行 SQL語句。其中 PreparedStatement 的功能較強, SQL 語句被預編譯并且存儲在 PreparedStatement對象中。然后可以使用此對象高效地多次執(zhí)行該語句。PreparedStatement 可以設置 set 方法設置參數(shù)。以下是一個設置參數(shù)的示例中, c
8、on 表示一個活動連接:PreparedStatement pstmt = con.prepareStatement("UPDATE EMPLOYEESSET SALARY = ? WHERE ID = ?");pstmt.setBigDecimal(1, 2000.00) ;pstmt.setInt(2, 100) ;pstmt.excuteUpdate();SQL語句中需要賦值的部分可以用?代替,然后通過set 方法來設置參數(shù)。由于設計參數(shù)時不同的類型方法也不同, 能夠很好的處理不同的類型。 例如在設置日期型的參數(shù)時, 可 以直接通過setDate方法設置參數(shù),而如果通
9、過SQL語句的字符串拼寫,則必須對日期進行格式化。對于復雜的Insert、update和delete的SQL語句,以及需要多次執(zhí)行而參數(shù)不同 的SQL語句,使用 PreparedStatement 具有明顯優(yōu)勢。3java.sql.ResultSet:從數(shù)據(jù)庫庫提取后的數(shù)據(jù)保存在 ResultSet 中, ResultSet提供的 getter 方法能夠方便的返回記錄集中的數(shù)據(jù)。程序代碼設計如下:package test.sample.dao;import java.sql.*;import java.util.ArrayList;import java.util.List;import te
10、st.sample.bean.Product;import test.sample.bean.ProductType;public class ProductDao / 提取商品數(shù)據(jù),并轉換為商品對象,存儲到列表中public static List getProductList() throws ExceptionConnection conn=DaoFactory.getConnection();tryStatement st = conn.createStatement();String sql="select * from Product"ResultSet rs
11、= st.executeQuery(sql);List list=new ArrayList();while(rs.next()Product p=new Product();p.setId(rs.getInt("ID");p.setName(rs.getString("NAME");p.setType(rs.getInt("TYPE");p.setCountInBox(rs.getString("COUNT_IN_BOX");p.setPrice(rs.getFloat("PRICE");p.
12、setStock(rs.getInt("STOCK");list.add(p);rs.close();st.close();return list;finallyconn.close();/ 將一個商品對象插入到數(shù)據(jù)庫public static Product InsertProduct(Product p) throws ExceptionConnection conn=DaoFactory.getConnection();tryString sql="INSERT INTO Product(NAME,TYPE,COUNT_IN_BOX,PRICE,STOCK)
13、 VALUES(?,?,?,?,?)"PreparedStatement ps=conn.prepareStatement(sql);ps.setString(1,p.getName();ps.setInt(2,p.getType();ps.setString(3,p.getCountInBox();ps.setFloat(4,p.getPrice();ps.setInt(5,p.getStock();ps.executeUpdate();Statement st=conn.createStatement();ResultSet rs=st.executeQuery("se
14、lect max(ID) from Product");rs.next();int id = rs.getInt(1);p.setId(id);rs.close();st.close();ps.close();return p;finallyconn.close();/ 將一個商品對象插入到數(shù)據(jù)庫public static Product updateProduct(Product p) throws ExceptionConnection conn=DaoFactory.getConnection();tryString sql="UPDATE Product setNA
15、ME=?,TYPE=?,COUNT_IN_BOX=?,PRICE=?,STOCK=? where ID=?"PreparedStatement ps=conn.prepareStatement(sql);ps.setString(1,p.getName();ps.setInt(2,p.getType();ps.setString(3,p.getCountInBox();ps.setFloat(4,p.getPrice();ps.setInt(5,p.getStock();ps.setInt(6,p.getId();ps.executeUpdate();ps.close();retur
16、n p;finallyconn.close();/ 刪除一個商品記錄public static boolean deleteProduct(int id) throws Exception Connection conn=DaoFactory.getConnection(); tryString sql="delete from Product where ID="+id;Statement ps=conn.createStatement();boolean b = ps.execute(sql);ps.close();return b;finallyconn.close(
17、);/ 獲取商品類型數(shù)據(jù),并轉換為商品類型對象,存儲到列表 public static List getProductTypeList() throws ExceptionConnection conn=DaoFactory.getConnection();tryStatement st = conn.createStatement();String sql="select * from ProductType" ResultSet rs = st.executeQuery(sql); List list=new ArrayList();while(rs.next()Pro
18、ductType pt=new ProductType(); pt.setId(rs.getInt("TYPE_ID"); pt.setName(rs.getString("TYPE_NAME"); pt.setRemark(rs.getString("REMARK"); list.add(pt);rs.close();st.close();return list;finallyconn.close();通過上面的代碼我們可以看到,從 ProductDao 調用方法的對象不需要關系數(shù)據(jù)庫,而只需要知道 Product 對象即可, 無論
19、我們如何修改 “商品”數(shù)據(jù)的存儲方法, 都不會影響訪問對象。也就是說,我們通過 ProductDao 對象將對商品表數(shù)據(jù)的訪問完全的封裝了。訪問 商品數(shù)據(jù)的對象甚至不知道商品數(shù)據(jù)是保存在什么樣的數(shù)據(jù)庫當中。 這就是面向對象設計的 重要特性封裝。ProductDao 對象提供了“商品”數(shù)據(jù)的提取、插入、刪除方法,提供了商品類別的提取 方法。事實上, 這不是數(shù)據(jù)操作的全部, 所有涉及商品的數(shù)據(jù)庫操作, 都可以放到 ProductDao 對象中,如修改商品的名稱,統(tǒng)計商品的庫存等等。4. 建立數(shù)據(jù)對象數(shù)據(jù)對象作為應用程序的數(shù)據(jù)載體, 能夠用于數(shù)據(jù)加工和數(shù)據(jù)傳遞, 同時由于數(shù)據(jù)對象 是無狀態(tài)的,因此數(shù)
20、據(jù)對象的使用能夠應用實現(xiàn)更加地靈活。數(shù)據(jù)對象( JavaBean )是一種特殊的對象,特殊的是在類的方法命名上遵守以下規(guī)則: 1如果類的成員變量的名字是xxx ,那么為了更改或獲取成員變量的值,即更改或獲取屬性,在類中可使用兩個方法: getXxx() ,用來獲取屬性; xxx ;setXxx() ,用來修改屬 性 xxx. 。2 對于 boolean 類型的成員變量, 即布爾邏輯類型的屬性, 允許使用 "is" 代替上面的 "get" 。3類中方法的訪問屬性都必須是public 的。4類中如果有構造方法,那么這個構造方法也是public 的并且是無參數(shù)
21、的。事實上,這種方式是面向對象設計的一個理念,即封裝。通過 get 和 set 方法,將對 象的外在表現(xiàn)和內部實現(xiàn)分離, 從而為對象的修改和重用提供良好的基礎。 例如: 一個方法 getName,它的作用是返回對象的名字,作為訪問者無需知道這個名字是如何存儲的,只需 調用方法訪問即可。package test.sample.bean;/* 商品對象*/ 商品的唯一標識/ 商品的名稱/ 商品的類型內碼 每箱數(shù)量/ 商品的價格庫存數(shù)量public class Product private int id;private String name; private int type;private S
22、tring countInBox; / private float price;private int stock; /public int getId() return id;public void setId(int id) this.id = id;public String getName() return name;public void setName(String name) = name;public int getType() return type;public void setType(int type) this.type = type;public
23、 float getPrice() return price;public void setPrice(float price) this.price = price;public String getCountInBox() return countInBox;public void setCountInBox(String countInBox) this.countInBox = countInBox;public int getStock() return stock;public void setStock(int stock) this.stock = stock;我們可以看到,
24、上面的兩個數(shù)據(jù)對象中, 都是通過 get 和 set 方法對私有屬性進行封裝。那么,不使用 get 和 set 方法,直接將屬性作為公開屬性不行嗎?單從程序訪問的角度沒 有問題,我們可以直接訪問公開屬性,但同時也破壞了數(shù)據(jù)對象的封裝性。更為重要的是, 數(shù)據(jù)對象這種形式已經廣泛的為業(yè)界所承認, 成為一種規(guī)則, 絕大部分 的開源軟件通過 get 和 set 這種形式來訪問數(shù)據(jù),例如 Hibernate , spring 。事實上,這種 形式不僅僅在 java 中使用,在主流的面向對象語言中,都采用了這種形式。5. 建立商品列表顯示模型和商品類型模型為了能夠將數(shù)據(jù)對象列表能夠以表格( table )
25、的形式進行顯示,我們需要為表格顯示 控件 JTable 提供一個數(shù)據(jù)來源。實現(xiàn)一個 AbstractTableModel ,能夠為 JTable 控件提供數(shù)據(jù),將商品對象列表以表格 的形式進行顯示。在 Java swing 中以表格形式顯示數(shù)據(jù)的標準控件是 JTable ,一般的情況下,數(shù)據(jù)以對 象列表或者對象數(shù)組的形式存儲, 表格的每一行顯示一個對象的信息, 表格的每一列顯示對 象某個屬性的信息。為了能夠方便的使用 JTable , Java 提供了專門支持它的數(shù)據(jù)模型 TableModel 。 TableModel 負責將數(shù)據(jù)整理為 JTable 顯示所需要的形式, 是介于基礎數(shù)據(jù)和顯示控
26、件之間 的代理。在創(chuàng)建 JTable 時,可以采用帶參數(shù)的創(chuàng)建方法,將 TableModel 作為參數(shù)傳遞給 JTable 。 JTable 的數(shù)據(jù)顯示是通過 TableModel 進行驅動的,因此修改 TableModel 中的數(shù) 據(jù),能夠通過調用方法刷新 JTable 的數(shù)據(jù)顯示。為了更加方便的處理數(shù)據(jù)和數(shù)據(jù)的顯示,我們需要從 AbstractTableModel 類繼承,實 現(xiàn)自己的 TableModel 類。 AbstractTableModel 是一個抽象數(shù)據(jù)類型,我們有三個方法必須實現(xiàn):獲得要顯示的數(shù)據(jù)的列數(shù)獲得要顯示的數(shù)據(jù)的行數(shù)獲得一個單元格的數(shù)據(jù)public int getCo
27、lumnCount()/public int getRowCount()/public Object getValueAt(int row, int col) /以上三個方法的實現(xiàn)可以根據(jù)原始數(shù)據(jù)和顯示需求來實現(xiàn),例如 getColumnCount 可以 根據(jù)需要顯示的屬性的個數(shù)確定, getRowCount 可以通過數(shù)據(jù)記錄的個數(shù)確定。為了支持對數(shù)據(jù)的修改操作,我們還可以在 TableModel 中添加一些數(shù)據(jù)修改的方法。 package test.sample.model;import java.util.ArrayList;import java.util.List;import jav
28、ax.swing.table.AbstractTableModel;import test.sample.bean.Product;import test.sample.dao.ProductDao;public class ProductModel extends AbstractTableModel private List pList=new ArrayList();public void loadData() throws Exception pList = ProductDao.getProductList();/ 列標題String headers = "商品內碼 &qu
29、ot;, " 商品名稱 ", " 商品類型 ", " 單位數(shù)量 ", " 價格", " 庫存"/ 獲得列數(shù)public int getColumnCount() return headers.length;/ 獲得行數(shù)public int getRowCount() return pList.size();/ 獲得一個單元格的數(shù)據(jù)public Object getValueAt(int row, int col) if (row<0) |(row>=pList.size() retu
30、rn ""Product p = (Product) pList.get(row);switch (col) case 0: return new Integer(p.getId();case 1: return p.getName();case 2: return ProductTypeEnum.getTypeName(p.getType();case 3: return p.getCountInBox();case 4: return new Float(p.getPrice();case 5: return new Integer(p.getStock(); retu
31、rn ""public Product getProduct(int row)return (Product) pList.get(row);/ 獲得列的名字Overridepublic String getColumnName(int col) return headerscol;/ 添加一個商品對象public void addProduct(Product p) throws ExceptionProductDao.InsertProduct(p);pList.add(p); this.fireTableRowsInserted(pList.size()-1, pLi
32、st.size()-1);/ 更新一個商品對象public void updateProduct(int index,Product p) throws Exceptionif (index<0) |(index>=pList.size() return ;ProductDao.updateProduct(p); pList.set(index, p); this.fireTableRowsUpdated(index, index);/ 刪除一個商品對象public void deleteProduct(int index) throws Exceptionif (index<
33、;0) |(index>=pList.size() return ;Product p1=getProduct(index);ProductDao.deleteProduct(p1.getId();pList.remove(index); this.fireTableRowsDeleted(index, index);ProductModel 擴展類 AbstractTableModel 類,以從數(shù)據(jù)庫提出的對象列表作為數(shù)據(jù)源。 并根據(jù)數(shù)據(jù)列表信息實現(xiàn)了基類的三個虛方法: getRowCount() 、 getColumnCount() 、 getValueAt(int row, int
34、 column);為了支持數(shù)據(jù)的增加、刪除和修改功能, ProductModel 還實現(xiàn)了三個方法:1 addProduct(Product p) :添加一個商品信息。2 updateProduct(int index,Product p):更新某行的商品信息。3 deleteProduct(int index):刪除某行的商品信息。以上三個方法直接調用數(shù)據(jù)庫操作對象的方法, 進行數(shù)據(jù)的添加、 刪除和修改, 數(shù)據(jù)庫 修改成功后, 修改顯示列表, 并調用方法 fireTableRowsInserted、fireTableRowsUpdated 、fireTableRowsDeleted ,激活
35、JTable 的顯示更新。另外、 對于商品類型屬性,在商品對象中保存的是類型的內碼,而顯示的時候,需要顯 示類型的名稱,在這種情況下,我們需要做通過商品類別模型獲得類型名稱。商品表的商品類別保存的是商品類別內碼, 或者說,商品類別內碼是商品表的一個外鍵。 類別表保存了所有商品類別的信息,包括類別 ID ,類別名稱和類別描述三個字段。雖然商 品表中使用類別 ID 記錄商品的類別,但是在列表中顯示的時候,需要顯示給用戶商品類別 的類別名稱。在新增商品信息時, 也需要將商品類別名稱做成下拉列表, 在列表中選擇商品類別, 同 時在商品對象中要保存的卻是類別ID 。以上的情況都要求一個對象來管理商品類別
36、中類別 ID 和類別名稱的映射關系。 作為商品的關聯(lián)信息,商品類別的在商品表中存儲時使用類別 ID ,而展示時總是展示類別名稱,因此通過類別 ID 獲得類別名稱, 以及通過類別名稱獲得類別 ID 的功能是必須的。 package test.sample.model; import java.util.HashMap;import java.util.List; import java.util.Map;import test.sample.bean.ProductType; import test.sample.dao.ProductDao; public class ProductTypeE
37、num static private Map map = null; static private Map nameMap = null; static private String names; static private List dataList = null; private static List getDataList() if (dataList = null) try dataList = ProductDao.getProductTypeList(); catch (Exception e) e.printStackTrace();return dataList;stati
38、c public String getTypeName(int type) if (map = null) map = new HashMap(); List list = getDataList(); for (int i = 0; i < list.size(); i+) ProductType pt = (ProductType) list.get(i); map.put("" + pt.getId(), pt); ProductType p = (ProductType) map.get("" + type); if (p != null)
39、return p.getName();elsereturn "" static public int getNameId(String name) if (nameMap = null) nameMap = new HashMap(); List list = getDataList(); for (int i = 0; i < list.size(); i+) ProductType pt = (ProductType) list.get(i); nameMap.put(pt.getName(), pt);ProductType p = (ProductType)
40、nameMap.get(name); if (p != null)return p.getId();elsereturn -1;public static String getNames() if (names = null) List list = getDataList();names = new Stringlist.size();for (int i = 0; i < list.size(); i+) ProductType pt = (ProductType) list.get(i); namesi = pt.getName();return names;6. 構造商品數(shù)據(jù)維護
41、界面整個界面可以分成三大部分:顯示列表的部分、顯示錄入控件的部分、 的顯示通過 JTable 可以完成, 彈出菜單使用 JpopupMenu 控件來完成, 需要用到 Jlabel 、JcomboBox、JtextField 、JformattedTextField 、彈出菜單。 列表 而數(shù)據(jù)的錄入部分則 Jbutton 等控件。由于錄入控件較多,我們采用固定位置的布局方式。package test.sample.view;import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.event
42、.ListSelectionEvent;import javax.swing.event.ListSelectionListener;import test.sample.bean.Product;import test.sample.model.ProductModel;import test.sample.model.ProductTypeEnum;public class ProductView extends JFrame private JButton buttonInsert; /插入按鈕private JButton buttonUpdate; /修改按鈕private JTex
43、tField jxt1; /private JTextField jxt2; / private JComboBox jcb1; /商品名稱錄入控件商品數(shù)量錄入控件 商品類別下拉列表private JFormattedTextField jft2; / 庫存錄入 private JPopupMenu popup; / 彈出菜單 private JTable jt; / 商品列表顯示控件 private JScrollPane jsp; /商品列表容器private JPanel jp1; /數(shù)據(jù)明細控件容器private ProductModel pm; /商品模型實例private Cont
44、ainer rootPanel; / 主界面容器 / 讓界面居與屏幕中間 private void centerFrame() setSize(600, 400); / 設置界面大小 Toolkit kit = Toolkit.getDefaultToolkit(); Dimension screenSize = kit.getScreenSize(); int screenHeight = screenSize.height; int screenWidth = screenSize.width; int frameH = getHeight();int frameW = getWidth(
45、); /計算和設置界面居中的位置setLocation(screenWidth - frameW) / 2, (screenHeight - frameH) / 2); setDefaultCloseOperation(EXIT_ON_CLOSE);/ 初始化彈出菜單 private void initPopupMenu() popup = new JPopupMenu(); JMenuItem item = new JMenuItem(" 刪除商品信息 "); item.addActionListener(new ActionListener() /鼠標點擊處理匿名內部類
46、public void actionPerformed(ActionEvent e) int index = jt.getSelectedRow(); /確定在 JTable 上右鍵點擊位置的行if (index >= 0) Integer pId = (Integer) jt.getValueAt(index, 0);String pName = (String) jt.getValueAt(index, 1); int i = JOptionPane.showConfirmDialog(null, "確定要刪除商品 " + pName + "?"
47、;, " 標題 ", JOptionPane.YES_NO_OPTION); / 彈出確認對話框 if (i = JOptionPane.YES_OPTION) try /刪除數(shù)據(jù)庫記錄pm.deleteProduct(index); / 刪除顯示記錄 catch (Exception e1) e1.printStackTrace(); );popup.add(item);jt.addMouseListener(new MouseAdapter() /鼠標右鍵監(jiān)聽匿名內部類public void mouseReleased(MouseEvent event) final i
48、nt row = jt.rowAtPoint(event.getPoint();if (row != -1) jt.setRowSelectionInterval(row, row); / 高亮選擇右鍵點擊的行if (event.isPopupTrigger()popup.show(event.getComponent(), event.getX(), event.getY(););/ 初始化錄入控件public void iniInsertPanel() throws Exception JLabel jl1 = new JLabel();jl1.setText(" 商品名稱 &q
49、uot;);jl1.setBounds(10, 10, 60, 20);jp1.add(jl1);jxt1 = new JTextField(10);jxt1.setBounds(75, 10, 200, 20);jp1.add(jxt1);JLabel jl2 = new JLabel();jl2.setText(" 商品類型 ");jl2.setBounds(285, 10, 60, 20);jp1.add(jl2);jcb1 = new JComboBox(ProductTypeEnum.getNames();jcb1.setBounds(350, 10, 100,
50、20);jp1.add(jcb1, BorderLayout.EAST);JLabel jl3 = new JLabel();jl3.setText(" 單位數(shù)量 ");jp1.add(jl3);jl3.setBounds(10, 40, 60, 20);jxt2 = new JTextField(6);jxt2.setBounds(75, 40, 80, 20);jp1.add(jxt2);JLabel jl4 = new JLabel();jl4.setText(" 單價 ");jl4.setBounds(160, 40, 60, 20);jp1.a
51、dd(jl4);jft1 = new JFormattedTextField();jft1.setColumns(5);jft1.setBounds(195, 40, 80, 20);jp1.add(jft1);JLabel jl5 = new JLabel();jl5.setText(" 庫存 "); jl5.setBounds(310, 40, 40, 20);jp1.add(jl5);jft2 = new JFormattedTextField();jft2.setColumns(5);jft2.setBounds(350, 40, 100, 20);jp1.add(
52、jft2);buttonInsert = new JButton("添加 ");buttonInsert.setBounds(460, 10, 60, 50);/ 添加按鈕點擊事件 buttonInsert.addActionListener(new ActionListener() public void actionPerformed(ActionEvent event) try doInsertProduct(); catch (Exception e) e.printStackTrace(););jp1.add(buttonInsert); buttonUpdate
53、 = new JButton(" 修改 "); buttonUpdate.setBounds(530, 10, 60, 50);/ 修改按鈕點擊事件 buttonUpdate.addActionListener(new ActionListener() public void actionPerformed(ActionEvent event) try doUpdateProduct(); catch (Exception e) e.printStackTrace();); jp1.add(buttonUpdate);/ 從錄入控件中組織一個商品信息 private Pro
54、duct getProductInfo() Product p = new Product();if (jxt1.getText().equals("")JOptionPane.showMessageDialog(buttonInsert," 請 輸 入 商 品 信 息 ! "," 提 示 ",JOptionPane.WARNING_MESSAGE);return null; p.setName(jxt1.getText();int typeId = ProductTypeEnum.getNameId(String) jcb1.get
55、SelectedItem();p.setType(typeId);p.setCountInBox(jxt2.getText();p.setPrice(Float.parseFloat(jft1.getText();p.setStock(Integer.parseInt(jft2.getText();return p;/ 新增商品數(shù)據(jù)方法private void doInsertProduct() throws Exception if (getProductInfo()=null)return;pm.addProduct(getProductInfo();int row=pm.getRowCo
56、unt(); jt.setRowSelectionInterval(row-1,row-1); Rectangle rect = jt.getCellRect(row-1, 0, true); jt.scrollRectToVisible(rect);/ 更新商品數(shù)據(jù)private void doUpdateProduct() throws Exception Product p1 = getProductInfo();int selectedRowIndex = jt.getSelectedRow();if (selectedRowIndex<0) JOptionPane.showMessag
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 南京商品房買賣合同終止協(xié)議書
- 庭院瀑布水系施工方案
- 文化傳承項目數(shù)據(jù)表格
- 文言文的讀音、用法及文化內涵介紹
- 課本課文全文背誦的技巧和好處
- 電纜溝施工方案
- 商品庫存預警報告表
- 二級路施工方案
- 外墻堵漏注漿施工方案
- 專業(yè)舊樓翻修防水施工方案
- COP生產一致性控制計劃
- 2025年電力人工智能多模態(tài)大模型創(chuàng)新技術及應用報告-西安交通大學
- 天津2025年天津市機關后勤事務服務中心分支機構天津市迎賓館招聘2人筆試歷年參考題庫附帶答案詳解
- 2025年江蘇南京技師學院招聘工作人員19人高頻重點模擬試卷提升(共500題附帶答案詳解)
- 華東師大版七年級數(shù)學下冊“第1周周考”
- DBJ50-T-385-2023半柔性復合路面技術標準
- 職業(yè)院校教師人工智能素養(yǎng):內涵流變、框架構建與生成路徑
- 如何在初中數(shù)學教學中提升學生的核心素養(yǎng)
- (完整版)小學一年級數(shù)學20以內進退位加減法(1600道題)計算卡
- 2025年包頭鐵道職業(yè)技術學院高職單招語文2018-2024歷年參考題庫頻考點含答案解析
- 教師論文撰寫培訓
評論
0/150
提交評論