程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA綜合教程 >> Java-單機版的書店管理系統(練習設計模塊和思想_系列 四)

Java-單機版的書店管理系統(練習設計模塊和思想_系列 四)

編輯:JAVA綜合教程

Java-單機版的書店管理系統(練習設計模塊和思想_系列 四)


本系列四介紹說明:

主要是添加了Uuid模塊,現在的uuid並不是由用戶輸入,而是我們通過軟件內部自動生成(通過唱票模式),uuid是不會產生重復的!

還有把主界面改了下,以後會改成登錄頁面的(待完善)。

現在進入其他模塊是通過菜單項進入的。

增加了圖書模塊!

注意:
每個模塊先寫哪個,我都是按照先後順序來上代碼的,從前往後依次寫代碼的。
注意是每個模塊!!!
先寫公共類和工具類再寫模塊的!
我公共類和工具類上傳在最後了,你看可以直接在左上角找目錄跳到那去看。
(限每個模塊內部)(因為還有公共類和工具類等)

軟件部分圖片:

現在先看軟件部分運行圖片:
待完成的主界面:現在通過菜單條:模塊進入其他的模塊:
\

用戶模塊主界面:

圖書模塊主界面:

添加用戶的界面:
相比上一系列,減少了uuid的輸入:
(其他如增刪改查的界面完全沒有動,這就是設計模塊的好處)

添加圖書的界面:

書店管理系統文件目錄:

\

主界面代碼:

main()方法:BookStore

/*
 * BookStore.java
 *
 * Created on __DATE__, __TIME__
 */

package cn.hncu.bookStore;

import java.awt.Dimension;
import java.awt.Toolkit;

import cn.hncu.bookStore.book.ui.BookListPanel;
import cn.hncu.bookStore.user.ui.ListPanel;

/**
 *
 * @author  陳浩翔
 */
public class BookStore extends javax.swing.JFrame {

    /** Creates new form BookStore */
    public BookStore() {
        super("書店管理系統---CHX---聯系QQ——619699629");
        initComponents();
        //this.setContentPane(new ListPanel(this));
        this.setResizable(false);//不能縮放
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        Dimension scr = Toolkit.getDefaultToolkit().getScreenSize();
        this.setLocation(scr.width / 5, scr.height / 5);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    //GEN-BEGIN:initComponents
    // 
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        menuBar = new javax.swing.JMenuBar();
        fileMenu = new javax.swing.JMenu();
        openMenuItem = new javax.swing.JMenuItem();
        saveMenuItem = new javax.swing.JMenuItem();
        saveAsMenuItem = new javax.swing.JMenuItem();
        exitMenuItem = new javax.swing.JMenuItem();
        editMenu = new javax.swing.JMenu();
        cutMenuItem = new javax.swing.JMenuItem();
        copyMenuItem = new javax.swing.JMenuItem();
        pasteMenuItem = new javax.swing.JMenuItem();
        deleteMenuItem = new javax.swing.JMenuItem();
        helpMenu = new javax.swing.JMenu();
        contentsMenuItem = new javax.swing.JMenuItem();
        aboutMenuItem = new javax.swing.JMenuItem();
        jMenu1 = new javax.swing.JMenu();
        jMenuItemUser = new javax.swing.JMenuItem();
        jMenuItemBook = new javax.swing.JMenuItem();
        jMenuItem1 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(800, 600));

        jButton1.setIcon(new javax.swing.ImageIcon(
                "E:\\百度雲同步盤\\MyJavaWork\\myBookStore\\shudian.png")); // NOI18N
        jButton1.setMaximumSize(new java.awt.Dimension(7500, 2300));
        jButton1.setMinimumSize(new java.awt.Dimension(800, 600));

        fileMenu.setText("File");

        openMenuItem.setText("Open");
        fileMenu.add(openMenuItem);

        saveMenuItem.setText("Save");
        fileMenu.add(saveMenuItem);

        saveAsMenuItem.setText("Save As ...");
        fileMenu.add(saveAsMenuItem);

        exitMenuItem.setText("Exit");
        exitMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        editMenu.setText("Edit");

        cutMenuItem.setText("Cut");
        editMenu.add(cutMenuItem);

        copyMenuItem.setText("Copy");
        editMenu.add(copyMenuItem);

        pasteMenuItem.setText("Paste");
        editMenu.add(pasteMenuItem);

        deleteMenuItem.setText("Delete");
        editMenu.add(deleteMenuItem);

        menuBar.add(editMenu);

        helpMenu.setText("Help");

        contentsMenuItem.setText("Contents");
        helpMenu.add(contentsMenuItem);

        aboutMenuItem.setText("About");
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        jMenu1.setForeground(new java.awt.Color(204, 0, 0));
        jMenu1.setText("\u6a21\u5757");

        jMenuItemUser.setAccelerator(javax.swing.KeyStroke.getKeyStroke(
                java.awt.event.KeyEvent.VK_U,
                java.awt.event.InputEvent.CTRL_MASK));
        jMenuItemUser.setFont(new java.awt.Font("Dialog", 1, 14));
        jMenuItemUser.setForeground(new java.awt.Color(0, 204, 0));
        jMenuItemUser.setText("\u7528\u6237");
        jMenuItemUser.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemUserActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItemUser);

        jMenuItemBook.setAccelerator(javax.swing.KeyStroke.getKeyStroke(
                java.awt.event.KeyEvent.VK_B,
                java.awt.event.InputEvent.CTRL_MASK));
        jMenuItemBook.setFont(new java.awt.Font("Dialog", 1, 14));
        jMenuItemBook.setForeground(new java.awt.Color(0, 204, 51));
        jMenuItemBook.setText("\u56fe\u4e66");
        jMenuItemBook.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemBookActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItemBook);

        jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(
                java.awt.event.KeyEvent.VK_DELETE,
                java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem1.setFont(new java.awt.Font("Dialog", 1, 14));
        jMenuItem1.setForeground(new java.awt.Color(0, 204, 0));
        jMenuItem1.setText("\u6ce8\u9500");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        menuBar.add(jMenu1);

        setJMenuBar(menuBar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
                getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(
                javax.swing.GroupLayout.Alignment.LEADING).addComponent(
                jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 800,
                Short.MAX_VALUE));
        layout.setVerticalGroup(layout.createParallelGroup(
                javax.swing.GroupLayout.Alignment.LEADING).addComponent(
                jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 600,
                Short.MAX_VALUE));

        pack();
    }// 
    //GEN-END:initComponents

    /**
     * 注銷菜單項
     * @param evt
     */
    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {

    }

    /**
     * 監聽進入圖書模塊
     * @param evt
     */
    private void jMenuItemBookActionPerformed(java.awt.event.ActionEvent evt) {
        this.setContentPane(new BookListPanel(this));
        this.validate();
    }

    /**
     * 監聽進入用戶模塊
     * @param evt
     */
    private void jMenuItemUserActionPerformed(java.awt.event.ActionEvent evt) {
        this.setContentPane(new ListPanel(this));
        this.validate();

    }

    private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitMenuItemActionPerformed
        System.exit(0);
    }//GEN-LAST:event_exitMenuItemActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new BookStore().setVisible(true);
            }
        });
    }

    //GEN-BEGIN:variables
    // Variables declaration - do not modify
    private javax.swing.JMenuItem aboutMenuItem;
    private javax.swing.JMenuItem contentsMenuItem;
    private javax.swing.JMenuItem copyMenuItem;
    private javax.swing.JMenuItem cutMenuItem;
    private javax.swing.JMenuItem deleteMenuItem;
    private javax.swing.JMenu editMenu;
    private javax.swing.JMenuItem exitMenuItem;
    private javax.swing.JMenu fileMenu;
    private javax.swing.JMenu helpMenu;
    private javax.swing.JButton jButton1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItemBook;
    private javax.swing.JMenuItem jMenuItemUser;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JMenuItem openMenuItem;
    private javax.swing.JMenuItem pasteMenuItem;
    private javax.swing.JMenuItem saveAsMenuItem;
    private javax.swing.JMenuItem saveMenuItem;
    // End of variables declaration//GEN-END:variables

}

圖書模塊的所有代碼和類:

值對象層:

||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

BookModel類:

package cn.hncu.bookStore.book.vo;

import java.io.Serializable;

/**
 * 值對象封裝
 * 
 * @author 陳浩翔
 * @version 1.0
 */
public class BookModel implements Serializable{//必須實現這個接口
    private String uuid;//圖書ID
    private String name;//圖書名字
    private double inPrice;//圖書的進價
    private double salePrice;//圖書的售價

    /**
     * 
     * @return 圖書的ID
     */
    public String getUuid() {
        return uuid;
    }
    /**
     * 
     * @param uuid ---設置圖書的ID
     */
    public void setUuid(String uuid) {
        this.uuid = uuid;
    }

    /**
     * 
     * @return 圖書的名字
     */
    public String getName() {
        return name;
    }

    /**
     * 
     * @param name--設置圖書的名字
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 
     * @return 得到圖書的進價
     */
    public double getInPrice() {
        return inPrice;
    }

    /**
     * 
     * @param inPrice---設置圖書的進價
     */
    public void setInPrice(double inPrice) {
        this.inPrice = inPrice;
    }

    /**
     * 
     * @return ---返回圖書的售價
     */
    public double getSalePrice() {
        return salePrice;
    }

    /**
     * 
     * @param outPrice---設置圖書的售價
     */
    public void setSalePrice(double salePrice) {
        this.salePrice = salePrice;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((uuid == null) ? 0 : uuid.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        BookModel other = (BookModel) obj;
        if (uuid == null) {
            if (other.uuid != null)
                return false;
        } else if (!uuid.equals(other.uuid))
            return false;
        return true;
    }
    @Override
    public String toString() {
        return uuid + ", " + name + ", 進價="
                + inPrice + ", 售價" + salePrice;
    }

}

||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

BookQueryModel類:

package cn.hncu.bookStore.book.vo;

public class BookQueryModel extends BookModel{
    private double inPrice2;
    private double salePrice2;
    public double getInPrice2() {
        return inPrice2;
    }
    public void setInPrice2(double inPrice2) {
        this.inPrice2 = inPrice2;
    }
    public double getSalePrice2() {
        return salePrice2;
    }
    public void setSalePrice2(double salePrice2) {
        this.salePrice2 = salePrice2;
    }


}

||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

數據層:

BookDao接口

package cn.hncu.bookStore.book.dao.dao;

import java.util.List;

import cn.hncu.bookStore.book.vo.BookModel;
import cn.hncu.bookStore.book.vo.BookQueryModel;
/**
 * 
 * @author 陳浩翔
 *
 * @version 1.0
 * 圖書模塊的數據層接口
 */
public interface BookDao {

    /**
     * 功能:創建一本圖書
     * 
     * @param bookModel---將要創建的圖書數據
     * @return---true表示創建成功,false表示創建失敗
     */
    public boolean create(BookModel book);


    /**
     * 功能:根據圖書的唯一標識碼uuid刪除一本圖書
     * 
     * @param uuid---圖書唯一的標識碼,每本圖書都不會相同
     * @return---true表示刪除成功,false表示刪除失敗
     */
    public boolean delete(String uuid);


    /**
     * 功能:修改圖書的數據資料
     * 
     * @param user---需要修改的圖書數據參數名
     * @return 返回true-表示修改成功了,返回false-表示修改失敗
     */
    public boolean update(BookModel book);


    /**
     * 功能:得到所有的圖書數據
     * 
     * @return---一個BookModel集合,也就是圖書的數據
     */
    public List getAll();


    /**
     * 功能:按照一定的查找條件進行查找,
     * 

     * 把滿足查找條件的圖書數據返回。
     * 
     * @param bqm---被封裝的查找條件
     * @return---滿足查找條件的圖書數據集合
     */
    public List getbyCondition(BookQueryModel bqm);


    /**
     * 功能:得到一個確定的圖書的數據資料
     * 
     * @param uuid---圖書唯一標識碼
     * @return ---返回按這個唯一標識碼找到的圖書數據
     */
    public BookModel getSingle(String uuid);

}

|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

BookDaoSerImpl類:

package cn.hncu.bookStore.book.dao.impl;

import java.util.ArrayList;
import java.util.List;

import cn.hncu.bookStore.book.dao.dao.BookDao;
import cn.hncu.bookStore.book.vo.BookModel;
import cn.hncu.bookStore.book.vo.BookQueryModel;
import cn.hncu.bookStore.util.FileIoUtil;
/**
 * Dao實現類
 * @author 陳浩翔
 *
 * @version 1.0
 */
public class BookDaoSerImpl implements BookDao {
    private final String FILE_NAME = "Book.txt";

    @Override
    public boolean create(BookModel book) {
        List books = FileIoUtil.readFormFile(FILE_NAME);

        for (BookModel model : books) {
            if (model.getUuid().equals(book.getUuid())) {
                return false;
            }
        }
        books.add(book);
        FileIoUtil.write2file(books, FILE_NAME);
        return true;
    }

    @Override
    public boolean delete(String uuid) {
        List books = FileIoUtil.readFormFile(FILE_NAME);

        for (BookModel model : books) {
            if (model.getUuid().equals(uuid)) {
                books.remove(model);
                FileIoUtil.write2file(books, FILE_NAME);
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean update(BookModel book) {
        List books = FileIoUtil.readFormFile(FILE_NAME);

        for (int i=0;i getAll() {
        return FileIoUtil.readFormFile(FILE_NAME);
    }

    @Override
    public List getbyCondition(BookQueryModel bqm) {
        List lists = getAll();
        List results = new ArrayList();

        for(BookModel book: lists){

            if(bqm.getUuid()!=null&&bqm.getUuid().trim().length()>0){
                if(!book.getUuid().equals(bqm.getUuid())){
                    continue;
                }
            }

            if(bqm.getName()!=null&&bqm.getName().trim().length()>0){
                if(book.getName().indexOf(bqm.getName())==-1){
                    continue;
                }
            }

            if(bqm.getInPrice()>0){
                if(book.getInPrice()0){
                if(book.getInPrice()>bqm.getInPrice2()){
                    continue;
                }
            }

            if(bqm.getSalePrice()>0){
                if(book.getSalePrice()0){
                if(book.getSalePrice()>bqm.getSalePrice2()){
                    continue;
                }
            }

            results.add(book);
        }

        return results;
    }

    @Override
    public BookModel getSingle(String uuid) {
        List list = FileIoUtil.readFormFile(FILE_NAME);
        for(BookModel book:list){
            if(book.getUuid().equals(uuid)){
                return book;
            }
        }
        return null;
    }

}

|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

BookDaoFactory類:

package cn.hncu.bookStore.book.dao.factory;

import cn.hncu.bookStore.book.dao.dao.BookDao;
import cn.hncu.bookStore.book.dao.impl.BookDaoSerImpl;
/**
 * 工廠方法
 * new一個實例
 * @author 陳浩翔
 * @version 1.0
 */
public class BookDaoFactory {
    /**
     * 
     * @return BookDao接口的一個實例
     */
    public static BookDao getBookDao(){
        return new BookDaoSerImpl();
    }
}

|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

邏輯層:

BookEbi 接口:

package cn.hncu.bookStore.book.business.ebi;

import java.util.List;

import cn.hncu.bookStore.book.vo.BookModel;
import cn.hncu.bookStore.book.vo.BookQueryModel;

/**
 * 
 * @author 陳浩翔
 *
 * @version 1.0
 * 圖書模塊的邏輯層接口
 */
public interface BookEbi {

        /**
         * 功能:創建一本圖書
         * 
         * @param bookModel---將要創建的圖書數據
         * @return---true表示創建成功,false表示創建失敗
         */
        public boolean create(BookModel book);


        /**
         * 功能:根據圖書的唯一標識碼uuid刪除一本圖書
         * 
         * @param uuid---圖書唯一的標識碼,每本圖書都不會相同
         * @return---true表示刪除成功,false表示刪除失敗
         */
        public boolean delete(String uuid);


        /**
         * 功能:修改圖書的數據資料
         * 
         * @param user---需要修改的圖書數據參數名
         * @return 返回true-表示修改成功了,返回false-表示修改失敗
         */
        public boolean update(BookModel book);


        /**
         * 功能:得到所有的圖書數據
         * 
         * @return---一個BookModel集合,也就是圖書的數據
         */
        public List getAll();


        /**
         * 功能:按照一定的查找條件進行查找,
         * 

         * 把滿足查找條件的圖書數據返回。
         * 
         * @param bqm---被封裝的查找條件
         * @return---滿足查找條件的圖書數據集合
         */
        public List getbyCondition(BookQueryModel bqm);


        /**
         * 功能:得到一個確定的圖書的數據資料
         * 
         * @param uuid---圖書唯一標識碼
         * @return ---返回按這個唯一標識碼找到的圖書數據
         */
        public BookModel getSingle(String uuid);

    }

||||||||||||||||||||||||||||||||||||||||||||||||||

BookEbo 類:

package cn.hncu.bookStore.book.business.ebo;

import java.util.List;

import cn.hncu.bookStore.book.business.ebi.BookEbi;
import cn.hncu.bookStore.book.dao.dao.BookDao;
import cn.hncu.bookStore.book.dao.factory.BookDaoFactory;
import cn.hncu.bookStore.book.vo.BookModel;
import cn.hncu.bookStore.book.vo.BookQueryModel;
import cn.hncu.bookStore.common.UuidModelConstance;
import cn.hncu.bookStore.common.uuid.dao.factory.UuidDaoFactory;
import cn.hncu.bookStore.common.uuid.dao.impl.UuidDaoSerImpl;
/**
 * 實現類
 * @author 陳浩翔
 *
 * @version 1.0
 */
public class BookEbo implements BookEbi {
    //注入
    BookDao dao = BookDaoFactory.getBookDao();

    @Override
    public boolean create(BookModel book) {
        //如果表現層中對book對象的數據沒有封裝完整,那麼在這裡要為它補全
        //調用uuid模塊的dao層來自動獲取當前book對象的uuid
        String uuid = UuidDaoFactory.getUuidDao().getNextUuid(UuidModelConstance.BOOK);
        book.setUuid(uuid);
        return dao.create(book);
    }

    @Override
    public boolean delete(String uuid) {
        return dao.delete(uuid);
    }

    @Override
    public boolean update(BookModel book) {
        return dao.update(book);
    }

    @Override
    public List getAll() {
        return dao.getAll();
    }

    @Override
    public List getbyCondition(BookQueryModel bqm) {
        return dao.getbyCondition(bqm);
    }

    @Override
    public BookModel getSingle(String uuid) {
        return dao.getSingle(uuid);
    }

}

||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

BookEbiFactory 類:

package cn.hncu.bookStore.book.business.factory;

import cn.hncu.bookStore.book.business.ebi.BookEbi;
import cn.hncu.bookStore.book.business.ebo.BookEbo;
/**
 * 工廠方法
 *new 一個實例
 * @author 陳浩翔
 * @version 1.0
 */
public class BookEbiFactory {

    public static BookEbi getBookEbi(){
        return new BookEbo();
    }
}

表現層:

BookListPanel 類:圖書列表界面

/*
 * BookListPanel.java
 *
 * Created on __DATE__, __TIME__
 */

package cn.hncu.bookStore.book.ui;

import java.util.List;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

import cn.hncu.bookStore.book.business.factory.BookEbiFactory;
import cn.hncu.bookStore.book.vo.BookModel;

/**
 *
 * @author  __USER__
 */
public class BookListPanel extends javax.swing.JPanel {
    private JFrame mainFrame = null;

    /** Creates new form BookListPanel */
    public BookListPanel(JFrame mainFrame) {
        this.mainFrame = mainFrame;
        initComponents();
        myInitData();
    }

    public BookListPanel(JFrame mainFrame, List results) {
        this.mainFrame=mainFrame;
        initComponents();
        bookLists.setListData(results.toArray());
    }

    private void myInitData() {
        List lists = BookEbiFactory.getBookEbi().getAll();
        bookLists.setListData(lists.toArray());

    }

    //GEN-BEGIN:initComponents
    // 
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        bookLists = new javax.swing.JList();
        jLabel1 = new javax.swing.JLabel();
        btnToAdd = new javax.swing.JButton();
        btnToDelete = new javax.swing.JButton();
        btnToUpdate = new javax.swing.JButton();
        btnToQuery = new javax.swing.JButton();

        setMinimumSize(new java.awt.Dimension(800, 600));
        setPreferredSize(new java.awt.Dimension(800, 600));
        setLayout(null);

        bookLists.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "" };

            public int getSize() {
                return strings.length;
            }

            public Object getElementAt(int i) {
                return strings[i];
            }
        });
        jScrollPane1.setViewportView(bookLists);

        add(jScrollPane1);
        jScrollPane1.setBounds(170, 80, 480, 230);

        jLabel1.setFont(new java.awt.Font("Dialog", 1, 48));
        jLabel1.setForeground(new java.awt.Color(204, 0, 51));
        jLabel1.setText("\u56fe\u4e66\u5217\u8868");
        add(jLabel1);
        jLabel1.setBounds(300, 0, 260, 80);

        btnToAdd.setFont(new java.awt.Font("Dialog", 1, 24));
        btnToAdd.setForeground(new java.awt.Color(0, 102, 102));
        btnToAdd.setText("\u6dfb\u52a0\u56fe\u4e66");
        btnToAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnToAddActionPerformed(evt);
            }
        });
        add(btnToAdd);
        btnToAdd.setBounds(160, 350, 150, 50);

        btnToDelete.setFont(new java.awt.Font("Dialog", 1, 24));
        btnToDelete.setForeground(new java.awt.Color(0, 102, 102));
        btnToDelete.setText("\u5220\u9664\u56fe\u4e66");
        btnToDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnToDeleteActionPerformed(evt);
            }
        });
        add(btnToDelete);
        btnToDelete.setBounds(510, 350, 150, 50);

        btnToUpdate.setFont(new java.awt.Font("Dialog", 1, 24));
        btnToUpdate.setForeground(new java.awt.Color(0, 102, 102));
        btnToUpdate.setText("\u4fee\u6539\u56fe\u4e66");
        btnToUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnToUpdateActionPerformed(evt);
            }
        });
        add(btnToUpdate);
        btnToUpdate.setBounds(160, 450, 150, 50);

        btnToQuery.setFont(new java.awt.Font("Dialog", 1, 24));
        btnToQuery.setForeground(new java.awt.Color(0, 102, 102));
        btnToQuery.setText("\u67e5\u627e\u56fe\u4e66");
        btnToQuery.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnToQueryActionPerformed(evt);
            }
        });
        add(btnToQuery);
        btnToQuery.setBounds(510, 450, 150, 50);
    }// 
    //GEN-END:initComponents

    private void btnToQueryActionPerformed(java.awt.event.ActionEvent evt) {
        mainFrame.setContentPane(new BookQueryPanel(mainFrame));
        mainFrame.validate();
    }

    private void btnToUpdateActionPerformed(java.awt.event.ActionEvent evt) {
        BookModel book = (BookModel) bookLists.getSelectedValue();
        if(book==null){
            JOptionPane.showMessageDialog(mainFrame, "請選擇要修改的圖書!");
            return ;
        }
        String uuid  = book.getUuid();

        mainFrame.setContentPane(new BookUpdatePanel(mainFrame,uuid));
        mainFrame.validate();


    }

    private void btnToDeleteActionPerformed(java.awt.event.ActionEvent evt) {
        BookModel book = (BookModel) bookLists.getSelectedValue();
        //System.out.println(book);
        if(book==null){
            JOptionPane.showMessageDialog(mainFrame, "請選擇要刪除的圖書!");
            return ;
        }
        String uuid  = book.getUuid();

        mainFrame.setContentPane(new BookDeletePanel(mainFrame,uuid));
        mainFrame.validate();
    }

    private void btnToAddActionPerformed(java.awt.event.ActionEvent evt) {
        mainFrame.setContentPane(new BookAddPanel(mainFrame));
        mainFrame.validate();
    }

    //GEN-BEGIN:variables
    // Variables declaration - do not modify
    private javax.swing.JList bookLists;
    private javax.swing.JButton btnToAdd;
    private javax.swing.JButton btnToDelete;
    private javax.swing.JButton btnToQuery;
    private javax.swing.JButton btnToUpdate;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

}

||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

BookAddPanel 類:添加界面:

/*
 * BookAddPanel.java
 *
 * Created on __DATE__, __TIME__
 */

package cn.hncu.bookStore.book.ui;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

import cn.hncu.bookStore.book.business.ebi.BookEbi;
import cn.hncu.bookStore.book.business.factory.BookEbiFactory;
import cn.hncu.bookStore.book.vo.BookModel;

/**
 * 
 * @author 陳浩翔
 *
 * @version 1.0
 */
public class BookAddPanel extends javax.swing.JPanel {
    private JFrame mainFrame = null;

    /** Creates new form BookAddPanel 
     * @param mainFrame */
    public BookAddPanel(JFrame mainFrame) {
        this.mainFrame = mainFrame;
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    //GEN-BEGIN:initComponents
    // 
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        tfdName = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        btnAdd = new javax.swing.JButton();
        btnBack = new javax.swing.JButton();
        tfdInPrice = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        tfdSalePrice = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();

        setMinimumSize(new java.awt.Dimension(800, 600));
        setLayout(null);

        jLabel1.setFont(new java.awt.Font("微軟雅黑", 1, 48));
        jLabel1.setForeground(new java.awt.Color(204, 0, 0));
        jLabel1.setText("\u6dfb\u52a0\u56fe\u4e66");
        add(jLabel1);
        jLabel1.setBounds(280, 40, 230, 80);

        tfdName.setFont(new java.awt.Font("Dialog", 1, 18));
        tfdName.setAutoscrolls(false);
        add(tfdName);
        tfdName.setBounds(280, 170, 150, 30);

        jLabel4.setFont(new java.awt.Font("微軟雅黑", 0, 18));
        jLabel4.setText("\u56fe\u4e66\u540d\u79f0:");
        add(jLabel4);
        jLabel4.setBounds(180, 170, 90, 30);

        btnAdd.setFont(new java.awt.Font("Dialog", 1, 24));
        btnAdd.setForeground(new java.awt.Color(0, 204, 204));
        btnAdd.setText("\u6dfb\u52a0");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });
        add(btnAdd);
        btnAdd.setBounds(140, 440, 120, 60);

        btnBack.setFont(new java.awt.Font("Dialog", 1, 24));
        btnBack.setForeground(new java.awt.Color(0, 204, 204));
        btnBack.setText("\u8fd4\u56de");
        btnBack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBackActionPerformed(evt);
            }
        });
        add(btnBack);
        btnBack.setBounds(520, 440, 120, 60);

        tfdInPrice.setFont(new java.awt.Font("Dialog", 1, 18));
        tfdInPrice.setAutoscrolls(false);
        add(tfdInPrice);
        tfdInPrice.setBounds(280, 250, 150, 30);

        jLabel5.setFont(new java.awt.Font("微軟雅黑", 0, 18));
        jLabel5.setText("\u8fdb\u8d27\u4ef7\u683c\uff1a");
        add(jLabel5);
        jLabel5.setBounds(180, 250, 90, 30);

        tfdSalePrice.setFont(new java.awt.Font("Dialog", 1, 18));
        tfdSalePrice.setAutoscrolls(false);
        add(tfdSalePrice);
        tfdSalePrice.setBounds(280, 330, 150, 30);

        jLabel6.setFont(new java.awt.Font("微軟雅黑", 0, 18));
        jLabel6.setText("\u552e\u8d27\u4ef7\u683c\uff1a");
        add(jLabel6);
        jLabel6.setBounds(180, 330, 90, 30);
    }// 
    //GEN-END:initComponents

    private void btnBackActionPerformed(java.awt.event.ActionEvent evt) {
        back();
    }

    private void back() {
        mainFrame.setContentPane(new BookListPanel(mainFrame));
        mainFrame.validate();
    }

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {
        //1收集參數
        String name = tfdName.getText();
        double inPrice = 0;
        double salePrice = 0;
        try {
            inPrice = Double.parseDouble(tfdInPrice.getText());
            salePrice = Double.parseDouble(tfdSalePrice.getText());
        } catch (NumberFormatException e1) {
            JOptionPane.showMessageDialog(mainFrame, "價格的格式錯誤,請重新輸入!");
            return;
        }


        if (name.equals("") || name.equals(null)) {
            JOptionPane.showMessageDialog(mainFrame, "用戶名為空,請重新輸入!");
            return;
        }

        //2組織參數
        BookModel book = new BookModel();
        book.setName(name);
        book.setInPrice(inPrice);
        book.setSalePrice(salePrice);

        //3調用邏輯層
        BookEbi ebi = BookEbiFactory.getBookEbi();

        //4根據調用返回結果導向不同頁面
        if (ebi.create(book)) {
            back();
        } else {
            JOptionPane.showMessageDialog(null, "該圖書已經存在!");
        }
    }

    //GEN-BEGIN:variables
    // Variables declaration - do not modify
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnBack;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JTextField tfdInPrice;
    private javax.swing.JTextField tfdName;
    private javax.swing.JTextField tfdSalePrice;
    // End of variables declaration//GEN-END:variables

}

||||||||||||||||||||||||||||||||||||||||||||||||||||||

BookDeletePanel 類:刪除界面:

/*
 * BookDeletePanel.java
 *
 * Created on __DATE__, __TIME__
 */

package cn.hncu.bookStore.book.ui;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

import cn.hncu.bookStore.book.business.ebi.BookEbi;
import cn.hncu.bookStore.book.business.factory.BookEbiFactory;
import cn.hncu.bookStore.book.vo.BookModel;

/**
 * 
 * @author 陳浩翔
 *
 * @version 1.0
 */
public class BookDeletePanel extends javax.swing.JPanel {
    private JFrame mainFrame = null;
    private String uuid = null;

    /** Creates new form BookDeletePanel 
     * @param mainFrame 
     * @param uuid */
    public BookDeletePanel(JFrame mainFrame, String uuid) {
        this.mainFrame = mainFrame;
        this.uuid = uuid;
        initComponents();
        myInitData();
    }

    private void myInitData() {
        BookModel book = BookEbiFactory.getBookEbi().getSingle(uuid);
        //System.out.println(book);
        tfdName.setText(book.getName());
        tfdName.setEditable(false);

        tfdUuid.setText(book.getUuid());
        tfdUuid.setEditable(false);

        tfdInPrice.setText(String.valueOf(book.getInPrice()));
        tfdInPrice.setEditable(false);

        tfdSalePrice.setText(String.valueOf(book.getSalePrice()));
        tfdSalePrice.setEditable(false);

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    //GEN-BEGIN:initComponents
    // 
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        tfdName = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        tfdUuid = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        btnDelete = new javax.swing.JButton();
        btnBack = new javax.swing.JButton();
        tfdInPrice = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        tfdSalePrice = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();

        setMinimumSize(new java.awt.Dimension(800, 600));
        setLayout(null);

        jLabel1.setFont(new java.awt.Font("微軟雅黑", 1, 48));
        jLabel1.setForeground(new java.awt.Color(204, 0, 0));
        jLabel1.setText("\u5220\u9664\u56fe\u4e66");
        add(jLabel1);
        jLabel1.setBounds(280, 40, 230, 80);

        tfdName.setFont(new java.awt.Font("Dialog", 1, 18));
        tfdName.setAutoscrolls(false);
        add(tfdName);
        tfdName.setBounds(480, 190, 150, 30);

        jLabel3.setFont(new java.awt.Font("微軟雅黑", 0, 18));
        jLabel3.setText("uuid:");
        add(jLabel3);
        jLabel3.setBounds(130, 190, 50, 30);

        tfdUuid.setFont(new java.awt.Font("Dialog", 1, 12));
        add(tfdUuid);
        tfdUuid.setBounds(200, 190, 110, 30);

        jLabel4.setFont(new java.awt.Font("微軟雅黑", 0, 18));
        jLabel4.setText("\u56fe\u4e66\u540d\u79f0:");
        add(jLabel4);
        jLabel4.setBounds(380, 190, 90, 30);

        btnDelete.setFont(new java.awt.Font("Dialog", 1, 24));
        btnDelete.setForeground(new java.awt.Color(0, 204, 204));
        btnDelete.setText("\u5220\u9664");
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });
        add(btnDelete);
        btnDelete.setBounds(140, 440, 120, 60);

        btnBack.setFont(new java.awt.Font("Dialog", 1, 24));
        btnBack.setForeground(new java.awt.Color(0, 204, 204));
        btnBack.setText("\u8fd4\u56de");
        btnBack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBackActionPerformed(evt);
            }
        });
        add(btnBack);
        btnBack.setBounds(520, 440, 120, 60);

        tfdInPrice.setFont(new java.awt.Font("Dialog", 1, 18));
        tfdInPrice.setAutoscrolls(false);
        add(tfdInPrice);
        tfdInPrice.setBounds(200, 300, 150, 30);

        jLabel5.setFont(new java.awt.Font("微軟雅黑", 0, 18));
        jLabel5.setText("\u8fdb\u8d27\u4ef7\u683c\uff1a");
        add(jLabel5);
        jLabel5.setBounds(100, 300, 90, 30);

        tfdSalePrice.setFont(new java.awt.Font("Dialog", 1, 18));
        tfdSalePrice.setAutoscrolls(false);
        add(tfdSalePrice);
        tfdSalePrice.setBounds(480, 300, 150, 30);

        jLabel6.setFont(new java.awt.Font("微軟雅黑", 0, 18));
        jLabel6.setText("\u552e\u8d27\u4ef7\u683c\uff1a");
        add(jLabel6);
        jLabel6.setBounds(380, 300, 90, 30);
    }// 
    //GEN-END:initComponents

    private void btnBackActionPerformed(java.awt.event.ActionEvent evt) {
        back();
    }

    private void back() {
        mainFrame.setContentPane(new BookListPanel(mainFrame));
        mainFrame.validate();
    }

    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {
        //1收集參數
        //2組織參數
        //3調用邏輯層

        //4根據調用返回結果導向不同頁面
        boolean isFlag = BookEbiFactory.getBookEbi().delete(uuid);
        if (isFlag) {
            back();
        } else {
            JOptionPane.showMessageDialog(null, "該圖書已經不存在!");
        }
    }

    //GEN-BEGIN:variables
    // Variables declaration - do not modify
    private javax.swing.JButton btnBack;
    private javax.swing.JButton btnDelete;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JTextField tfdInPrice;
    private javax.swing.JTextField tfdName;
    private javax.swing.JTextField tfdSalePrice;
    private javax.swing.JTextField tfdUuid;
    // End of variables declaration//GEN-END:variables

}

|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

BookQueryPanel 類:查詢界面:

/*
 * BookQueryPanel.java
 *
 * Created on __DATE__, __TIME__
 */

package cn.hncu.bookStore.book.ui;

import java.awt.event.ActionEvent;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

import cn.hncu.bookStore.book.business.ebi.BookEbi;
import cn.hncu.bookStore.book.business.factory.BookEbiFactory;
import cn.hncu.bookStore.book.vo.BookModel;
import cn.hncu.bookStore.book.vo.BookQueryModel;

/**
 * 
 * @author 陳浩翔
 *
 * @version 1.0
 */
public class BookQueryPanel extends javax.swing.JPanel {
    private JFrame mainFrame = null;

    /** Creates new form BookQueryPanel 
     * @param mainFrame */
    public BookQueryPanel(JFrame mainFrame) {
        this.mainFrame = mainFrame;
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    //GEN-BEGIN:initComponents
    // 
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        tfdName = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        tfdUuid = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        btnQuery = new javax.swing.JButton();
        btnBack = new javax.swing.JButton();
        tfdInPrice = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        tfdSalePrice = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        tfdInPrice2 = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        tfdSalePrice2 = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();

        setMinimumSize(new java.awt.Dimension(800, 600));
        setLayout(null);

        jLabel1.setFont(new java.awt.Font("微軟雅黑", 1, 48));
        jLabel1.setForeground(new java.awt.Color(204, 0, 0));
        jLabel1.setText("\u67e5\u627e\u56fe\u4e66");
        add(jLabel1);
        jLabel1.setBounds(280, 40, 230, 80);

        tfdName.setFont(new java.awt.Font("Dialog", 1, 18));
        tfdName.setAutoscrolls(false);
        add(tfdName);
        tfdName.setBounds(520, 190, 150, 30);

        jLabel3.setFont(new java.awt.Font("微軟雅黑", 0, 18));
        jLabel3.setText("uuid:");
        add(jLabel3);
        jLabel3.setBounds(130, 190, 50, 30);

        tfdUuid.setFont(new java.awt.Font("Dialog", 1, 12));
        add(tfdUuid);
        tfdUuid.setBounds(200, 190, 150, 30);

        jLabel4.setFont(new java.awt.Font("微軟雅黑", 0, 18));
        jLabel4.setText("\u56fe\u4e66\u540d\u79f0:");
        add(jLabel4);
        jLabel4.setBounds(420, 190, 90, 30);

        btnQuery.setFont(new java.awt.Font("Dialog", 1, 24));
        btnQuery.setForeground(new java.awt.Color(0, 204, 204));
        btnQuery.setText("\u67e5\u627e");
        btnQuery.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnQueryActionPerformed(evt);
            }
        });
        add(btnQuery);
        btnQuery.setBounds(140, 470, 120, 60);

        btnBack.setFont(new java.awt.Font("Dialog", 1, 24));
        btnBack.setForeground(new java.awt.Color(0, 204, 204));
        btnBack.setText("\u8fd4\u56de");
        btnBack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBackActionPerformed(evt);
            }
        });
        add(btnBack);
        btnBack.setBounds(490, 470, 120, 60);

        tfdInPrice.setFont(new java.awt.Font("Dialog", 1, 18));
        tfdInPrice.setAutoscrolls(false);
        add(tfdInPrice);
        tfdInPrice.setBounds(200, 280, 150, 30);

        jLabel5.setFont(new java.awt.Font("微軟雅黑", 0, 18));
        jLabel5.setText("\u8fdb\u8d27\u6700\u4f4e\u4ef7\u683c\uff1a");
        add(jLabel5);
        jLabel5.setBounds(60, 280, 130, 30);

        tfdSalePrice.setFont(new java.awt.Font("Dialog", 1, 18));
        tfdSalePrice.setAutoscrolls(false);
        add(tfdSalePrice);
        tfdSalePrice.setBounds(200, 370, 150, 30);

        jLabel6.setFont(new java.awt.Font("微軟雅黑", 0, 18));
        jLabel6.setText("\u552e\u8d27\u6700\u4f4e\u4ef7\u683c\uff1a");
        add(jLabel6);
        jLabel6.setBounds(60, 370, 130, 30);

        tfdInPrice2.setFont(new java.awt.Font("Dialog", 1, 18));
        tfdInPrice2.setAutoscrolls(false);
        add(tfdInPrice2);
        tfdInPrice2.setBounds(520, 280, 150, 30);

        jLabel11.setFont(new java.awt.Font("微軟雅黑", 0, 18));
        jLabel11.setText("\u8fdb\u8d27\u6700\u9ad8\u4ef7\u683c\uff1a");
        add(jLabel11);
        jLabel11.setBounds(380, 280, 130, 30);

        tfdSalePrice2.setFont(new java.awt.Font("Dialog", 1, 18));
        tfdSalePrice2.setAutoscrolls(false);
        add(tfdSalePrice2);
        tfdSalePrice2.setBounds(520, 370, 150, 30);

        jLabel12.setFont(new java.awt.Font("微軟雅黑", 0, 18));
        jLabel12.setText("\u552e\u8d27\u6700\u9ad8\u4ef7\u683c\uff1a");
        add(jLabel12);
        jLabel12.setBounds(380, 370, 130, 30);
    }// 
    //GEN-END:initComponents

    protected void btnQueryActionPerformed(ActionEvent evt) {
        //1收集參數
        String uuid = tfdUuid.getText();
        String name = tfdName.getText();
        double inPrice = 0;
        double inPrice2 = 0;
        double salePrice = 0;
        double salePrice2 = 0;

        if (tfdInPrice.getText() != null
                && tfdInPrice.getText().trim().length() > 0) {
            try {
                inPrice = Double.parseDouble(tfdInPrice.getText());
            } catch (NumberFormatException e) {
                JOptionPane.showMessageDialog(mainFrame, "最低進價輸入格式錯誤!");
                return;
            }
        }

        if (tfdInPrice2.getText() != null
                && tfdInPrice2.getText().trim().length() > 0) {
            try {
                inPrice2 = Double.parseDouble(tfdInPrice2.getText());
            } catch (NumberFormatException e) {
                JOptionPane.showMessageDialog(mainFrame, "最高進價輸入格式錯誤!");
                return;
            }
        }

        if (tfdSalePrice.getText() != null
                && tfdSalePrice.getText().trim().length() > 0) {
            try {
                salePrice = Double.parseDouble(tfdSalePrice.getText());
            } catch (NumberFormatException e) {
                JOptionPane.showMessageDialog(mainFrame, "最低進價輸入格式錯誤!");
                return;
            }
        }

        if (tfdSalePrice2.getText() != null
                && tfdSalePrice2.getText().trim().length() > 0) {
            try {
                salePrice2 = Double.parseDouble(tfdSalePrice2.getText());
            } catch (NumberFormatException e) {
                JOptionPane.showMessageDialog(mainFrame, "最低進價輸入格式錯誤!");
                return;
            }
        }
        //2組織參數
        BookQueryModel bqm = new BookQueryModel();
        bqm.setUuid(uuid);
        bqm.setName(name);
        bqm.setInPrice(inPrice);
        bqm.setInPrice2(inPrice2);
        bqm.setSalePrice(salePrice);
        bqm.setSalePrice2(salePrice2);

        //3調用邏輯層
        BookEbi ebi = BookEbiFactory.getBookEbi();
        List results = ebi.getbyCondition(bqm);

        //4根據調用返回結果導向不同頁面
        mainFrame.setContentPane(new BookListPanel(mainFrame, results));
        mainFrame.validate();

    }

    private void btnBackActionPerformed(java.awt.event.ActionEvent evt) {
        back();
    }

    private void back() {
        mainFrame.setContentPane(new BookListPanel(mainFrame));
        mainFrame.validate();
    }

    //GEN-BEGIN:variables
    // Variables declaration - do not modify
    private javax.swing.JButton btnBack;
    private javax.swing.JButton btnQuery;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JTextField tfdInPrice;
    private javax.swing.JTextField tfdInPrice2;
    private javax.swing.JTextField tfdName;
    private javax.swing.JTextField tfdSalePrice;
    private javax.swing.JTextField tfdSalePrice2;
    private javax.swing.JTextField tfdUuid;
    // End of variables declaration//GEN-END:variables

}

|||||||||||||||||||||||||||||||||||||||||||||||||||||||

BookUpdatePanel類:修改界面:

/*
 * BookUpdatePanel.java
 *
 * Created on __DATE__, __TIME__
 */

package cn.hncu.bookStore.book.ui;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

import cn.hncu.bookStore.book.business.ebi.BookEbi;
import cn.hncu.bookStore.book.business.factory.BookEbiFactory;
import cn.hncu.bookStore.book.vo.BookModel;

/**
 * 
 * @author 陳浩翔
 *
 * @version 1.0
 */
public class BookUpdatePanel extends javax.swing.JPanel {
    private JFrame mainFrame = null;
    private String uuid = null;

    /** Creates new form BookUpdatePanel 
     * @param uuid 
     * @param mainFrame */
    public BookUpdatePanel(JFrame mainFrame, String uuid) {
        this.mainFrame = mainFrame;
        this.uuid = uuid;
        initComponents();
        myInitData();
    }

    private void myInitData() {
        BookModel book = BookEbiFactory.getBookEbi().getSingle(uuid);

        tfdUuid.setText(uuid);
        tfdUuid.setEditable(false);

        tfdName.setText(book.getName());
        tfdInPrice.setText(String.valueOf(book.getInPrice()));
        tfdSalePrice.setText(String.valueOf(book.getSalePrice()));

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    //GEN-BEGIN:initComponents
    // 
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        tfdName = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        tfdUuid = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        btnUpdata = new javax.swing.JButton();
        btnBack = new javax.swing.JButton();
        tfdInPrice = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        tfdSalePrice = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();

        setMinimumSize(new java.awt.Dimension(800, 600));
        setLayout(null);

        jLabel1.setFont(new java.awt.Font("微軟雅黑", 1, 48));
        jLabel1.setForeground(new java.awt.Color(204, 0, 0));
        jLabel1.setText("\u4fee\u6539\u56fe\u4e66");
        add(jLabel1);
        jLabel1.setBounds(280, 40, 230, 80);

        tfdName.setFont(new java.awt.Font("Dialog", 1, 18));
        tfdName.setAutoscrolls(false);
        add(tfdName);
        tfdName.setBounds(480, 190, 150, 30);

        jLabel3.setFont(new java.awt.Font("微軟雅黑", 0, 18));
        jLabel3.setText("uuid:");
        add(jLabel3);
        jLabel3.setBounds(130, 190, 50, 30);

        tfdUuid.setFont(new java.awt.Font("Dialog", 1, 12));
        add(tfdUuid);
        tfdUuid.setBounds(200, 190, 110, 30);

        jLabel4.setFont(new java.awt.Font("微軟雅黑", 0, 18));
        jLabel4.setText("\u56fe\u4e66\u540d\u79f0:");
        add(jLabel4);
        jLabel4.setBounds(380, 190, 90, 30);

        btnUpdata.setFont(new java.awt.Font("Dialog", 1, 24));
        btnUpdata.setForeground(new java.awt.Color(0, 204, 204));
        btnUpdata.setText("\u4fee\u6539");
        btnUpdata.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUpdataActionPerformed(evt);
            }
        });
        add(btnUpdata);
        btnUpdata.setBounds(140, 440, 120, 60);

        btnBack.setFont(new java.awt.Font("Dialog", 1, 24));
        btnBack.setForeground(new java.awt.Color(0, 204, 204));
        btnBack.setText("\u8fd4\u56de");
        btnBack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBackActionPerformed(evt);
            }
        });
        add(btnBack);
        btnBack.setBounds(520, 440, 120, 60);

        tfdInPrice.setFont(new java.awt.Font("Dialog", 1, 18));
        tfdInPrice.setAutoscrolls(false);
        add(tfdInPrice);
        tfdInPrice.setBounds(200, 300, 150, 30);

        jLabel5.setFont(new java.awt.Font("微軟雅黑", 0, 18));
        jLabel5.setText("\u8fdb\u8d27\u4ef7\u683c\uff1a");
        add(jLabel5);
        jLabel5.setBounds(100, 300, 90, 30);

        tfdSalePrice.setFont(new java.awt.Font("Dialog", 1, 18));
        tfdSalePrice.setAutoscrolls(false);
        add(tfdSalePrice);
        tfdSalePrice.setBounds(480, 300, 150, 30);

        jLabel6.setFont(new java.awt.Font("微軟雅黑", 0, 18));
        jLabel6.setText("\u552e\u8d27\u4ef7\u683c\uff1a");
        add(jLabel6);
        jLabel6.setBounds(380, 300, 90, 30);
    }// 
    //GEN-END:initComponents

    private void btnBackActionPerformed(java.awt.event.ActionEvent evt) {
        back();
    }

    private void back() {
        mainFrame.setContentPane(new BookListPanel(mainFrame));
        mainFrame.validate();
    }

    private void btnUpdataActionPerformed(java.awt.event.ActionEvent evt) {
        //1收集參數
        String name = tfdName.getText();
        double inPrice = 0;
        double salePrice = 0;
        try {
            inPrice = Double.parseDouble(tfdInPrice.getText());
            salePrice = Double.parseDouble(tfdSalePrice.getText());
        } catch (NumberFormatException e1) {
            JOptionPane.showMessageDialog(mainFrame, "價格的格式錯誤,請重新輸入!");
            return;
        }

        if (name.equals("") || name.equals(null)) {
            JOptionPane.showMessageDialog(mainFrame, "用戶名為空,請重新輸入!");
            return;
        }

        //2組織參數
        BookModel book = new BookModel();
        book.setName(name);
        book.setUuid(uuid);
        book.setInPrice(inPrice);
        book.setSalePrice(salePrice);

        //3調用邏輯層
        BookEbi ebi = BookEbiFactory.getBookEbi();

        //4根據調用返回結果導向不同頁面
        if (ebi.update(book)) {
            back();
        } else {
            JOptionPane.showMessageDialog(null, "該圖書已經存在!");
        }
    }

    //GEN-BEGIN:variables
    // Variables declaration - do not modify
    private javax.swing.JButton btnBack;
    private javax.swing.JButton btnUpdata;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JTextField tfdInPrice;
    private javax.swing.JTextField tfdName;
    private javax.swing.JTextField tfdSalePrice;
    private javax.swing.JTextField tfdUuid;
    // End of variables declaration//GEN-END:variables

}

工具類:

FileIoUtil 類:數據讀取:

package cn.hncu.bookStore.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;

/**
 * 用戶的公用數據讀取寫入類
 * @author 陳浩翔
 *
 * @version 1.0
 */
public class FileIoUtil {

    public FileIoUtil() {
    }


    /**
     *  從數據庫中讀取所有的數據並返回出來
     *  
     * @param fileName:(數據表對應的文件名字)
     * @return 所有表的記錄!
     */
    @SuppressWarnings("unchecked")//壓警告
    public static List readFormFile(String fileName){
        List list = new ArrayList();
        final File file = new File(fileName);

        ObjectInputStream in =null;
        if(!file.exists()){
            //JOptionPane.showMessageDialog(null, "數據表不存在!");
            return list;
        }
        try {
            in = new ObjectInputStream(new FileInputStream(fileName));
            try {
                list = (List) in.readObject();

            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            if(in!=null){
                try {
                    in.close();
                } catch (IOException e) {
                    throw new RuntimeException("數據庫關閉失敗");
                }
            }
        }
        return list;
    }


    /**
     * 寫入一個list集合進入數據文件fileName
     * 
     * @param list(需要存儲的數據集合)
     * @param fileName(寫入到哪個文件的文件名字)
     */
    public static void write2file(List list, String fileName){
        ObjectOutputStream out = null;

        try {
            out = new ObjectOutputStream(new FileOutputStream(fileName));
            out.writeObject(list);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            if(out!=null){
                try {
                    out.close();
                } catch (IOException e) {
                    throw new RuntimeException("數據庫關閉失敗!");
                }
            }
        }
    }

}

最後說明:

因為感覺實在是太長了,我系列四又分了2個小模塊。
還有Uuid模塊和用戶模塊和公共類,我會在Java-單機版的書店管理系統(練習設計模塊和思想_系列 四(2) )貼出全部代碼。

   

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved