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

📄 borrowbook.java

📁 是Eclipse web开发从入门到精通的源码
💻 JAVA
字号:
package library.book;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.GregorianCalendar;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import library.hibernate.BookBrowse;
import library.hibernate.Books;
import library.main.HibernateUtil;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

/**
 * 借阅图书类,将借阅的图书信息提交到数据库中
 * 
 * @author lianhw
 *
 */
public class BorrowBook extends JFrame implements ActionListener {

	JPanel panel1, panel2;

	Container container;

	JLabel borrowedBookStudentLabel, borrowedBookNameLabel, borrowedDateLabel,
			returnDateLabel, borrowedCommentLabel;

	JTextField borrowedBookStudentText, borrowedDateText, returnDateText,
			borrowedCommentText;

	JButton clearButton, yesButton, cancelButton;

	JComboBox bookNameComboBox = new JComboBox();

	/**
	 * 类的构造函数,完成界面的初始化
	 */
	public BorrowBook() {
		super("书籍出借");
		container = getContentPane();
		container.setLayout(new BorderLayout());
		// “借阅者姓名”标签
		borrowedBookStudentLabel = new JLabel("借阅者姓名", JLabel.CENTER);
		// “书名”标签
		borrowedBookNameLabel = new JLabel("书名", JLabel.CENTER);
		// "借阅日期"标签
		borrowedDateLabel = new JLabel("借阅日期", JLabel.CENTER);
		// “归还日期”标签
		returnDateLabel = new JLabel("归还日期", JLabel.CENTER);
		// “备注”标签
		borrowedCommentLabel = new JLabel("备注", JLabel.CENTER);
		// 输入借阅者姓名文本框
		borrowedBookStudentText = new JTextField(15);
		// 输入借阅日期文本框
		borrowedDateText = new JTextField(15);
		// 输入归还日期文本框
		returnDateText = new JTextField(15);
		// 输入备注信息文本框
		borrowedCommentText = new JTextField(15);
		// 取得SessionFactory
		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
		// 打开session
		Session session = sessionFactory.openSession();
		// 创建一个事务
		Transaction tx = session.beginTransaction();
		// hsql执行语句
		String hql = "from Books where books_count>borrowed_count";
		// 执行查询
		Query userList = session.createQuery(hql);
		// 将查询结果放置到一个list链表中
		List list = userList.list();
		// 将查询到所有符合条件的图书加入到出借列表中
		for (int index = 0; index < list.size(); index++) {
			bookNameComboBox.addItem(((Books) list.get(index)).getBookName());
		}
		// 事务提交
		tx.commit();
		// 关闭session
		session.close();

		panel1 = new JPanel();
		panel1.setLayout(new GridLayout(5, 2));
		panel1.add(borrowedBookStudentLabel);
		panel1.add(borrowedBookStudentText);
		panel1.add(borrowedBookNameLabel);
		panel1.add(bookNameComboBox);
		panel1.add(borrowedDateLabel);
		panel1.add(borrowedDateText);
		panel1.add(returnDateLabel);
		panel1.add(returnDateText);
		panel1.add(borrowedCommentLabel);
		panel1.add(borrowedCommentText);
		container.add(panel1, BorderLayout.CENTER);
		panel2 = new JPanel();
		panel2.setLayout(new GridLayout(1, 3));
		// “清空”按钮
		clearButton = new JButton("清空");
		// “确定”按钮
		yesButton = new JButton("确定");
		// “取消”按钮
		cancelButton = new JButton("取消");
		// 为“清空”按钮添加信息监听者
		clearButton.addActionListener(this);
		// 为“确定”按钮添加信息监听者
		yesButton.addActionListener(this);
		// 为“取消”按钮添加信息监听者
		cancelButton.addActionListener(this);
		panel2.add(clearButton);
		panel2.add(yesButton);
		panel2.add(cancelButton);
		container.add(panel2, BorderLayout.SOUTH);

	}

	/**
	 * 动作响应方法,将出借的图书信息提交到数据库中
	 * 
	 * @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() == clearButton) {
			// 单击"清空"按钮将文本框中的信息清空
			borrowedBookStudentText.setText("");
			borrowedDateText.setText("");
			borrowedCommentText.setText("");

		} else if (action.getSource() == yesButton) {
			// 验证输入的借阅者姓名是否为空
			if (borrowedBookStudentText.getText().trim().equals("")) {
				JOptionPane.showMessageDialog(null, "请输入借阅者的姓名。。。");
			} else if (bookNameComboBox.getSelectedItem().equals("")) {
				// 验证现在是否有书可以借阅
				JOptionPane.showMessageDialog(null, "对不起,现在书库里没有书,\n你现在不能借书!");
			} else {

				// 取得SessionFactory
				SessionFactory sessionFactory = HibernateUtil
						.getSessionFactory();
				// 打开session
				Session session = sessionFactory.openSession();
				// 创建一个事务
				Transaction tx = session.beginTransaction();
				// 创建UserTable对象
				BookBrowse browse = new BookBrowse();
				browse.setBookName(bookNameComboBox.getSelectedItem() + "");
				browse.setStudentName(borrowedBookStudentText.getText().trim());
				browse.setBorrowDate(new GregorianCalendar().getTime());
				browse.setCom(borrowedCommentText.getText().trim());
				browse.setReturnDate(new GregorianCalendar().getTime());
				session.saveOrUpdate(browse);

				// 事务提交
				tx.commit();
				// 关闭session
				session.close();

				JOptionPane.showMessageDialog(null, "借阅书籍成功!");
				this.dispose();

			}
		}

	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -