Java+Swing图书管理系统
- 一、系统介绍
- 二、功能展示
- 1.管理员登陆
- 2.图书查询
- 3.图书入库
- 4.借书
- 5.还书
- 6.图书证管理
- 三、系统实现
- 1.BookManageMainFrame.java
- 四、其它
- 1.其他系统实现
一、系统介绍
该系统实现了用户端实现书籍查询,借书,还书功能。用户能够查询自己的借书记录。管理员能够上架图书,下架图书,添加图书类别,添加图书信息,修改图书信息,添加借书证信息、查询借书证信息、删除除借书证,查询图书借阅归还记录。
二、功能展示
1.管理员登陆
2.图书查询
3.图书入库
4.借书
5.还书
6.图书证管理
三、系统实现
1.BookManageMainFrame.java
package cn.book.view;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import cn.book.dao.AdminDao;
import cn.book.dao.BookDao;
import cn.book.dao.BorrowDao;
import cn.book.dao.CardDao;
import cn.book.model.Book;
public class BookManageMainFrame {
String username = "";
CardLayout layout = new CardLayout();;
JFrame bamFrame;
MainPanel mainPanel = new MainPanel();// 主面板
JPanel upPanel = new JPanel();// 主面板中的---上面板
JPanel downPanel = new JPanel();// 主面板中的---下面板
JPanel leftPanel = new JPanel();// 主面板中的---左面板
JPanel rightPanel = new JPanel();// 主面板中的---右面板
BookDao book = new BookDao();
AdminDao admin = new AdminDao();
CardDao card = new CardDao();
BorrowDao borrow = new BorrowDao();
SelectBookPanel selectBookPanel = new SelectBookPanel();
LoginPanel loginPanel = new LoginPanel();
final Object[] columnNames = {"书号", "类别", "书名","出版社","出版年份","作者","价格","总数量","馆存量"}; //列名最好用final修饰
Object[][] rowData = queryData("");
DefaultTableModel model = new DefaultTableModel(rowData,columnNames);
JTable dataTable = new JTable (model);
AddBookPanel addBookPanel = new AddBookPanel();
BorrowPanel borrowPanel = new BorrowPanel();
ReturnPanel returnPanel = new ReturnPanel();
CardPanel cardPanel = new CardPanel();
public BookManageMainFrame(){
dataTable.setPreferredScrollableViewportSize(new Dimension(900, 580));//设置表格的大小
dataTable.setRowHeight (30);//设置每行的高度为20
dataTable.setRowHeight (0, 30);//设置第1行的高度为15
dataTable.setSelectionBackground (Color.white);//设置所选择行的背景色
dataTable.setSelectionForeground (Color.blue);//设置所选择行的前景色
//dataTable.setGridColor (Color.black);//设置网格线的颜色
dataTable.selectAll ();//选择所有行
//dataTable.setRowSelectionInterval (0,2);//设置初始的选择行,这里是1到3行都处于选择状态
dataTable.clearSelection ();//取消选择
dataTable.setDragEnabled (false);//不懂这个
dataTable.setShowGrid (false);//是否显示网格线
dataTable.setShowHorizontalLines (false);//是否显示水平的网格线
dataTable.setShowVerticalLines (true);//是否显示垂直的网格线
//friends.setValueAt ("tt", 0, 0);//设置某个单元格的值,这个值是一个对象
dataTable.doLayout ();
dataTable.setBackground (Color.lightGray);
// 设置表格中的数据居中显示
DefaultTableCellRenderer r=new DefaultTableCellRenderer();
r.setHorizontalAlignment(JLabel.CENTER);
dataTable.setDefaultRenderer(Object.class,r);
//JScrollPane pane2 = new JScrollPane (example2);
selectBookPanel.add(new JScrollPane (dataTable), BorderLayout.SOUTH);
// 定义布局
BorderLayout mainLayout = new BorderLayout();
// 左布局
leftPanel.add(mainPanel);
leftPanel.setBackground(Color.WHITE);// 左边背影色
//右布局
rightPanel.setLayout(layout);
rightPanel.add("cardselectbook", selectBookPanel);// 图书查询
rightPanel.add("cardaddbook", addBookPanel);// 图书入库
rightPanel.add("cardlogin", loginPanel);// 登陆
rightPanel.add("cardborrow", borrowPanel);// 借书
rightPanel.add("cardreturn", returnPanel);// 还书
rightPanel.add("cardcard", cardPanel);// 图书管理
// 定义主窗体
bamFrame = new JFrame("图书管理系统");
bamFrame.getContentPane().setLayout(mainLayout);
bamFrame.setSize(1050, 600);// 设定窗体大小
// 给窗体加面板
bamFrame.getContentPane().add(leftPanel, BorderLayout.WEST);
bamFrame.getContentPane().add(rightPanel, BorderLayout.CENTER);
addListeners();// 调用监听方法
bamFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//bamFrame.pack();
bamFrame.setVisible(true);
//设置窗口居中
bamFrame.setLocationRelativeTo(null);
}
// 窗体中部分对象监听方法
private void addListeners() {
// 在窗体的左容器上点查询按钮
mainPanel.getSelectBookButton().addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
layout.show(rightPanel, "cardselectbook");
}
});
// 在窗体的左容器上点《图书入库》按钮
mainPanel.getStockButton().addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(username.equals("")) {
layout.show(rightPanel, "cardlogin");
}else {
layout.show(rightPanel, "cardaddbook");
}
}
});
// 在窗体的左容器上点《借书》按钮
mainPanel.getBorrowButton().addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(username.equals("")) {
layout.show(rightPanel, "cardlogin");
}else {
layout.show(rightPanel, "cardborrow");
}
}
});
// 在窗体的左容器上点《还书》按钮
mainPanel.getStillButton().addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(username.equals("")) {
layout.show(rightPanel, "cardlogin");
}else {
layout.show(rightPanel, "cardreturn");
}
}
});
// 在窗体的左容器上点《图书证管理》按钮
mainPanel.getCardButton().addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(username.equals("")) {
layout.show(rightPanel, "cardlogin");
}else {
layout.show(rightPanel, "cardcard");
}
}
});
// 在窗体的左容器上点登陆按钮
mainPanel.getLoginButton().addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
layout.show(rightPanel, "cardlogin");
}
});
// 在窗体的左容器上点《退出》按钮
mainPanel.getExitButton().addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);//退出系统
}
});
// 在窗体的左容器上点《图书查询》按钮
selectBookPanel.getSelectButton().addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
String title = selectBookPanel.getTitleField().getText();
System.out.print(title);
Object[][] rowData2 = queryData(title);
model.setDataVector(rowData2, columnNames);
layout.show(rightPanel, "cardselectbook");
}
});
//图书入库,点击提交按钮
addBookPanel.getOkButton().addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
String bno = addBookPanel.getBnoField().getText();
String category = addBookPanel.getCategoryField().getText();
String title = addBookPanel.getTitleField().getText();
String press = addBookPanel.getPressField().getText();
String year = addBookPanel.getYearField().getText();
String author = addBookPanel.getAuthorField().getText();
String price = addBookPanel.getPriceField().getText();
String total = addBookPanel.getTotalField().getText();
String stock = addBookPanel.getStockField().getText();
int i = book.AddBook(bno, category, title, press, year, author, price, total, stock);
if(i>0) {
JOptionPane.showMessageDialog(addBookPanel, "图书入库成功!");
addBookPanel.getBnoField().setText("");
addBookPanel.getCategoryField().setText("");
addBookPanel.getTitleField().setText("");
addBookPanel.getPressField().setText("");
addBookPanel.getYearField().setText("");
addBookPanel.getAuthorField().setText("");
addBookPanel.getPriceField().setText("");
addBookPanel.getTotalField().setText("");
addBookPanel.getStockField().setText("");
}else {
JOptionPane.showMessageDialog(addBookPanel, "图书入库失败!");
addBookPanel.getBnoField().setText("");
addBookPanel.getCategoryField().setText("");
addBookPanel.getTitleField().setText("");
addBookPanel.getPressField().setText("");
addBookPanel.getYearField().setText("");
addBookPanel.getAuthorField().setText("");
addBookPanel.getPriceField().setText("");
addBookPanel.getTotalField().setText("");
addBookPanel.getStockField().setText("");
}
}
});
//图书入库,点击返回按钮
addBookPanel.getBackButton().addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
Object[][] rowData2 = queryData("");
model.setDataVector(rowData2, columnNames);
layout.show(rightPanel, "cardselectbook");
}
});
// 点击《登陆》按钮
loginPanel.getOkButton().addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String aname = loginPanel.getAnameField().getText();
String password = loginPanel.getPasswordField().getText();
if(!aname.equals("")) {
boolean isture = admin.login(aname, password);
if(isture) {
JOptionPane.showMessageDialog(loginPanel, "登陆成功!");
layout.show(rightPanel, "cardselectbook");
username=aname;
loginPanel.getAnameField().setText("");
loginPanel.getPasswordField().setText("");
}else {
JOptionPane.showMessageDialog(loginPanel, "用户名或密码错误!");
}
}else {
JOptionPane.showMessageDialog(loginPanel, "用户名不能为空!");
}
}
});
// 点击《返回》按钮
loginPanel.getBackButton().addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
layout.show(rightPanel, "cardselectbook");
}
});
//点击《借书查询》按钮
borrowPanel.selectBorrowPanel.getSelectButton().addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String cno=borrowPanel.selectBorrowPanel.getCnoField().getText();
borrowPanel.selectBorrow(cno);
}
});
//点击《借书》按钮
borrowPanel.selectBorrowPanel.getBorrowButton().addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String cno=borrowPanel.selectBorrowPanel.getCnoField().getText();
String bno=borrowPanel.selectBorrowPanel.getBnoField().getText();
if(cno.equals("")) {
JOptionPane.showMessageDialog(borrowPanel, "借书证卡号不能为空!");
return;
}
if(bno.equals("")) {
JOptionPane.showMessageDialog(borrowPanel, "图书号不能为空!");
return;
}
int isbook = book.isBook(bno);
if(isbook==-1) {
JOptionPane.showMessageDialog(borrowPanel, "图书号存在,请重新输入!");
return;
}else if(isbook==0) {
String returndeadline = borrow.queryReturndeadline(bno);
JOptionPane.showMessageDialog(borrowPanel, "图书库存不足,最早还书时间为:"+returndeadline);
return;
}
boolean iscard = card.isCard(cno);
if(!iscard) {
JOptionPane.showMessageDialog(borrowPanel, "借书证卡号存在,请重新输入!");
return;
}
int i = borrow.AddBorrow(cno, bno, username);
if(i>0) {
int j = book.updateBook(bno);
if(j>0) {
JOptionPane.showMessageDialog(loginPanel, "借书成功!");
borrowPanel.selectBorrow(cno);
}
}
}
});
//点击《还书查询》按钮
returnPanel.selectReturnPanel.getSelectButton().addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String cno=returnPanel.selectReturnPanel.getCnoField().getText();
returnPanel.selectReturn(cno);
}
});
//点击《还书》按钮
returnPanel.selectReturnPanel.getReturnButton().addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int i = returnPanel.rdataTable.getSelectedRow();
if(i>=0) {
String cno = String.valueOf(returnPanel.rdataTable.getValueAt(i,0));
String bno = String.valueOf(returnPanel.rdataTable.getValueAt(i,1));
String borrowdate = String.valueOf(returnPanel.rdataTable.getValueAt(i,7));
int j = borrow.updateBorrow(cno, bno, borrowdate);
if(j>0) {
int r = book.updateReturnBook(bno);
if(r>0) {
String cno1=returnPanel.selectReturnPanel.getCnoField().getText();
returnPanel.selectReturn(cno1);
}else {
JOptionPane.showMessageDialog(returnPanel, "还书失败!");
}
}else {
JOptionPane.showMessageDialog(returnPanel, "还书失败!");
}
}else {
JOptionPane.showMessageDialog(returnPanel, "请选择要还的图书!");
}
}
});
//点击《图书证查询》按钮
cardPanel.selectCardPanel.getSelectButton().addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String cno=cardPanel.selectCardPanel.getCnoField().getText();
cardPanel.selectCard(cno);
cardPanel.layout.show(cardPanel.downPanel, "selectcardpanel");
}
});
//点击《图书证添加》按钮
cardPanel.selectCardPanel.getAddButton().addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cardPanel.layout.show(cardPanel.downPanel, "addcardpanel");
}
});
//点击《图书证返回》按钮
cardPanel.addcardpanel.getBackButton().addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String cno=cardPanel.selectCardPanel.getCnoField().getText();
cardPanel.selectCard(cno);
cardPanel.layout.show(cardPanel.downPanel, "selectcardpanel");
}
});
//点击《图书证提交》按钮
cardPanel.addcardpanel.getOkButton().addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String cno=cardPanel.addcardpanel.getCnoField().getText();
String name=cardPanel.addcardpanel.getNameField().getText();
String department=cardPanel.addcardpanel.getDepartmentField().getText();
String type=cardPanel.addcardpanel.getTypeField().getText();
int i=card.AddCard(cno, name, department, type);
if(i>0) {
JOptionPane.showMessageDialog(cardPanel, "图书证添加成功!");
cardPanel.addcardpanel.getCnoField().setText("");
cardPanel.addcardpanel.getNameField().setText("");
cardPanel.addcardpanel.getDepartmentField().setText("");
cardPanel.addcardpanel.getTypeField().setText("");
}else {
JOptionPane.showMessageDialog(cardPanel, "图书证添加失败!");
}
}
});
//点击《图书证删除》按钮
cardPanel.selectCardPanel.getDelButton().addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int i = cardPanel.cdataTable.getSelectedRow();
if(i>0) {
String cno = String.valueOf(cardPanel.cdataTable.getValueAt(i,0));
int j = card.deleteCard(cno);
if(j>0) {
JOptionPane.showMessageDialog(returnPanel, "删除成功!");
cardPanel.selectCard("");
}else {
JOptionPane.showMessageDialog(returnPanel, "删除失败!");
}
}else {
JOptionPane.showMessageDialog(returnPanel, "请选择要删除除的图书证!");
}
}
});
}
//生成表格数据
/**
* @return
*/
public Object[][] queryData(String title){
List<Book> list=book.queryBook(title);
rowData=new Object[list.size()][columnNames.length];
for(int i=0;i<list.size();i++){
for(int j=0;j<rowData.length;j++){
rowData[i][0]=list.get(i).getBno();
rowData[i][1]=list.get(i).getCategory();
rowData[i][2]=list.get(i).getTitle();
rowData[i][3]=list.get(i).getPress();
rowData[i][4]=list.get(i).getYear();
rowData[i][5]=list.get(i).getAuthor();
rowData[i][6]=list.get(i).getPrice();
rowData[i][7]=list.get(i).getTotal();
rowData[i][8]=list.get(i).getStock();
}
}
return rowData;
}
public static void main(String[] args) {
new BookManageMainFrame();
}
}
2.BookManageMainFrame.java
package cn.book.view;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
public class AddBookPanel extends JPanel {
private JTextField bnoField; //编号
private JTextField categoryField;//类别
private JTextField titleField;//书名
private JTextField pressField;//出版社
private JTextField yearField;//年份
private JTextField authorField;//作者
private JTextField priceField;//价格
private JTextField totalField;//总量
private JTextField stockField;//馆存量
private JButton okButton;//确定按钮
private JButton backButton;//返回按钮
public AddBookPanel(){//构造函数
//初始化成员变量
bnoField = new JTextField();
categoryField = new JTextField();
titleField = new JTextField();
pressField = new JTextField();
yearField = new JTextField();
authorField = new JTextField();
priceField = new JTextField();
totalField = new JTextField();
stockField = new JTextField();
okButton=new JButton("提交");
backButton=new JButton("返回");
//设置RegisterPanel容器的布局方式
this.setBorder(new EmptyBorder(5,0,0,5));
this.setLayout(new BorderLayout());
//RegisterPanel容器新增一个容器
JPanel p=new JPanel();
p.setLayout(new GridLayout(9,2,0,20));
//p.setBackground(Color.ORANGE);//设置背景色
//在新增的容器中存入以下控件对象
p.add(new JLabel("图书编号:",SwingConstants.CENTER));
p.add(bnoField);
p.add(new JLabel("图书类别:",SwingConstants.CENTER));
p.add(categoryField);
p.add(new JLabel("图书名称:",SwingConstants.CENTER));
p.add(titleField);
p.add(new JLabel("出版社:",SwingConstants.CENTER));
p.add(pressField);
p.add(new JLabel("年份:",SwingConstants.CENTER));
p.add(yearField);
p.add(new JLabel("作者:",SwingConstants.CENTER));
p.add(authorField);
p.add(new JLabel("价格:",SwingConstants.CENTER));
p.add(priceField);
p.add(new JLabel("总量:",SwingConstants.CENTER));
p.add(totalField);
p.add(new JLabel("馆存量:",SwingConstants.CENTER));
p.add(stockField);
//将新增的容器p加入到RegisterPanel容器中
this.add(p);
//RegisterPanel容器新增第二个容器
JPanel p2=new JPanel();
//p2.setBackground(Color.DARK_GRAY);//设置背景色
//在新增的第二容器中存入以下控件对象
p2.add(okButton);
p2.add(backButton);
//将新增的容器p2加入到RegisterPanel容器中
this.add(p2,"South");
}
//以下都是RegisterPanel类的get方法
public JButton getOkButton() {
return okButton;
}
public JTextField getBnoField() {
return bnoField;
}
public JTextField getCategoryField() {
return categoryField;
}
public JTextField getTitleField() {
return titleField;
}
public JTextField getPressField() {
return pressField;
}
public JTextField getYearField() {
return yearField;
}
public JTextField getAuthorField() {
return authorField;
}
public JTextField getPriceField() {
return priceField;
}
public JTextField getTotalField() {
return totalField;
}
public JTextField getStockField() {
return stockField;
}
public JButton getBackButton() {
return backButton;
}
}
四、其它
1.其他系统实现
Java+Swing实现学生选课管理系统