📄 returnbook.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.HashMap;
import java.util.List;
import java.util.Map;
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.main.HibernateUtil;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
/**
* 还书类
*
* @author lianhw
*
*/
public class ReturnBook extends JFrame implements ActionListener {
JPanel panel1, panel2;
Container container;
JLabel returnedBookStudentLabel, returnedBookNameLabel, returnedDateLabel,
returnedCommentLabel;
JTextField returnedBookStudentText, returnedDateText, returnedCommentText;
JButton clearButton, yseButton, cancelButton;
JComboBox bookNameComboBox = new JComboBox();
// 书名和BookBrowse类的映射
private Map bookName2BookBrowse = new HashMap();
public ReturnBook() {
super("书籍还入");
container = getContentPane();
container.setLayout(new BorderLayout());
// “还书者姓名”标签
returnedBookStudentLabel = new JLabel("还书者姓名", JLabel.CENTER);
// “书名”标签
returnedBookNameLabel = new JLabel("书名", JLabel.CENTER);
// “日期”标签
returnedDateLabel = new JLabel("日期", JLabel.CENTER);
// “备注”标签
returnedCommentLabel = new JLabel("备注", JLabel.CENTER);
// 输入归还者姓名文本框
returnedBookStudentText = new JTextField(15);
// 输入归还日期文本框
returnedDateText = new JTextField(15);
// 输入备注文本框
returnedCommentText = new JTextField(15);
// 取得SessionFactory
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
// 打开session
Session session = sessionFactory.openSession();
// 创建一个事务
Transaction tx = session.beginTransaction();
// hsql执行语句
String hql = "from BookBrowse where is_returned='否'";
// 执行查询
Query userList = session.createQuery(hql);
// 将查询结果放置到一个list链表中
List list = userList.list();
for (int index = 0; index < list.size(); index++) {
BookBrowse bb = (BookBrowse) list.get(index);
bookNameComboBox.addItem(bb.getBookName());
bookName2BookBrowse.put(bb.getBookName(), bb);
}
// 事务提交
tx.commit();
// 关闭session
session.close();
panel1 = new JPanel();
panel1.setLayout(new GridLayout(4, 2));
panel1.add(returnedBookStudentLabel);
panel1.add(returnedBookStudentText);
panel1.add(returnedBookNameLabel);
panel1.add(bookNameComboBox);
panel1.add(returnedDateLabel);
panel1.add(returnedDateText);
panel1.add(returnedCommentLabel);
panel1.add(returnedCommentText);
container.add(panel1, BorderLayout.CENTER);
panel2 = new JPanel();
panel2.setLayout(new GridLayout(1, 3));
// “清空”按钮
clearButton = new JButton("清空");
// “确定”按钮
yseButton = new JButton("确定");
// “取消”按钮
cancelButton = new JButton("取消");
// 为“清空”按钮添加事件监听者
clearButton.addActionListener(this);
// 为“确定”按钮添加事件监听者
yseButton.addActionListener(this);
// 为“取消”按钮添加事件监听者
cancelButton.addActionListener(this);
panel2.add(clearButton);
panel2.add(yseButton);
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) {
// 单击“清空”按钮,清空所有的文本框
returnedBookStudentText.setText("");
returnedDateText.setText("");
returnedCommentText.setText("");
} else if (action.getSource() == yseButton) {
// 判断还书者的姓名是否为空
if (returnedBookStudentText.getText().trim().equals("")) {
JOptionPane.showMessageDialog(null, "请输入还书者的姓名。。。");
} else if (bookNameComboBox.getSelectedItem().equals("")) {
// 判断有没用出借的书
JOptionPane.showMessageDialog(null, "图书馆没有出借过书!");
} else {
// 取得SessionFactory
SessionFactory sessionFactory = HibernateUtil
.getSessionFactory();
// 打开session
Session session = sessionFactory.openSession();
// 创建一个事务
Transaction tx = session.beginTransaction();
String bookName = bookNameComboBox.getSelectedItem().toString();
BookBrowse bookBrowse = (BookBrowse) bookName2BookBrowse
.get(bookName);
bookBrowse.setIsReturned("是");
bookBrowse.setCom(returnedCommentText.getText().trim());
session.saveOrUpdate(bookBrowse);
JOptionPane.showMessageDialog(null, "还书成功!");
// 事务提交
tx.commit();
// 关闭session
session.close();
this.dispose();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -