📄 bookmangementaction.java
字号:
package librarymanagement.action.bookMangementAction;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Vector;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import librarymanagement.common.GetJTable;
import librarymanagement.dao.common.DbException;
import librarymanagement.dao.libraryDao.LibraryDao;
import librarymanagement.dao.libraryDao.LibraryDaoIpml;
import librarymanagement.view.dialog.AddBookWindow;
import librarymanagement.view.dialog.BookMangement;
import librarymanagement.view.dialog.bookModifywindow;
import librarymanagement.view.common.ExportbookInfo;
/**
* 图书管理事件
*
* @author: 廖运球
*
*/
public class BookMangementaction implements ActionListener {
private BookMangement mangement;
private JRadioButton Rigor, Dark;
private JPanel radioPanel;
private JComboBox ComBox;
private JFileChooser fileChooser;
public BookMangementaction(BookMangement mangement) {
this.mangement = mangement;
}
/**
* 图书管理窗体上的各按钮的事件
*/
public void actionPerformed(ActionEvent e) {
String name = e.getActionCommand();
if (name.equals("查询")) {
System.out.print(name);
if (mangement.getSelectRadioButton().equals("精确")) {
intilalJComboBox();
} else if (mangement.getSelectRadioButton().equals("模糊")) {
intilalJComboBox();
}
}
if (name.equals("添加")) {
new AddBookWindow().setVisible(true);
}
if (name.equals("修改")) {
new bookModifywindow().setVisible(true);
}
if (name.equals("删除")) {
LibraryDao dao = null;
Vector v = null;
dao = new LibraryDaoIpml();
JTable table = mangement.buildJTable();
int row = table.getSelectedRow();
if (row >= 0) {
// int book_id = Integer.parseInt(table.getValueAt(row, 0)
// .toString());
int book_id = Integer.parseInt(table.getValueAt(row, 0)
.toString());// 获得某行第0列的值
int ch = JOptionPane.showConfirmDialog(null, "你确定要删除该条记录吗?",
"图书数据删除", JOptionPane.YES_NO_OPTION);
if (ch == JOptionPane.YES_OPTION) {
if (dao.deleteBookById(book_id)) {
JOptionPane.showMessageDialog(null, "数据删除成功", "info",
JOptionPane.YES_OPTION);
DefaultTableModel model = (DefaultTableModel) table
.getModel();
model.removeRow(row);
}
}
table.setRequestFocusEnabled(false);
} else {
JOptionPane.showMessageDialog(null, "请选择你要删除的记录", "删除提示",
JOptionPane.YES_OPTION);
table.setRequestFocusEnabled(false);
}
}
if (name.equals("刷新")) {
Vector v = null;
LibraryDao dao = null;
try {
dao = new LibraryDaoIpml();
v = dao.findBookInfo();
if (v.size() == 0) {
DefaultTableModel model = (DefaultTableModel) mangement
.buildJTable().getModel();
int rows = model.getRowCount();
for (int i = rows - 1; i >= 0; i--) {
model.removeRow(i);
}
}
JTable table = mangement.buildJTable();
GetJTable.initiaJTableValues(table, v);
// 调用GetJTable表的静态方法向JTable表中插入记录
} catch (DbException ex) {
JOptionPane.showMessageDialog(null, ex.getMessage(), "数据查找",
JOptionPane.YES_OPTION);
return;
}
}
if (name.equals("打印")) {
}
if (name.equals("转Excel")) {
LibraryDao dao = new LibraryDaoIpml();
String fileName = null;
fileChooser = new JFileChooser();
fileChooser.setCurrentDirectory(new File("."));
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
File file = null;
if (fileName == null) {
int result = fileChooser.showSaveDialog(mangement);
if (result == JFileChooser.APPROVE_OPTION) {
file = fileChooser.getSelectedFile();
fileName = file.getAbsolutePath();
} else {
return;
}
}
try {
ExportbookInfo exportAction = new ExportbookInfo();
String fileNameSave = file.getAbsolutePath() + fileName;
Vector data = dao.findBookInfo();// 得到所有读者信息
exportAction.writeExcelOfFile(fileName, data, null);
// 记录到数据库中去
String content = "导出读者信息 ";
JOptionPane.showMessageDialog(null, "已导出到工程目录下,请刷新工程");
} catch (DbException ex) {
System.out.println("导出message: " + ex.getMessage());
}
mangement.setVisible(false);
}
if (name.equals("退出")) {
mangement.dispose();
}
}
/**
* 根据不同查询条件查询的方法
*
*/
public void intilalJComboBox() {
LibraryDao dao = null;
Vector v = null;
System.out.print(mangement.getSelectRadioButton());
String Selectname = mangement.getSelectCombox();
try {
if (mangement.combox.getSelectedItem().toString().equals("")) {
JOptionPane.showMessageDialog(null, "请选择查找方式", "信息提示",
JOptionPane.YES_OPTION);
return;
} else if (Selectname.equals("作者")) {
checkJTextFieldISNULL();
String author = mangement.text.getText();
dao = new LibraryDaoIpml();
v = dao.findBookByAuthor(author);
} else if (Selectname.equals("存放位置")) {
checkJTextFieldISNULL();
String book_location = mangement.text.getText();
dao = new LibraryDaoIpml();
v = dao.findBookByLocation(book_location);
} else if (Selectname.equals("图书名")) {
checkJTextFieldISNULL();
String book_name = mangement.text.getText();
dao = new LibraryDaoIpml();
v = dao.findBookByBook_name(book_name);
} else if (Selectname.equals("图书编号")) {
checkJTextFieldISNULL();
if (mangement.text.getText().matches("\\d{0,10}")) {// 检查输入的数据是否按要求填写
System.out.print(mangement.getSelectCombox());
int book_id = Integer.parseInt(mangement.text.getText());
dao = new LibraryDaoIpml();
v = dao.findBookBybook_id(book_id);
// System.out.print("V=;"+v);
} else {
JOptionPane.showMessageDialog(null, "图书编号只能是十位以内的数",
"提示信息", JOptionPane.YES_OPTION);
return;
}
}
JTable table = mangement.buildJTable();
GetJTable.initiaJTableValues(table, v);
// 调用GetJTable表的静态方法向JTable表中插入记录
} catch (DbException e1) {
JOptionPane.showMessageDialog(null, e1.getMessage(), "书籍信息查找异常:",
JOptionPane.YES_OPTION);
return;
}
}
/**
* 判断查询面板中的文本框是否 填写
*/
public void checkJTextFieldISNULL() {
if (mangement.text.getText().equals("")) {
JOptionPane.showMessageDialog(null, "对不起!需按要求填写文本框内容", "提示信息",
JOptionPane.YES_OPTION);
return;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -