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

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

編輯:JAVA綜合教程

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


介紹

今天對系列二的一個添加用戶的輸入小bug進行了解決,
增加了用戶模塊的,刪除用戶,和修改用戶模塊。

下面的代碼是相對於系列二來說進行了修改或者是增加的類.

先看AddPanel類的修改:
在收集參數的時候進行防范就可防止那個bug了。

cn.hncu.bookStore.user.ui;

AddPanel類:(添加用戶模塊)

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

package cn.hncu.bookStore.user.ui;

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

import cn.hncu.bookStore.common.UserTypeEnum;
import cn.hncu.bookStore.user.business.ebi.UserEbi;
import cn.hncu.bookStore.user.business.factory.UserEbiFactory;
import cn.hncu.bookStore.user.vo.UserModel;
import cn.hncu.bookStore.util.FileIoUtil;

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

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

    private void myInitData() {
        for (UserTypeEnum type : UserTypeEnum.values()) {
            combType.addItem(type.getName());
        }
    }

    /** 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();
        jLabel2 = new javax.swing.JLabel();
        tfdName = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        tfdUuid = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        tfdPwd2 = new javax.swing.JPasswordField();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        combType = new javax.swing.JComboBox();
        tfdPwd = new javax.swing.JPasswordField();
        btnAdd = new javax.swing.JButton();
        btnBack = new javax.swing.JButton();

        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\u7528\u6237");
        add(jLabel1);
        jLabel1.setBounds(330, 30, 230, 80);

        jLabel2.setFont(new java.awt.Font("微軟雅黑", 0, 18));
        jLabel2.setText("\u7528\u6237\u7c7b\u578b:");
        add(jLabel2);
        jLabel2.setBounds(90, 310, 90, 30);

        tfdName.setFont(new java.awt.Font("Dialog", 1, 18));
        tfdName.setAutoscrolls(false);
        add(tfdName);
        tfdName.setBounds(470, 160, 120, 30);

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

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

        jLabel4.setFont(new java.awt.Font("微軟雅黑", 0, 18));
        jLabel4.setText("\u59d3\u540d:");
        add(jLabel4);
        jLabel4.setBounds(410, 160, 50, 30);

        tfdPwd2.setFont(new java.awt.Font("宋體", 1, 18));
        add(tfdPwd2);
        tfdPwd2.setBounds(470, 240, 170, 30);

        jLabel5.setFont(new java.awt.Font("微軟雅黑", 0, 18));
        jLabel5.setText("\u5bc6\u7801:");
        add(jLabel5);
        jLabel5.setBounds(120, 240, 50, 30);

        jLabel6.setFont(new java.awt.Font("微軟雅黑", 0, 18));
        jLabel6.setText("\u786e\u8ba4\u5bc6\u7801:");
        add(jLabel6);
        jLabel6.setBounds(380, 240, 90, 30);

        combType.setFont(new java.awt.Font("Dialog", 1, 18));
        combType.setForeground(new java.awt.Color(51, 51, 255));
        combType.setModel(new javax.swing.DefaultComboBoxModel(
                new String[] { "請選擇..." }));
        add(combType);
        combType.setBounds(190, 310, 160, 30);

        tfdPwd.setFont(new java.awt.Font("宋體", 1, 18));
        add(tfdPwd);
        tfdPwd.setBounds(190, 240, 160, 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(190, 430, 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, 430, 120, 60);
    }// 
    //GEN-END:initComponents

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

    /**
     *監聽返回按鈕
     * @param 返回按鈕的點擊監聽
     */
    private void btnBackActionPerformed(java.awt.event.ActionEvent evt) {
        back();
    }

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {
        //1收集參數
        String uuid = tfdUuid.getText();
        String name = tfdName.getText();
        String pwd = new String(tfdPwd.getPassword());
        String pwd2 = new String(tfdPwd2.getPassword());
        //System.out.println(uuid);

        //System.out.println(uuid+","+name+","+pwd);
        if (uuid.equals("") || uuid.equals(null)) {
            JOptionPane.showMessageDialog(mainFrame, "用戶ID為空,請重新輸入!");
            return;
        }

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

        if (!pwd.equals(pwd2) || pwd.equals("") || pwd.equals(null)
                || pwd2.equals("") || pwd2.equals(null)) {
            JOptionPane.showMessageDialog(mainFrame, "兩次密碼輸入不一致或密碼為空,請重新輸入!");
            return;
        }
        int type = 0;

        try {
            type = UserTypeEnum.getTypeByName(combType.getSelectedItem()
                    .toString());
        } catch (Exception e) {
            JOptionPane.showMessageDialog(mainFrame, "請指定用戶類型!");
            return;
        }

        //2組織參數
        UserModel user = new UserModel();
        user.setName(name);
        user.setPwd(pwd);
        user.setType(type);
        user.setUuid(uuid);

        //3調用邏輯層
        UserEbi ebi = UserEbiFactory.getUserEbi();

        //4根據調用返回結果導向不同頁面
        if (ebi.create(user)) {
            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.JComboBox combType;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JTextField tfdName;
    private javax.swing.JPasswordField tfdPwd;
    private javax.swing.JPasswordField tfdPwd2;
    private javax.swing.JTextField tfdUuid;
    // End of variables declaration//GEN-END:variables

}

|
|
|

cn.hncu.bookStore.user.ui;

DeletePanel類:(刪除用戶模塊)

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

package cn.hncu.bookStore.user.ui;

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

import cn.hncu.bookStore.common.UserTypeEnum;
import cn.hncu.bookStore.user.business.ebi.UserEbi;
import cn.hncu.bookStore.user.business.factory.UserEbiFactory;
import cn.hncu.bookStore.user.vo.UserModel;
import cn.hncu.bookStore.util.FileIoUtil;

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

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

    private void myInitData() {
        UserEbi user = UserEbiFactory.getUserEbi();
        UserModel userModel = user.getSingle(uuid);
        tfdName.setText(userModel.getName());
        tfdUuid.setText(userModel.getUuid());
        tfdPwd.setText(userModel.getPwd());
        tfdType.setText(UserTypeEnum.getNameByType(userModel.getType()));

        tfdName.setEditable(false);
        tfdPwd.setEditable(false);
        tfdType.setEditable(false);
        tfdUuid.setEditable(false);

    }

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

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        tfdName = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        tfdUuid = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        tfdPwd = new javax.swing.JTextField();
        tfdType = new javax.swing.JTextField();
        btnBack = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();

        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\u7528\u6237");
        add(jLabel1);
        jLabel1.setBounds(330, 30, 230, 80);

        jLabel2.setFont(new java.awt.Font("微軟雅黑", 0, 18));
        jLabel2.setText("\u7528\u6237\u7c7b\u578b:");
        add(jLabel2);
        jLabel2.setBounds(100, 310, 90, 30);

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

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

        tfdUuid.setFont(new java.awt.Font("Dialog", 0, 11));
        add(tfdUuid);
        tfdUuid.setBounds(200, 160, 110, 30);

        jLabel4.setFont(new java.awt.Font("微軟雅黑", 0, 18));
        jLabel4.setText("\u59d3\u540d:");
        add(jLabel4);
        jLabel4.setBounds(420, 160, 50, 30);

        jLabel5.setFont(new java.awt.Font("微軟雅黑", 0, 18));
        jLabel5.setText("\u5bc6\u7801:");
        add(jLabel5);
        jLabel5.setBounds(130, 240, 50, 30);

        tfdPwd.setFont(new java.awt.Font("Tahoma", 1, 12));
        add(tfdPwd);
        tfdPwd.setBounds(200, 240, 160, 30);

        tfdType.setFont(new java.awt.Font("Dialog", 1, 12));
        add(tfdType);
        tfdType.setBounds(200, 310, 160, 30);

        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(540, 450, 120, 60);

        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(210, 450, 120, 60);
    }// 
    //GEN-END:initComponents

    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {
        //3調用邏輯層
        if (UserEbiFactory.getUserEbi().delete(uuid)) {
            back();
        } else {
            JOptionPane.showMessageDialog(mainFrame, "該用戶已經不存在!");
        }

        //4根據調用返回結果導向不同頁面

    }

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

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

    //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 jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JTextField tfdName;
    private javax.swing.JTextField tfdPwd;
    private javax.swing.JTextField tfdType;
    private javax.swing.JTextField tfdUuid;
    // End of variables declaration//GEN-END:variables

}

|
|
|

cn.hncu.bookStore.user.ui;

UpdatePanel類:(修改用戶模塊)

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

package cn.hncu.bookStore.user.ui;

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

import cn.hncu.bookStore.common.UserTypeEnum;
import cn.hncu.bookStore.user.business.ebi.UserEbi;
import cn.hncu.bookStore.user.business.factory.UserEbiFactory;
import cn.hncu.bookStore.user.vo.UserModel;

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

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

    private void myInitData() {
        UserEbi ebi = UserEbiFactory.getUserEbi();
        UserModel user = ebi.getSingle(uuid);
        tfdUuid.setText(user.getUuid());
        tfdUuid.setEditable(false);

        tfdName.setText(user.getName());
        tfdPwd.setText(user.getPwd());
        tfdPwd2.setText(user.getPwd());

        combType.removeAllItems();
        String usert = UserTypeEnum.getNameByType(user.getType());
        combType.addItem(usert);
        for (UserTypeEnum userType : UserTypeEnum.values()) {
            if (!userType.getName().equals(usert)) {
                combType.addItem(userType.getName());
            }
        }

    }

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

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        tfdName = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        tfdUuid = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        tfdPwd2 = new javax.swing.JPasswordField();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        tfdPwd = new javax.swing.JPasswordField();
        combType = new javax.swing.JComboBox();
        btnBack = new javax.swing.JButton();
        btnUpdate = new javax.swing.JButton();

        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\u7528\u6237");
        add(jLabel1);
        jLabel1.setBounds(320, 30, 230, 80);

        jLabel2.setFont(new java.awt.Font("微軟雅黑", 0, 18));
        jLabel2.setText("\u7528\u6237\u7c7b\u578b:");
        add(jLabel2);
        jLabel2.setBounds(90, 310, 90, 30);

        tfdName.setFont(new java.awt.Font("Dialog", 1, 18));
        tfdName.setAutoscrolls(false);
        add(tfdName);
        tfdName.setBounds(470, 160, 120, 30);

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

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

        jLabel4.setFont(new java.awt.Font("微軟雅黑", 0, 18));
        jLabel4.setText("\u59d3\u540d:");
        add(jLabel4);
        jLabel4.setBounds(410, 160, 50, 30);

        tfdPwd2.setFont(new java.awt.Font("Dialog", 1, 18));
        add(tfdPwd2);
        tfdPwd2.setBounds(470, 240, 170, 30);

        jLabel5.setFont(new java.awt.Font("微軟雅黑", 0, 18));
        jLabel5.setText("\u5bc6\u7801:");
        add(jLabel5);
        jLabel5.setBounds(120, 240, 50, 30);

        jLabel6.setFont(new java.awt.Font("微軟雅黑", 0, 18));
        jLabel6.setText("\u786e\u8ba4\u5bc6\u7801:");
        add(jLabel6);
        jLabel6.setBounds(380, 240, 90, 30);

        tfdPwd.setFont(new java.awt.Font("宋體", 1, 18));
        add(tfdPwd);
        tfdPwd.setBounds(190, 240, 160, 30);

        combType.setFont(new java.awt.Font("Dialog", 1, 12));
        combType.setForeground(new java.awt.Color(51, 0, 255));
        combType.setModel(new javax.swing.DefaultComboBoxModel(
                new String[] { "" }));
        add(combType);
        combType.setBounds(190, 310, 170, 30);

        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(500, 430, 120, 60);

        btnUpdate.setFont(new java.awt.Font("Dialog", 1, 24));
        btnUpdate.setForeground(new java.awt.Color(0, 204, 204));
        btnUpdate.setText("\u4fee\u6539");
        btnUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUpdateActionPerformed(evt);
            }
        });
        add(btnUpdate);
        btnUpdate.setBounds(170, 430, 120, 60);
    }// 
        // GEN-END:initComponents

    private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {
        // 1收集參數
        String uuid = tfdUuid.getText();
        String name = tfdName.getText();
        String pwd = new String(tfdPwd.getPassword());
        String pwd2 = new String(tfdPwd2.getPassword());

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

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

        if (!pwd.equals(pwd2) || pwd.equals("") || pwd.equals(null)
                || pwd2.equals("") || pwd2.equals(null)) {
            JOptionPane.showMessageDialog(mainFrame, "兩次密碼輸入不一致或密碼為空,請重新輸入!");
            return;
        }
        int type = 0;

        try {
            type = UserTypeEnum.getTypeByName(combType.getSelectedItem()
                    .toString());
        } catch (Exception e) {
            JOptionPane.showMessageDialog(mainFrame, "請指定用戶類型!");
            return;
        }

        // 2組織參數
        UserModel user = new UserModel();
        user.setName(name);
        user.setPwd(pwd);
        user.setType(type);
        user.setUuid(uuid);

        // 3調用邏輯層
        UserEbi ebi = UserEbiFactory.getUserEbi();

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

    }

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

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

    // GEN-BEGIN:variables
    // Variables declaration - do not modify
    private javax.swing.JButton btnBack;
    private javax.swing.JButton btnUpdate;
    private javax.swing.JComboBox combType;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JTextField tfdName;
    private javax.swing.JPasswordField tfdPwd;
    private javax.swing.JPasswordField tfdPwd2;
    private javax.swing.JTextField tfdUuid;
    // End of variables declaration//GEN-END:variables

}

|
|
|

cn.hncu.bookStore.user.ui;

ListPanel類(調用用戶功能模塊)

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

package cn.hncu.bookStore.user.ui;

import java.util.List;

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

import cn.hncu.bookStore.user.business.ebi.UserEbi;
import cn.hncu.bookStore.user.business.factory.UserEbiFactory;
import cn.hncu.bookStore.user.vo.UserModel;

/**
 * 表現層-用戶列表面板
 * 
 * @author 陳浩翔
 * @version 1.0
 */
public class ListPanel extends javax.swing.JPanel {
    private JFrame mainFrame = null;

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

    /**
     * 讀取所有用戶並添加進列表
     */
    private void myInitData() {
        UserEbi user = UserEbiFactory.getUserEbi();
        List list = user.getAll();
        userLists.setListData(list.toArray());
    }

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

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

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

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

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

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

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

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 48));
        jLabel1.setForeground(new java.awt.Color(204, 0, 51));
        jLabel1.setText("User List");
        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\u7528\u6237");
        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\u7528\u6237");
        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\u7528\u6237");
        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);
    }// 
    //GEN-END:initComponents

    private void btnToUpdateActionPerformed(java.awt.event.ActionEvent evt) {
        UserModel user = (UserModel) userLists.getSelectedValue();

        if (user == null) {
            JOptionPane.showMessageDialog(mainFrame, "請選擇要修改的用戶!");
            return;
        }

        String uuid = user.getUuid();

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

    private void btnToDeleteActionPerformed(java.awt.event.ActionEvent evt) {
        UserModel user = (UserModel) userLists.getSelectedValue();

        if (user == null) {
            JOptionPane.showMessageDialog(mainFrame, "請選擇要刪除的用戶!");
            return;
        }

        String uuid = user.getUuid();

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

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

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

}
   

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