⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 userdelete.java

📁 是Eclipse web开发从入门到精通的源码
💻 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 + -