实现类UserEbo :
package cn.hncu.app.business.impl; import cn.hncu.app.business.ebi.UserEbi; import cn.hncu.app.dao.dao.UserDAO; import cn.hncu.app.dao.factory.UserDaoFactory; import cn.hncu.app.vo.User; public class UserEbo implements UserEbi{ @Override public boolean addUser(User user) { UserDAO userDao = UserDaoFactory.getUserDAO(); return userDao.addUser(user); } @Override public Object[] getAllUser() { UserDAO userDao = UserDaoFactory.getUserDAO(); return userDao.getAllUsers(); } }
表现层:
UserAddPanel 类:
/* * UserAddPanel.java * * Created on __DATE__, __TIME__ */ package cn.hncu.app.ui; import javax.swing.JFrame; import javax.swing.JOptionPane; import cn.hncu.app.business.ebi.UserEbi; import cn.hncu.app.business.factory.UserEbiFactory; import cn.hncu.app.vo.User; /** * * @author __USER__ */ public class UserAddPanel extends javax.swing.JPanel { private JFrame mainFrame = null; /** Creates new form UserAddPanel */ public UserAddPanel(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 // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); jButton1 = new javax.swing.JButton(); tfdAge = new javax.swing.JTextField(); tfdName = new javax.swing.JTextField(); setMinimumSize(new java.awt.Dimension(800, 600)); setLayout(null); jLabel1.setText("\u5e74\u9f84\uff1a"); add(jLabel1); jLabel1.setBounds(170, 200, 50, 40); jLabel2.setText("\u59d3\u540d\uff1a"); add(jLabel2); jLabel2.setBounds(170, 120, 50, 40); jButton1.setText("\u6dfb\u52a0"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); add(jButton1); jButton1.setBounds(240, 330, 170, 60); add(tfdAge); tfdAge.setBounds(210, 210, 170, 30); add(tfdName); tfdName.setBounds(210, 130, 170, 30); }// </editor-fold> //GEN-END:initComponents private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { //1收集参数 String name = tfdName.getText(); String strAge = tfdAge.getText(); int age = Integer.parseInt(strAge);//简单的转换成一个整型数了。 //2组织参数 //User user = new User(name, age); User user = new User(); user.setAge(age); user.setName(name); //3调用逻辑层---通过接口 UserEbi userEbi = UserEbiFactory.getUserEbi(); boolean isSuccess=userEbi.addUser(user); //4导向不同页面 if(isSuccess){ JOptionPane.showMessageDialog(this, "恭喜,添加成功!"); this.mainFrame.setContentPane(new UserListPanel(mainFrame)); this.mainFrame.validate(); }else{ JOptionPane.showMessageDialog(this, "祝贺,添加失败!"); } } //GEN-BEGIN:variables // Variables declaration - do not modify private javax.swing.JButton jButton1; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JTextField tfdAge; private javax.swing.JTextField tfdName; // End of variables declaration//GEN-END:variables }
UserListPanel 类:
/* * UserListPanel.java * * Created on __DATE__, __TIME__ */ package cn.hncu.app.ui; import javax.swing.JFrame; import cn.hncu.app.business.ebi.UserEbi; import cn.hncu.app.business.factory.UserEbiFactory; /** * * @author __USER__ */ public class UserListPanel extends javax.swing.JPanel { private JFrame mainFrame = null; /** Creates new form UserListPanel */ public UserListPanel(JFrame mainFrame) { this.mainFrame = mainFrame; initComponents(); myInitDatas(); } private void myInitDatas() { UserEbi userEbi = UserEbiFactory.getUserEbi(); Object[] objs = userEbi.getAllUser(); this.listUsers.setListData(objs); } /** 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 // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jScrollPane1 = new javax.swing.JScrollPane(); listUsers = new javax.swing.JList(); jLabel1 = new javax.swing.JLabel(); setMinimumSize(new java.awt.Dimension(800, 600)); setLayout(null); listUsers.setModel(new javax.swing.AbstractListModel() { String[] strings = { "" }; public int getSize() { return strings.length; } public Object getElementAt(int i) { return strings[i]; } }); jScrollPane1.setViewportView(listUsers); add(jScrollPane1); jScrollPane1.setBounds(170, 170, 410, 210); jLabel1.setText("\u4fe1\u606f"); add(jLabel1); jLabel1.setBounds(350, 90, 70, 50); }// </editor-fold> //GEN-END:initComponents //GEN-BEGIN:variables // Variables declaration - do not modify private javax.swing.JLabel jLabel1; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JList listUsers; // End of variables declaration//GEN-END:variables }