package com.library.view; |
import java.awt.BorderLayout; |
import java.awt.GridLayout; |
import java.awt.event.ActionEvent; |
import java.awt.event.ActionListener; |
import java.awt.event.MouseAdapter; |
import java.awt.event.MouseEvent; |
import java.util.List; |
import javax.swing.BorderFactory; |
import javax.swing.JButton; |
import javax.swing.JComboBox; |
import javax.swing.JInternalFrame; |
import javax.swing.JLabel; |
import javax.swing.JOptionPane; |
import javax.swing.JPanel; |
import javax.swing.JScrollPane; |
import javax.swing.JTable; |
import javax.swing.event.TableModelListener; |
import javax.swing.table.TableModel; |
import com.library.biz.impl.BookBiz; |
import com.library.biz.impl.BookBizImpl; |
import com.library.biz.impl.UsersBiz; |
import com.library.biz.impl.UsersBizImpl; |
import com.libraty.entity.Book; |
import com.libraty.entity.Users; |
public class UserBookInquiry extends JInternalFrame{ |
private static final long serialVersionUID = 1L; |
|
private JScrollPane panel_tablet= null ; /** 可滚动面板 */ |
private JPanel panel_table= null ; /** 表格面板 */ |
private JPanel panel_buttons= null ; /** 按钮面板 */ |
|
private JTable table= null ; /** 表格控件,用来存放列表数据 */ |
private JLabel lb_type= null ; /** 类型标签 */ |
|
private JComboBox<String> cb_type= null ; /** 查询类型选择框,下拉框 */ |
|
private JButton btn_search= null ; /** 查询按钮 */ |
private JButton btn_borrow= null ; /** 借书按钮 */ |
private JButton btn_exit= null ; /** 退出按钮 */ |
|
/** 属性依赖 */ |
private BookBiz bookBiz; |
private UsersBiz usersBiz; |
/** 保存用户信息的属性 */ |
private Users user; |
/** 用户临时存放图书编号 */ |
private int book_id; |
|
/** 初始化块 */ |
{ |
this .usersBiz= new UsersBizImpl(); |
this .bookBiz= new BookBizImpl(); |
init(); |
|
} |
|
public UserBookInquiry(){ |
|
} |
public UserBookInquiry(Users user){ |
this .user=user; /** 一定要调用初始化方法,否则空指针 */ |
} |
|
private void init() |
{ |
table= new JTable(); |
panel_table= new JPanel( new BorderLayout()); /** 表格面板 new BorderLayout()边框布局*/ |
panel_tablet= new JScrollPane(table); |
panel_table.setBorder(BorderFactory.createTitledBorder(BorderFactory.createRaisedBevelBorder(), "图书信息列表" )); |
|
panel_buttons = new JPanel( new GridLayout( 7 , 1 , 0 , 30 )); /** 按钮面板 */ |
panel_buttons.setBorder(BorderFactory.createTitledBorder(BorderFactory.createRaisedBevelBorder(), "查询条件" )); |
|
/** |
* 文本类型 |
* 下拉框 |
* 按钮 |
* */ |
lb_type= new JLabel( "查询类型:" ); |
cb_type= new JComboBox<String>( new String[]{ "查询全部图书" , "查询热门图书" , "查询可借图书" , "查询不可借图书" }); |
btn_search= new JButton( "查询" ); /** 查询按钮 */ |
btn_borrow= new JButton( "借书" ); /** 查借书按钮 */ |
btn_exit= new JButton( "退出窗口" ); /** 退出窗口按钮 */ |
/** 给按钮注册侦听器*/ |
registerActionListener(); |
/** 拼装 */ |
panel_table.add(panel_tablet); |
// panel_table.add(table); |
|
panel_buttons.add(lb_type); |
panel_buttons.add(cb_type); |
panel_buttons.add(btn_search); |
panel_buttons.add(btn_borrow); |
panel_buttons.add( new JLabel()); /** 空白 */ |
panel_buttons.add( new JLabel()); /** 空白 */ |
panel_buttons.add(btn_exit); |
|
this .add(panel_table,BorderLayout.CENTER); |
this .add(panel_buttons,BorderLayout.EAST); |
|
this .setTitle( "图书信息查询界面" ); |
this .setIconifiable( true ); /** 设置窗体是否可以最小化 */ |
this .setClosable( true ); /** 内嵌窗体关闭 */ |
this .setDefaultCloseOperation(DISPOSE_ON_CLOSE); /** 设置窗体可以关闭,类型是隐藏自己,释放所占用内存 */ |
this .setSize( 520 , 500 ); /** 设置窗体大小 */ |
this .setVisible( true ); |
|
} |
|
/** 定义一个显瘦图书数据的数据模型 */ |
private class BookDateModel implements TableModel{ |
|
/** 显示什么数据 */ |
private List<Book> book_list= null ; |
|
public void setBooks_list(List<Book> book_list){ |
this .book_list = book_list; |
} |
@Override |
public int getRowCount() { /** 需要显示多少行 */ |
return book_list.size(); |
} |
@Override |
public int getColumnCount() { /** 显示多少列 */ |
return 4 ; |
} |
@Override /** select book_id,book_name,book_count,status from book,获取列名的方法 */ |
public String getColumnName( int columnIndex) { /** 根据行号,列号获取值 */ |
if (columnIndex== 0 ) { |
return "图书编号" ; |
} else if (columnIndex== 1 ) |
{ |
return "图书名称" ; |
} else if (columnIndex== 2 ) |
{ |
return "借阅次数" ; |
} else if (columnIndex== 3 ) |
{ |
return "是否可借" ; |
} else { |
return "出错" ; |
} |
} |
@Override |
public Class<?> getColumnClass( int columnIndex) { |
return String. class ; |
} |
@Override |
public boolean isCellEditable( int rowIndex, int columnIndex) { |
return false ; |
} |
@Override |
public Object getValueAt( int rowIndex, int columnIndex) { |
Book book=book_list.get(rowIndex); |
if (columnIndex== 0 ) { |
return book.getBook_id(); |
} else if (columnIndex== 1 ) |
{ |
return book.getBook_name(); |
} else if (columnIndex== 2 ) |
{ |
return book.getBook_count(); |
} else if (columnIndex== 3 ) |
{ |
return book.getStatus()== 1 ? "可借" : "不可借" ; |
} else { |
return "出错" ; |
} |
|
} |
@Override |
public void setValueAt(Object aValue, int rowIndex, int columnIndex) { |
|
} |
@Override |
public void addTableModelListener(TableModelListener l) { |
|
} |
@Override |
public void removeTableModelListener(TableModelListener l) { |
|
} |
|
} |
|
/** 给所有按钮注册侦听器 */ |
private void registerActionListener() |
{ |
/** 给table控件注册侦听器 */ |
table.addMouseListener( new MouseAdapter() { |
public void mouseClicked(MouseEvent e) { |
/** 获取用户选中图书的编号 */ |
int row_num=table.getSelectedRow(); |
book_id=( int ) table.getValueAt(row_num, 0 ); |
|
|
} |
}); |
|
/** 给借书按钮注册侦听器 */ |
btn_borrow.addActionListener( new ActionListener() { |
|
@Override |
public void actionPerformed(ActionEvent e) { |
if (book_id== 0 ) { |
JOptionPane.showMessageDialog(UserBookInquiry. this , "请选择你要借的书!" ); |
return ; |
} |
|
/** 1.获取用户选中的图书 2.调用biz中的方法完成借阅功能 */ |
boolean tmp=usersBiz.borrowBook(user.getUser_id(), book_id); |
if (tmp) { |
JOptionPane.showMessageDialog(UserBookInquiry. this , "借书成功" ); |
} else |
{ |
JOptionPane.showMessageDialog(UserBookInquiry. this , "借书不成功" ); |
} |
} |
}); |
|
/** 给查询按钮注册侦听器 */ |
btn_search.addActionListener( new ActionListener() { |
|
@Override |
public void actionPerformed(ActionEvent e) { |
List<Book> books= null ; |
int index=cb_type.getSelectedIndex(); /** 获取下拉框选项的索引,从0开始 */ |
if (index== 0 ) { |
books=bookBiz.queryAll(); /** 查询全部书籍 */ |
} |
if (index== 1 ) { |
books=bookBiz.queryHotBooks(); /** 查询热门图书信息 */ |
} |
if (index== 2 ) { |
books=bookBiz.queryCanBorrowBooks(); /** 查询可借图书信息 */ |
} |
if (index== 3 ) { |
books=bookBiz.queryNotCanBorrowBooks(); /** 查询不可借图书信息 */ |
} |
refreshTable(books); |
} |
}); |
|
|
btn_exit.addActionListener( new ActionListener() { |
|
@Override |
public void actionPerformed(ActionEvent e) { |
dispose(); |
} |
}); |
|
} |
|
/** 定义一个刷新表的方法 */ |
private void refreshTable(List<Book> books) |
{ |
BookDateModel dm = new BookDateModel(); |
dm.setBooks_list(books); /** 给书籍集合赋值,不然没有书籍可以显示 */ |
table.setModel(dm); /** 现在把模型(含有数据的模型)交给Table控件 */ |
} |
|
} |