TXT

AssetManager

By Vanessa Watson,2014-04-17 02:29
6 views 0
AssetManager

/*

     * w.java

     *

     * Created on __DATE__, __TIME__

     */

package ui;

    import java.text.SimpleDateFormat; import java.util.Date;

    import java.util.List;

    import javax.swing.DefaultComboBoxModel;

import entity.Asset;

    import entity.Cate;

    import entity.SubCate;

/**

     *

     * @author __USER__

     */

    public class AssetManager extends javax.swing.JPanel {

     //给主界面一个名字?以至于可以用这个名字行使调用主界面的一切内容的权力. :

     private Face face = null;

     /** Creates new form w

     * @param face */

     public AssetManager(Face face) {

     this.face = face;

     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() {

     btnGrp1 = new javax.swing.ButtonGroup();

     rdoAdd = new javax.swing.JRadioButton();

     rdoUpdate = new javax.swing.JRadioButton();

     rdoDel = new javax.swing.JRadioButton();

     jButton1 = new javax.swing.JButton();

     jLabel1 = new javax.swing.JLabel();

     txtDate = new javax.swing.JTextField();

     jLabel2 = new javax.swing.JLabel();

     txtName = new javax.swing.JTextField();

     jLabel3 = new javax.swing.JLabel();

     txtModel = new javax.swing.JTextField();

     jLabel4 = new javax.swing.JLabel();

     txtPrice = new javax.swing.JTextField();

     jLabel5 = new javax.swing.JLabel();

     txtId = new javax.swing.JTextField();

     jLabel6 = new javax.swing.JLabel();

     jLabel7 = new javax.swing.JLabel();

     jLabel8 = new javax.swing.JLabel();

     cboSubCate = new javax.swing.JComboBox();

     cboStatus = new javax.swing.JComboBox();

     cboCate = new javax.swing.JComboBox();

     setLayout(null);

     rdoAdd.setText("\u589e\u52a0");

     rdoAdd.addActionListener(new java.awt.event.ActionListener() {

     public void actionPerformed(java.awt.event.ActionEvent evt) {

     rdoAddActionPerformed(evt);

     }

     });

     add(rdoAdd);

     rdoAdd.setBounds(30, 20, 80, 25);

     rdoUpdate.setText("\u4fee\u6539");

     rdoUpdate.addActionListener(new java.awt.event.ActionListener() {

     public void actionPerformed(java.awt.event.ActionEvent evt) {

     rdoUpdateActionPerformed(evt);

     }

     });

     add(rdoUpdate);

     rdoUpdate.setBounds(130, 20, 110, 25);

     rdoDel.setText("\u5220\u9664");

     rdoDel.addActionListener(new java.awt.event.ActionListener() {

     public void actionPerformed(java.awt.event.ActionEvent evt) {

     rdoDelActionPerformed(evt);

     }

     });

     add(rdoDel);

     rdoDel.setBounds(240, 20, 70, 25);

     jButton1.setText("\u63d0\u4ea4");

     jButton1.addActionListener(new java.awt.event.ActionListener() {

     public void actionPerformed(java.awt.event.ActionEvent evt) {

     jButton1ActionPerformed(evt);

     }

     });

     add(jButton1);

     jButton1.setBounds(371, 20, 80, 25);

     jLabel1.setText("\u767b\u8bb0\u65e5\u671f");

     add(jLabel1);

     jLabel1.setBounds(260, 60, 80, 17);

     txtDate.setText(" ");

     add(txtDate);

     txtDate.setBounds(340, 60, 110, 23);

     jLabel2.setText("\u8d44\u4ea7\u540d\u79f0");

     add(jLabel2);

     jLabel2.setBounds(50, 100, 80, 20);

     txtName.setText(" ");

     add(txtName);

     txtName.setBounds(130, 100, 110, 20);

     jLabel3.setText("\u8d44\u4ea7\u6837\u5f0f");

     add(jLabel3);

     jLabel3.setBounds(50, 140, 80, 17);

     txtModel.setText(" ");

     add(txtModel);

     txtModel.setBounds(130, 140, 110, 23);

     jLabel4.setText("\u8d44\u4ea7\u4ef7\u683c");

     add(jLabel4);

     jLabel4.setBounds(50, 180, 80, 17);

     txtPrice.setText(" ");

     add(txtPrice);

     txtPrice.setBounds(130, 180, 110, 23);

     jLabel5.setText("\u8d44\u4ea7\u7f16\u53f7");

     add(jLabel5);

     jLabel5.setBounds(50, 60, 80, 17);

     txtId.setText(" ");

     add(txtId);

     txtId.setBounds(130, 60, 110, 23);

     jLabel6.setText("\u8d44\u4ea7\u72b6\u6001");

     add(jLabel6);

     jLabel6.setBounds(260, 100, 80, 17);

     jLabel7.setText("\u8d44\u4ea7\u5927\u7c7b");

     add(jLabel7);

     jLabel7.setBounds(260, 140, 80, 17);

     jLabel8.setText("\u8d44\u4ea7\u5c0f\u7c7b");

     add(jLabel8);

     jLabel8.setBounds(260, 180, 80, 17);

     cboSubCate.setModel(new javax.swing.DefaultComboBoxModel(new String[] {

     "Item 1", "Item 2", "Item 3", "Item 4" }));

     add(cboSubCate);

     cboSubCate.setBounds(340, 180, 110, 23);

     cboStatus.setModel(new javax.swing.DefaultComboBoxModel(new String[] {

     "Item 1", "Item 2", "Item 3", "Item 4" }));

     add(cboStatus);

     cboStatus.setBounds(340, 100, 110, 23);

     cboCate.setModel(new javax.swing.DefaultComboBoxModel(new String[] {

     "Item 1", "Item 2", "Item 3", "Item 4" }));

     cboCate.addActionListener(new java.awt.event.ActionListener() {

     public void actionPerformed(java.awt.event.ActionEvent evt) {

     cboCateActionPerformed(evt);

     }

     });

     add(cboCate);

     cboCate.setBounds(340, 140, 110, 23);

     add(cboCate);

     cboCate.setBounds(340, 140, 110, 23);

     cboStatus.setModel(new javax.swing.DefaultComboBoxModel(new String[] {

     "Item 1", "Item 2", "Item 3", "Item 4" }));

     add(cboStatus);

     cboStatus.setBounds(340, 100, 110, 23);

     btnGrp1.add(rdoAdd); btnGrp1.add(rdoUpdate); btnGrp1.add(rdoDel);

     createComboBox();

     }//

     //GEN-END:initComponents

     /*

     * add(cboCate);

     cboCate.setBounds(340, 140, 110, 23);

     cboStatus.setModel(new javax.swing.DefaultComboBoxModel(new String[] {

     "Item 1", "Item 2", "Item 3", "Item 4" }));

     add(cboStatus);

     cboStatus.setBounds(340, 100, 110, 23);

     btnGrp1.add(rdoAdd); btnGrp1.add(rdoUpdate); btnGrp1.add(rdoDel);

     createComboBox();

     */

     //点击确定

     private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

     // TODO add your handling code here:

     //拼装资产信息

     Asset a = new Asset();

     //通过各个text的方法得到其内容并且设置为资产a的内容

     a.setAsset_name(txtName.getText());

     a.setPrice(Double.parseDouble(txtPrice.getText()));

     a.setModel(txtModel.getText());

     a.setPurchase_date(txtDate.getText());

     a.setUseby("操作员");

     //取出下拉列表中的数据?注意有类型的转换

     //取出状态下拉列表的内容 更为字符串内容?并且取第一位。更为字符串是只取

    出一位的好做法

     char c = cboStatus.getSelectedItem().toString().charAt(0);

     //将取到的状态封装到资产表中

     a.setStatus(c);

     //在下拉列表中取出当前内容

     Cate cate = (Cate) cboCate.getSelectedItem();

     SubCate sub = (SubCate) cboSubCate.getSelectedItem();

     //将当前内容封装到资产表中

     a.setCateid(cate.getCateid());

     a.setSubcateid(sub.getSubcateid());

// if (!rdoAdd.isSelected()) {

// a.setAssetid(Long.parseLong(txtId.getText()));

    // }

    // if (rdoAdd.isSelected()) {

    // try {

    // face.adao.insert(a);

    // } catch (Exception e) {

    // // TODO Auto-generated catch block // e.printStackTrace();

    // }

    // } else if (rdoDel.isSelected()) { // try {

    // face.adao.delete(a);

    // } catch (Exception e) {

    // // TODO Auto-generated catch block // e.printStackTrace();

    // }

    // } else {

    // try {

    // face.adao.update(a);

    // } catch (Exception e) {

    // // TODO Auto-generated catch block // e.printStackTrace();

    // }

    // }

    // }

     if (!rdoAdd.isSelected()) {

     a.setAssetid(Long.parseLong(txtId.getText()));

     }

     //当是增加单选时

     if (rdoAdd.isSelected()) {

     try {

     //adaoinsert的方法插入所有的a的内容

     face.adao.insert(a);

     } catch (Exception e) {

     // TODO Auto-generated catch block

     e.printStackTrace();

     }

     }

     //当是更新单选时

     else if (rdoUpdate.isSelected()) {

     try {

     //adaoupdate的方法更新所有的a的内容

     face.adao.update(a);

     } catch (Exception e) {

     // TODO Auto-generated catch block

     e.printStackTrace();

     }

     }

     //当是删除单选时

     else {

     try {

     //adaodelete的方法删除a的所有的内容

     face.adao.delete(a);

     } catch (Exception e) {

     // TODO Auto-generated catch block

     e.printStackTrace();

     }

     }

     }

     //添加增加按钮

     private void rdoAddActionPerformed(java.awt.event.ActionEvent evt) {

     // TODO add your handling code here:

     this.txtId.setEditable(false);

     this.txtId.setText("");

     this.txtName.setText("");

     this.txtModel.setText("");

     this.txtDate.setText((new SimpleDateFormat("yyyy--MM--dd")

     .format(new Date())));

     this.txtPrice.setText("");

     this.cboStatus.setSelectedIndex(1);

     }

     //添加更新按钮

     private void rdoUpdateActionPerformed(java.awt.event.ActionEvent evt) {

     // TODO add your handling code here:

     //该内容不可再更改

     this.txtId.setEditable(false);

     }

     //添加删除按钮

     private void rdoDelActionPerformed(java.awt.event.ActionEvent evt) {

     }

     //大类改变?小类级联同步

     private void cboCateActionPerformed(java.awt.event.ActionEvent evt) {

     // TODO add your handling code here:

     //得到大类的内容

     Cate c = (Cate) cboCate.getSelectedItem();

     //大类中一个通过大类的id从而大类找小类的方法,得到小类

     List subcatelist;

     try {

     subcatelist = face.subdao.selectByCate(c.getCateid());

     //新建数据模型作为小类的cbobox

     DefaultComboBoxModel model = new DefaultComboBoxModel();

     //取出得到的小类中的每一个数据作为元素加入model

     for (SubCate s : subcatelist) {

     model.addElement(s);

     }

     //构造得出小类的所有内容

     cboSubCate.setModel(model);

     } catch (Exception e) {

     // TODO Auto-generated catch block

     e.printStackTrace();

     }

     }

     //构建状态和大类下拉列表

     private void createComboBox() {

     //为状态下拉框设置内容

     cboStatus.setModel(new DefaultComboBoxModel(new String[] { "0", "1" }));

     //为大类下拉框设置内容

     try {

     //通过selectAll方法得到主界面cdao大类的内容

     List catelist;

     catelist = face.cdao.selectAll();

     //新建数据模型

     DefaultComboBoxModel model = new DefaultComboBoxModel();

     //取出所有从大类得到的内容作为model的元素依次加入

     for (Cate c : catelist) {

     model.addElement(c);

     }

     cboCate.setModel(model);

     } catch (Exception e) {

     // TODO Auto-generated catch block

     e.printStackTrace();

     }

     //大类中默认选项为第一项

     cboCate.setSelectedIndex(0);

     }

     //当主界面点击表格时?获得数据?传递进来设置本界面的数据,该方法

     //传递了一个参数资产Asset?信息通过该参数传递

     public void setValue(Asset a) {

     //每个框的内容都设置为a得到的内容

     this.txtId.setText(a.getAssetid() + "");

     this.txtName.setText(a.getAsset_name());

     this.txtModel.setText(a.getModel());

     this.txtDate.setText(a.getPurchase_date());

     this.txtPrice.setText(a.getPrice()+ "");

     //得到状态

     int key = (int) a.getStatus();

     //设置选中的下拉框选项

     this.cboStatus.setSelectedItem(key);

     Cate c = null;

     //通过;资产?a的通过id找到大类的方法?从而得到主页面的cdao

     try {

     c = face.cdao.select(a.getCateid());

     } catch (Exception e) {

     // TODO Auto-generated catch block

     e.printStackTrace();

     }

     //得到的类设置为下拉框选项的内容

     this.cboCate.setSelectedItem(c); //

    // SubCate sub = null;

    // try {

    // sub = face.subdao.select(a.getSubcateid());

    //

    // } catch (Exception e) {

    // // TODO Auto-generated catch block // e.printStackTrace();

    // } // 通过大类找对象

    // this.cboSubCate.setSelectedItem(sub);

     }

     //GEN-BEGIN:variables

     // Variables declaration - do not modify

     private javax.swing.ButtonGroup btnGrp1;

     private javax.swing.JComboBox cboCate;

     private javax.swing.JComboBox cboStatus;

     private javax.swing.JComboBox cboSubCate;

     private javax.swing.JButton jButton1;

     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.JLabel jLabel7;

     private javax.swing.JLabel jLabel8;

     private javax.swing.JRadioButton rdoAdd;

     private javax.swing.JRadioButton rdoDel;

     private javax.swing.JRadioButton rdoUpdate;

     private javax.swing.JTextField txtDate;

     private javax.swing.JTextField txtId;

     private javax.swing.JTextField txtModel;

     private javax.swing.JTextField txtName;

     private javax.swing.JTextField txtPrice;

     // End of variables declaration//GEN-END:variables

}

Report this document

For any questions or suggestions please email
cust-service@docsford.com