s1(java)项目实战(固定资产管理)(set_ Asset_manager_set )

来源:互联网 发布:淘宝logo设计理念 编辑:程序博客网 时间:2024/04/30 19:24

package set;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2007</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import tools.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;

public class Asset_manager_set extends JPanel {
    private JButton Asset_manager_set_modi = new JButton("修改");
    private JButton Asset_manager_set_add = new JButton("增加");
    private JButton Asset_manager_set_del = new JButton("删除");
    private TableModel tmd;
    private String user;
    private HashMap mapManager = new HashMap();
    private HashMap mapTemp = new HashMap();
    private DBConnection dcon = null;
    private JButton btn_return = new JButton();
    private JScrollPane table_pan = new JScrollPane();
    private Mytable model;
    private JTable table;
    private String tempID = "";

    public Asset_manager_set() {
        try {
            jbInit();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private void jbInit() throws Exception {
        this.setSize(new Dimension(500, 400));
        this.setLayout(null);
        Asset_manager_set_modi.setBounds(new Rectangle(154, 33, 79, 38));
        Asset_manager_set_add.setBounds(new Rectangle(46, 33, 79, 38));
        Asset_manager_set_del.setBounds(new Rectangle(273, 35, 79, 38));
        btn_return.setBounds(new Rectangle(375, 35, 79, 35));
        btn_return.setToolTipText("");
        btn_return.setText("返回");
        table_pan.setBounds(new Rectangle(39, 105, 430, 244));
        this.add(Asset_manager_set_add);
        this.add(Asset_manager_set_modi);
        this.add(Asset_manager_set_del);
        this.add(btn_return);
        this.add(table_pan);
        model = new Mytable("admin");
        model.setTitle(getTitle());
        model.setContent(getContents());
        table = new JTable(model);
        table.setFont(new Font("宋体", 1, 17));
        table.setRowSelectionAllowed(false);
        table_pan.getViewport().add(table);
        tmd = table.getModel();
        initmapTemp();
        //表格编辑变更
        tmd.addTableModelListener(new TableModelListener() {
            public void tableChanged(TableModelEvent e) {
                String id = table.getValueAt(table.getSelectedRow(), 0).
                            toString();
                String Tname = table.getValueAt(table.getSelectedRow(), 1).
                               toString();
                String Tpass = table.getValueAt(table.getSelectedRow(), 2).
                               toString();
                Manager m2 = new Manager(Tname, Tpass);
                Manager mtemp = returnTempManager(id);
                if (!Tname.equals(mtemp.getName()) ||
                    !Tpass.equals(mtemp.getPass())) {
                    if (mapManager.containsKey(id)) {
                        Set set = mapManager.entrySet();
                        Iterator it = set.iterator();
                        while (it.hasNext()) {
                            Map.Entry me = (Map.Entry) it.next();
                            if (me.getKey().toString().trim().equals(id)) {
                                me.setValue(m2);
                                break;
                            }
                        }
                    } else {
                        mapManager.put(id, m2);
                    }
                    modiTempManager(id, m2);
                }
            }

            public void editingCanceled(ChangeEvent e) {
            }
        });
        //表格行选择的变动
        table.addMouseListener(new MouseListener() {
            public void mouseClicked(MouseEvent e) {
                if (table.getSelectedColumn() != -1 &&
                    table.getSelectedRow() != -1) {
                    tempID = table.getValueAt(table.getSelectedRow(), 0).
                             toString().trim();
                    user = table.getValueAt(table.getSelectedRow(), 1).
                           toString().trim();
                } else {
                    tempID = "";
                    user = "";
                }
            }

            public void mousePressed(MouseEvent e) {
            }

            public void mouseReleased(MouseEvent e) {
            }

            public void mouseEntered(MouseEvent e) {
            }

            public void mouseExited(MouseEvent e) {
            }
        });
        //修改记录
        Asset_manager_set_modi.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (mapManager.size() == 0) {
                    JOptionPane.showMessageDialog(table_pan, "数据没有修改");
                } else {
                    dcon = new DBConnection();
                    Set set = mapManager.entrySet();
                    Iterator it = set.iterator();
                    int i = 0;
                    while (it.hasNext()) {
                        i++;
                        Map.Entry me = (Map.Entry) it.next();
                        String id = me.getKey().toString();
                        String name = ((Manager) me.getValue()).getName();
                        String pass = ((Manager) me.getValue()).getPass();
                        String sql = "update Manager set manager_password = '" +
                                     pass + "',manager_username= '" + name +
                                     "'  where  manager_id = '" + id + "'";
                        dcon.update(1, sql);
                    }
                    JOptionPane.showMessageDialog(table_pan, "你修改了" + i + "个记录");
                    mapManager.clear();
                    initmapTemp();
                }
            }
        });
        //删除记录
        Asset_manager_set_del.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (tempID.trim().length() == 0) {
                    JOptionPane.showMessageDialog(table_pan, "你没有选择要删除的记录");
                } else {
                    dcon = new DBConnection();
                    String sql = "select * from Action where manager_id='" +
                                 tempID + "'";
                    if (!dcon.isNull(sql)) {
                        sql = "delete from Manager  where  manager_id = '" +
                              tempID + "'";
                        dcon.update(1, sql);
                        JOptionPane.showMessageDialog(table_pan,
                                "你成功的删除了ID为" + tempID + "的操作员");
                        tempID = "";
                        model.setContent(getContents());
                        table.updateUI();
                    } else {
                        if (JOptionPane.showConfirmDialog(table_pan, "是否要继续操作",
                                "admin",
                                JOptionPane.YES_NO_OPTION) ==
                            JOptionPane.NO_OPTION) {
                            tempID = "";
                        } else {
                            deleteManager dm = new deleteManager(user);
                            Dimension frmsize = getSize();
                            Point loc = getLocation();
                            dm.setLocation((frmsize.width - dm.WIDTH) / 2 +
                                           loc.x,
                                           (frmsize.height - dm.HEIGHT) / 2 +
                                           loc.y);
                            dm.setSize(250, 250);
                            dm.setModal(true);
                            dm.setVisible(true);
                            model.setContent(getContents());
                            table.updateUI();
                            mapManager.clear();
                            initmapTemp();
                        }
                    }
                }
            }
        });
        //增加记录
        Asset_manager_set_add.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                ManagerADD ma = new ManagerADD();
                Dimension frmsize = getSize();
                Point loc = getLocation();
                ma.setLocation((frmsize.width - ma.WIDTH) / 2 + loc.x,
                               (frmsize.height - ma.HEIGHT) / 2 + loc.y);
                ma.setSize(250, 300);
                ma.setModal(true);
                ma.setVisible(true);
                model.setContent(getContents());
                table.updateUI();
                mapManager.clear();
                initmapTemp();
            }
        });
        btn_return.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                closeAsset_manager_set();
            }
        });
    }

    //修改临时的
    private void modiTempManager(String id, Manager m) {
        Set set = mapTemp.entrySet();
        Iterator it = set.iterator();
        while (it.hasNext()) {
            Map.Entry me = (Map.Entry) it.next();
            if (me.getKey().toString().equals(id)) {
                me.setValue(m);
                break;
            }
        }
    }

    //返回修改的
    private Manager returnTempManager(String id) {
        Set set = mapTemp.entrySet();
        Iterator it = set.iterator();
        Manager manager = null;
        while (it.hasNext()) {
            Map.Entry me = (Map.Entry) it.next();
            if (me.getKey().toString().equals(id)) {
                manager = (Manager) me.getValue();
                break;
            }
        }
        return manager;
    }

    //初始化临时表
    private void initmapTemp() {
        dcon = new DBConnection();
        String sql = "select * from Manager";
        Vector v = dcon.select(sql);
        for (int i = 0; i < v.size(); i++) {
            String id = ((Vector) v.get(i)).get(0).toString();
            String name = ((Vector) v.get(i)).get(1).toString();
            String pass = ((Vector) v.get(i)).get(2).toString();
            Manager m = new Manager(name, pass);
            mapTemp.put(id, m);
        }
    }

    //获取表格的列表
    private String[] getTitle() {
        dcon = new DBConnection();
        String sql = "select * from Manager";
        return dcon.getColumnname(sql);
    }

    //获取表格的内容
    private Vector getContents() {
        dcon = new DBConnection();
        String sql = "select * from Manager";
        return dcon.select(sql);
    }

    //关闭Asset_manager_set
    private void closeAsset_manager_set() {
        this.removeAll();
        Asset_OP asp = new Asset_OP(user);
        this.add(asp);
        this.updateUI();
    }
}
 

原创粉丝点击