?? formuser.java
字號:
/**
* -----------------------------------------------------------------------
* 創(chuàng)建時間:2006年2月9日
* 作 者:孫豐偉
* 功 能:管理系統(tǒng)帳號,要主要添加新的帳號與刪除原有的帳號.
* 說 明:1.這是一個邊界類,也就是系統(tǒng)與用戶交互有界面,在這一層中要訪問
* mode包中的ManageAccount類中的實例方法saveAccount()實現(xiàn)新建一個
* 帳號到數(shù)據(jù)庫中。添加的新帳號要及到的更新到左側(cè)的樹中 。新建帳
* 號如果已經(jīng)存在則重新建立。
* 2.刪除密碼,調(diào)用ManageAccount類中的實例方法removeAccount()實現(xiàn)。
* ------------------------------------------------------------------------
*/
package cn.sunfengwei.employee.view;
import java.awt.*;
import java.awt.event.*;
import java.util.Iterator;
import java.util.Vector;
import javax.swing.*;
import javax.swing.tree.*;
import cn.sunfengwei.employee.model.*;
public class FormUser extends JInternalFrame implements ActionListener{
//創(chuàng)建幾個JButton
private JButton btnAdd,btnDelete,btnSave,btnExit;
private Container contentPane;
//樹
private JTree treeAccount;
private DefaultTreeModel treeModel;
private DefaultMutableTreeNode treeNode,node;
//
private JScrollPane treeScrollPane;
private JSplitPane leftSplitPane;
private JTextField txtUserId;
private JPasswordField newPassword,validatePassword;
private UserDTO account;
public FormUser(String title) {
super(title,
false, //resizable
true, //closable
true, //maximizable
true);//iconifiable
contentPane=this.getContentPane();
treeScrollPane=new JScrollPane(this.setTree());
leftSplitPane=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,this.treeScrollPane,setAccountPanel());
leftSplitPane.setDividerLocation(150);
this.contentPane.add(leftSplitPane);
this.setBounds(10,10,500,400);
//加載工具欄
this.contentPane.add(this.createToolBar(),BorderLayout.NORTH);
}
//創(chuàng)建工具欄JToolBar
private JToolBar createToolBar()
{
JToolBar toolBar=new JToolBar();
btnExit=new JButton(new ImageIcon("images\\exit.gif"));
btnSave=new JButton(new ImageIcon("images\\save1.gif"));
btnDelete=new JButton(new ImageIcon("images\\del.gif"));
btnExit.setToolTipText("退出系統(tǒng)");
btnSave.setToolTipText("保存新帳號");
btnSave.setActionCommand("save");
btnDelete.setActionCommand("delete");
btnExit.setActionCommand("exit");
btnSave.addActionListener(this);
btnDelete.addActionListener(this);
btnExit.addActionListener(this);
toolBar.add(btnSave);
toolBar.add(btnDelete);
//加分隔條
toolBar.addSeparator();
toolBar.add(btnExit);
return toolBar;
}
private JTree setTree()
{
treeModel=new DefaultTreeModel(getTreeNode());
treeAccount=new JTree(treeModel);
return treeAccount;
}
private JPanel setAccountPanel()
{
JPanel panel=new JPanel();
// 使用Box容器,設(shè)置登錄窗體中的組件
Box boxTitle=Box.createVerticalBox();
boxTitle.add(Box.createVerticalStrut(5));
boxTitle.add(new JLabel("用 戶 ID:"));
boxTitle.add(Box.createVerticalStrut(10));
boxTitle.add(new JLabel("新 密 碼:"));
boxTitle.add(Box.createVerticalStrut(10));
boxTitle.add(new JLabel("確認密碼:"));
txtUserId=new JTextField(16); //20個字符
newPassword=new JPasswordField();
validatePassword=new JPasswordField();
Box boxUser=Box.createVerticalBox();
boxUser.add(Box.createVerticalStrut(6));
boxUser.add(txtUserId);
boxUser.add(Box.createVerticalStrut(9));
boxUser.add(newPassword);
boxUser.add(Box.createVerticalStrut(9));
boxUser.add(validatePassword);
Box baseBox=Box.createHorizontalBox();
baseBox.add(boxTitle);
baseBox.add(Box.createHorizontalStrut(10));
baseBox.add(boxUser);
/*-----------------------------------------------*/
FlowLayout flow=new FlowLayout();
flow.setAlignment(FlowLayout.LEFT);
panel.setLayout(flow);
panel.add(baseBox);
contentPane.validate();
return panel;
}
//設(shè)置樹的結(jié)點
private DefaultMutableTreeNode getTreeNode()
{
//創(chuàng)建根結(jié)點
treeNode=new DefaultMutableTreeNode("信息技術(shù)教學(xué)中心");
UserDAO accountDAO=new UserDAO();
UserDTO account=new UserDTO("","");
for(UserDTO tempAccount : accountDAO.getAccounts(account))
{
treeNode.add(new DefaultMutableTreeNode(tempAccount));
}
return treeNode;
}
/* (非 Javadoc)
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
public void actionPerformed(ActionEvent e) {
// TODO 自動生成方法存根
String es=e.getActionCommand();
if(es.equals("add"))
{}
if(es.equals("save"))
{
this.saveUser();
}
//當刪除一個用戶時
if(es.equals("delete"))
{
//取出當前被選中的結(jié)點
if(treeAccount.getLastSelectedPathComponent()==null)
return;
node=(DefaultMutableTreeNode)treeAccount.getLastSelectedPathComponent();
if(!node.isLeaf())
{
return;
}
//取出結(jié)點上的用戶名,user
String user=node.getUserObject().toString();
//if(user.equals("DB培訓(xùn)中心")) return;
if(node.isRoot()) return;
int n=JOptionPane.showConfirmDialog(FormUser.this,"你真的要刪除["+user+"]用戶嗎?","系統(tǒng)詢問",JOptionPane.YES_NO_OPTION);
if(n==JOptionPane.YES_OPTION)
{
UserDAO mAccount=new UserDAO();
if(mAccount.removeAccountById(user))
{
//在數(shù)據(jù)庫刪除成功后,樹上的結(jié)點也沒應(yīng)該存在了
//下面的方法實現(xiàn)從樹上移除一個結(jié)點
node.removeFromParent();
//刪除后在內(nèi)存中完成了,但是在界面上沒有反映,所以要刷新樹模板
treeModel.reload();
}
}
}
if(es.equals("exit"))
{
this.dispose();
}
}
private void saveUser()
{
UserDAO manageAccount=new UserDAO();
if(txtUserId.getText().equals(""))
{
JOptionPane.showMessageDialog(this,"系統(tǒng)帳號不能為空,請輸入!","系統(tǒng)提示",JOptionPane.INFORMATION_MESSAGE);
newPassword.setText("");
validatePassword.setText("");
txtUserId.requestFocus(true);
return;
}
//創(chuàng)建一個新帳號時,創(chuàng)建保證數(shù)據(jù)表中不存在
if(manageAccount.isExistedAccount(txtUserId.getText().trim()))
{
JOptionPane.showMessageDialog(this,"新帳號在系統(tǒng)中已經(jīng)存在,請重新輸入!","系統(tǒng)提示",JOptionPane.INFORMATION_MESSAGE);
newPassword.setText("");
validatePassword.setText("");
txtUserId.requestFocus(true);
txtUserId.selectAll();
return;
}
String newWord=new String(newPassword.getPassword());
String validateWord=new String(validatePassword.getPassword());
if(!newWord.equals(validateWord))
{
JOptionPane.showMessageDialog(this,"你輸入的密碼與確認密碼不符,請重新輸入!","系統(tǒng)提示",JOptionPane.ERROR_MESSAGE);
newPassword.setText("");
validatePassword.setText("");
newPassword.requestFocus(true);
newPassword.selectAll();
}
else
{
UserDTO account=new UserDTO();
account.setUserName(txtUserId.getText().trim());
account.setPassword(newWord);
if(manageAccount.saveAccount(account))
{
JOptionPane.showMessageDialog(this,"新帳號創(chuàng)建成功,歡迎使用新帳號登錄系統(tǒng)!","系統(tǒng)提示",JOptionPane.INFORMATION_MESSAGE);
//如果保存成功后,要把新帳號加載樹上
treeNode.add(new DefaultMutableTreeNode(txtUserId.getText().trim()));
txtUserId.setText("");
newPassword.setText("");
validatePassword.setText("");
txtUserId.requestFocus(true);
treeModel.reload();
}
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -