?? userdelete.java
字號:
package library.user;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import library.hibernate.UserTable;
import library.main.HibernateUtil;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
/**
*
* 刪除用戶類,將一些用戶從系統中刪除
*
* @author lianhw
*
*/
public class UserDelete extends JFrame implements ActionListener {
JPanel panel1, panel2;
Container container;
JLabel userLabel, passwordLabel;
JTextField userText;
JPasswordField passwordText;
JButton yesButton, cancelButton;
public UserDelete() {
super("刪除用戶");
container = getContentPane();
container.setLayout(new BorderLayout());
//“用戶名”標簽
userLabel = new JLabel("用戶名", JLabel.CENTER);
//“密碼”標簽
passwordLabel = new JLabel("密碼", JLabel.CENTER);
//輸入用戶名文本框
userText = new JTextField(10);
//輸入密碼文本框
passwordText = new JPasswordField(10);
//“確定”按鈕
yesButton = new JButton("確定");
//“取消”按鈕
cancelButton = new JButton("取消");
//為“確定”按鈕添加事件監聽者
yesButton.addActionListener(this);
//為“取消”按鈕添加事件監聽者
cancelButton.addActionListener(this);
panel1 = new JPanel();
panel1.setLayout(new GridLayout(2, 2));
panel1.add(userLabel);
panel1.add(userText);
panel1.add(passwordLabel);
panel1.add(passwordText);
panel2 = new JPanel();
panel2.add(yesButton);
panel2.add(cancelButton);
container.add(panel1, BorderLayout.CENTER);
container.add(panel2, BorderLayout.SOUTH);
//設置窗口的大小
setSize(300, 300);
}
/**
* 動作響應方法,將指定用戶從數據庫中刪除
*
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
public void actionPerformed(ActionEvent action) {
if (action.getSource() == cancelButton) {
// 如果單擊“取消”按鈕后執行的動作
this.dispose();
} else if (action.getSource() == yesButton) {
// 單擊“確定”按鈕后將用戶從數據庫中刪除
char[] password = passwordText.getPassword();
// 將用戶密碼轉換成字符串
String passwordStr = new String(password);
// 判斷用戶名是否為空
if (userText.getText().trim().equals("")) {
JOptionPane.showMessageDialog(null, "用戶名不能為空!");
}
// 判斷密碼是否為空
else if (passwordText.equals("")) {
JOptionPane.showMessageDialog(null, "密碼不能為空!");
} else {
// 取得SessionFactory
SessionFactory sessionFactory = HibernateUtil
.getSessionFactory();
// 打開session
Session session = sessionFactory.openSession();
// 創建一個事務
Transaction tx = session.beginTransaction();
// 創建UserTable對象
UserTable user = new UserTable();
// 設置user對象的名字
user.setUserName(userText.getText().trim());
// 設置user對象的密碼
user.setPassword(passwordText.getText().trim());
// 刪除該用戶
session.delete(user);
JOptionPane.showMessageDialog(null, "刪除成功!");
// 事務提交
tx.commit();
// 關閉session
session.close();
this.dispose();
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -