[Java] 事务跟踪系统 →→→→→进入此内容的聊天室

来自 , 2021-03-08, 写在 Java, 查看 150 次.
URL http://www.code666.cn/view/806beafe
  1. package org.crazyit.transaction.ui.dialog;
  2.  
  3. import java.awt.Dimension;
  4. import java.awt.Toolkit;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.util.List;
  8.  
  9. import javax.swing.Box;
  10. import javax.swing.JButton;
  11. import javax.swing.JComboBox;
  12. import javax.swing.JDialog;
  13. import javax.swing.JLabel;
  14. import javax.swing.JPasswordField;
  15. import javax.swing.JTextField;
  16.  
  17. import org.crazyit.transaction.model.Role;
  18. import org.crazyit.transaction.model.User;
  19. import org.crazyit.transaction.ui.UserPanel;
  20. import org.crazyit.transaction.util.ApplicationContext;
  21. import org.crazyit.transaction.util.ViewUtil;
  22.  
  23. /**
  24.  * 添加用户界面
  25.  * @author yangenxiong yangenxiong2009@gmail.com
  26.  * @version  1.0
  27.  * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
  28.  * <br>Copyright (C), 2009-2010, yangenxiong
  29.  * <br>This program is protected by copyright laws.
  30.  */
  31. public class AddUserDialog extends JDialog {
  32.  
  33.         //用户名
  34.         private JLabel userNameLabel = new JLabel("用户名: ");
  35.         private JTextField userName = new JTextField(10);
  36.        
  37.         //真实姓名
  38.         private JLabel realNameLabel = new JLabel("真实姓名: ");
  39.         private JTextField realName = new JTextField(10);
  40.        
  41.         //密码
  42.         private JLabel passwordLabel = new JLabel("密码: ");
  43.         private JPasswordField password = new JPasswordField(20);
  44.        
  45.         private JLabel roleLabel = new JLabel("角色: ");
  46.         private JComboBox roleSelect = new JComboBox();
  47.        
  48.         //按钮
  49.         private JButton confirmButton = new JButton("确定");
  50.         private JButton cancelButton = new JButton("取消");
  51.        
  52.         private UserPanel userPanel;
  53.        
  54.         public AddUserDialog(UserPanel userPanel) {
  55.                 this.userPanel = userPanel;
  56.                 //用户名
  57.                 Box userNameBox = Box.createHorizontalBox();
  58.                 userNameBox.add(Box.createHorizontalStrut(30));
  59.                 userNameBox.add(this.userNameLabel);
  60.                 userNameBox.add(this.userName);
  61.                 userNameBox.add(Box.createHorizontalStrut(30));
  62.                 //真实姓名
  63.                 Box realNameBox = Box.createHorizontalBox();
  64.                 realNameBox.add(Box.createHorizontalStrut(17));
  65.                 realNameBox.add(this.realNameLabel);
  66.                 realNameBox.add(this.realName);
  67.                 realNameBox.add(Box.createHorizontalStrut(30));
  68.                 //密码
  69.                 Box passwdBox = Box.createHorizontalBox();
  70.                 passwdBox.add(Box.createHorizontalStrut(43));
  71.                 passwdBox.add(this.passwordLabel);
  72.                 passwdBox.add(this.password);
  73.                 passwdBox.add(Box.createHorizontalStrut(30));
  74.                 //角色选择
  75.                 Box roleSelectBox = Box.createHorizontalBox();
  76.                 roleSelectBox.add(Box.createHorizontalStrut(43));
  77.                 roleSelectBox.add(this.roleLabel);
  78.                 roleSelectBox.add(this.roleSelect);
  79.                 roleSelectBox.add(Box.createHorizontalStrut(30));
  80.                 //按钮
  81.                 Box buttonBox = Box.createHorizontalBox();
  82.                 buttonBox.add(this.confirmButton);
  83.                 buttonBox.add(Box.createHorizontalStrut(40));
  84.                 buttonBox.add(this.cancelButton);
  85.                
  86.                 Box mainBox = Box.createVerticalBox();
  87.                 mainBox.add(Box.createVerticalStrut(20));
  88.                 mainBox.add(userNameBox);
  89.                 mainBox.add(Box.createVerticalStrut(10));
  90.                 mainBox.add(realNameBox);
  91.                 mainBox.add(Box.createVerticalStrut(10));
  92.                 mainBox.add(passwdBox);
  93.                 mainBox.add(Box.createVerticalStrut(10));
  94.                 mainBox.add(roleSelectBox);
  95.                 mainBox.add(Box.createVerticalStrut(10));
  96.                 mainBox.add(buttonBox);
  97.                 mainBox.add(Box.createVerticalStrut(20));
  98.                 this.add(mainBox);     
  99.                 this.pack();
  100.                 this.setResizable(false);
  101.                 this.setTitle("新建用户");
  102.                 Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
  103.                 this.setLocation((int)screen.getWidth()/4, (int)screen.getHeight()/5);
  104.                 initListeners();
  105.         }
  106.        
  107.         //创建角色下拉
  108.         private void createRoleSelect() {
  109.                 this.roleSelect.removeAllItems();
  110.                 List<Role> roles = ApplicationContext.roleService.getRoles();
  111.                 for (Role r : roles) {
  112.                         this.roleSelect.addItem(r);
  113.                 }
  114.         }
  115.        
  116.         @Override
  117.         public void setVisible(boolean b) {
  118.                 super.setVisible(b);
  119.                 if (b) createRoleSelect();
  120.         }
  121.  
  122.         private void initListeners() {
  123.                 this.confirmButton.addActionListener(new ActionListener() {
  124.                         public void actionPerformed(ActionEvent arg0) {
  125.                                 add();
  126.                         }
  127.                 });
  128.                 this.cancelButton.addActionListener(new ActionListener() {
  129.                         public void actionPerformed(ActionEvent arg0) {
  130.                                 setVisible(false);
  131.                         }
  132.                 });
  133.         }
  134.        
  135.         //返回密码字符串
  136.         private String getPassword() {
  137.                 char[] passes = this.password.getPassword();
  138.                 StringBuffer password = new StringBuffer();
  139.                 for (char c : passes) {
  140.                         password.append(c);
  141.                 }
  142.                 return password.toString();
  143.         }
  144.        
  145.         //添加用户
  146.         private void add() {
  147.                 if (this.userName.getText().equals("") || this.realName.getText().equals("")
  148.                                 || getPassword().equals("")) {
  149.                         ViewUtil.showWarn("请输入完成的用户信息", this);
  150.                         return;
  151.                 }
  152.                 try {
  153.                         //调用业务接口添加用户
  154.                         ApplicationContext.userService.addUser(getUser());
  155.                         this.setVisible(false);
  156.                         this.userPanel.readData();
  157.                         clean();
  158.                 } catch (Exception e) {
  159.                         e.printStackTrace();
  160.                         ViewUtil.showWarn(e.getMessage(), this);
  161.                 }
  162.         }
  163.        
  164.         //清空界面元素
  165.         private void clean() {
  166.                 this.userName.setText("");
  167.                 this.realName.setText("");
  168.                 this.password.setText("");
  169.         }
  170.        
  171.         //从界面元素中得到各个值, 并创建User对象
  172.         private User getUser() {
  173.                 String userName = this.userName.getText();
  174.                 String realName = this.realName.getText();
  175.                 String passwd = getPassword();
  176.                 Role role = (Role)this.roleSelect.getSelectedItem();
  177.                 User user = new User();
  178.                 user.setUSER_NAME(userName);
  179.                 user.setREAL_NAME(realName);
  180.                 user.setPASS_WD(passwd);
  181.                 user.setROLE_ID(role.getID());
  182.                 return user;
  183.         }
  184. }

回复 "事务跟踪系统"

这儿你可以回复上面这条便签

captcha