📄 bookviewframe.java
字号:
/****************************************************************
* 读者浏览图书窗体 *
*****************************************************************/
package frames;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.util.*;
import booksys.*;
public class bookviewframe extends JFrame{
Vector book = new Vector();//book矢量集
Vector bookname = new Vector();
Vector bookstore = new Vector();//book数量矢量集
Vector readerbook = new Vector();//借书矢量集
String[][] book1;
String[] bookname1;
books books1;//当前书籍对象
bookstores stores1;//当前书库对象
int index=0;
int zs;
String s;
boolean nmstate,authorstate,publisherstate;//checkbox状态
JPanel p = new JPanel();
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
JPanel p4 = new JPanel();
JPanel p5 = new JPanel();
JPanel p6 = new JPanel();
JCheckBox nmc = new JCheckBox("书名");
JCheckBox authorc = new JCheckBox("作者名");
JCheckBox publisherc = new JCheckBox("出版社名");
JTextField nmct = new JTextField(10);
JTextField authorct = new JTextField(10);
JTextField publisherct = new JTextField(10);
JLabel booknm = new JLabel("书名:");
JLabel publisher = new JLabel("出版社:");
JLabel author1 = new JLabel("作者1:");
JLabel author2 = new JLabel("作者2:");
JLabel isbn = new JLabel("ISBN:");
JLabel price = new JLabel("单价:");
JLabel version = new JLabel("版本:");
JLabel allcount = new JLabel("状态");
static JTable t1;
static JScrollPane scrollp;
JTextField booknmt = new JTextField(10);
JTextField publishert = new JTextField(10);
JTextField authort1 = new JTextField(10);
JTextField authort2 = new JTextField(10);
JTextField isbnt = new JTextField(10);
JTextField pricet = new JTextField(10);
JTextField versiont = new JTextField(10);
JTextField allcountt = new JTextField(10);
JTextField number = new JTextField(50);
JButton firstb = new JButton("第一条");
JButton privb = new JButton("上一条");
JButton nextb = new JButton("下一条");
JButton lastb = new JButton("最后一条");
JButton exitb = new JButton("退出");
JButton orderb = new JButton("预定图书");
JButton cancelorderb = new JButton("取消预定");
JButton check = new JButton("查找");
public bookviewframe(){
//this.addWindowListener(new winopen());
// win();
this.setBounds(50,50,600,500);
this.setTitle("");
p1.setSize(400,500);
p1.setLayout(new GridLayout(4,4,5,15));
p2.setLayout(new FlowLayout(FlowLayout.CENTER));
p4.setLayout(new FlowLayout(FlowLayout.CENTER));
p5.setLayout(new GridLayout(2,3,15,0));
p6.setLayout(new FlowLayout());
Dimension d = new Dimension(500,100);
Dimension d1 = new Dimension(800,100);
// t1 = new JTable(book1,bookname1);
// t1.setPreferredSize(d1);
JTextArea j = new JTextArea(4,50);
scrollp = new JScrollPane(t1,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
check.addActionListener(new checkl());
p6.add(check);
orderb.addActionListener(new orderl());
p6.add(orderb);
cancelorderb.addActionListener(new cancelorderl());
cancelorderb.setEnabled(false);
p6.add(cancelorderb);
p5.setBorder(BorderFactory.createTitledBorder("请选择查询类型"));
nmc.addItemListener(new nmc());
p5.add(nmc);
authorc.addItemListener(new authorc());
p5.add(authorc);
publisherc.addItemListener(new publisherc());
p5.add(publisherc);
nmct.setEditable(false);
p5.add(nmct);
authorct.setEditable(false);
p5.add(authorct);
publisherct.setEditable(false);
p5.add(publisherct);
p1.add(booknm);
p1.add(booknmt);
p1.add(publisher);
p1.add(publishert);
p1.add(author1);
p1.add(authort1);
p1.add(author2);
p1.add(authort2);
p1.add(isbn);
p1.add(isbnt);
p1.add(price);
p1.add(pricet);
p1.add(version);
p1.add(versiont);
p1.add(allcount);
p1.add(allcountt);
p2.add(scrollp);
scrollp.setPreferredSize(d);
p3.add(number);
//check.addActionListener(new check());
firstb.addActionListener(new first());
p4.add(firstb);
privb.addActionListener(new priv());
p4.add(privb);
nextb.addActionListener(new next());
p4.add(nextb);
lastb.addActionListener(new last());
p4.add(lastb);
exitb.addActionListener(new exit());
p4.add(exitb);
p.add(p5);
p.add(p1);
p.add(p6);
p.add(p2);
p.add(p3);
p.add(p4);
this.setContentPane(p);
try{
setlook("windows");
}catch(Exception sete){
JOptionPane.showMessageDialog(null,sete.getMessage());
}
this.setVisible(true);
}
public void win(){
try{
settable();
filltext();
s = "共有"+String.valueOf(zs)+"本书,这是第"+String.valueOf(index+1)+"本。";
number.setText(s);
}catch(Exception we){
}
}
public void filltext() throws Exception{
if(book.size()>0){
books1 = (books)book.elementAt(index);
stores1 = (bookstores)bookstore.elementAt(index);
booknmt.setText(books1.getnm().trim());
String s1 = publishers.searchnm(books1.getpublisherid());
publishert.setText(s1.trim());
authort1.setText(books1.getauthor1().trim());
authort2.setText(books1.getauthor2().trim());
isbnt.setText(books1.getisbn().trim());
pricet.setText(String.valueOf(books1.getprice()).trim());
versiont.setText(books1.getversion().trim());
if(stores1.currentcount>stores1.lowlimit)
allcountt.setText("可以借阅");
else
allcountt.setText("不可以借阅");
}
}
class first implements ActionListener{
public void actionPerformed(ActionEvent e){
try{
index=0;
filltext();
s = "共有"+String.valueOf(zs)+"本书,这是第"+String.valueOf(index+1)+"本。";
number.setText(s);
check();
}catch(Exception fe){
JOptionPane.showMessageDialog(null,fe.getMessage());
}
}
}
class priv implements ActionListener{
public void actionPerformed(ActionEvent e){
try{
if(index>0){
index-=1;
filltext();
s = "共有"+String.valueOf(zs)+"本书,这是第"+String.valueOf(index+1)+"本。";
number.setText(s);
check();
}else{
JOptionPane.showMessageDialog(null,"已到第一条记录!");
}
}catch(Exception fe){
JOptionPane.showMessageDialog(null,fe.getMessage());
}
}
}
class next implements ActionListener{
public void actionPerformed(ActionEvent e){
try{
if(index<book.size()-1){
index+=1;
filltext();
s = "共有"+String.valueOf(zs)+"本书,这是第"+String.valueOf(index+1)+"本。";
number.setText(s);
check();
}else{
JOptionPane.showMessageDialog(null,"已到最后一条!");
}
}catch(Exception fe){
JOptionPane.showMessageDialog(null,fe.getMessage());
}
}
}
class last implements ActionListener{
public void actionPerformed(ActionEvent e){
try{
index=book.size()-1;
filltext();
s = "共有"+String.valueOf(zs)+"本书,这是第"+String.valueOf(index+1)+"本。";
number.setText(s);
check();
}catch(Exception fe){
JOptionPane.showMessageDialog(null,fe.getMessage());
}
}
}
class exit implements ActionListener{
public void actionPerformed(ActionEvent e){
mainframe.rvb1.setVisible(false);
}
}
class nmc implements ItemListener{
public void itemStateChanged(ItemEvent e){
if(e.getStateChange()==1){
nmstate = true;
nmct.setEditable(true);
}else{
nmstate = false;
nmct.setEditable(false);
}
}
}
class authorc implements ItemListener{
public void itemStateChanged(ItemEvent e){
if(e.getStateChange()==1){
authorstate = true;
authorct.setEditable(true);
}else{
authorstate = false;
authorct.setEditable(false);
}
}
}
class publisherc implements ItemListener{
public void itemStateChanged(ItemEvent e){
if(e.getStateChange()==1){
publisherstate = true;
publisherct.setEditable(true);
}else{
publisherstate = false;
publisherct.setEditable(false);
}
}
}
public void settable() throws Exception{
book.clear();
String sql = "select * from books ";
if(nmstate){
sql = sql + "where booknm='"+nmct.getText()+"'";
}
if(authorstate){
if(sql.indexOf("where")>-1){
sql = sql +" and authors1='"+authorct.getText()+"'";
}else{
sql = sql + "where authors1='"+authorct.getText()+"'";
}
}
if(publisherstate){
int id = publishers.searchid(publisherct.getText());
if(sql.indexOf("where")>-1){
sql = sql +" and publisherid="+id;
}else{
sql = sql + "where publisherid="+id;
}
}
book = books.browse(sql);
book1 = new String[book.size()][9];
zs = book.size();
if(book.size()>0){
for(int i=0;i<book.size();i++){
books b1 = (books)book.elementAt(i);
bookstore.addElement(bookstores.searchcount(b1.getbookid()));
book1[i][0] = String.valueOf(b1.getbookid());
book1[i][1] = b1.getnm();
book1[i][2] = b1.getauthor1();
book1[i][3] = b1.getauthor2();
book1[i][4] = b1.getisbn();
book1[i][5] = String.valueOf(b1.getprice());
book1[i][6] = publishers.searchnm(b1.getpublisherid());
book1[i][7] = b1.getversion();
}
bookname1 = new String[8];
bookname1[0] = "书id";
bookname1[1] = "书名";
bookname1[2] = "作者1";
bookname1[3] = "作者2";
bookname1[4] = "isbn";
bookname1[5] = "单价";
bookname1[6] = "出版社";
bookname1[7] = "版本";
}else{
JOptionPane.showMessageDialog(null,"没有书");
cancelorderb.setEnabled(false);
booknmt.setText("");
publishert.setText("");
authort1.setText("");
authort2.setText("");
isbnt.setText("");
pricet.setText("");
versiont.setText("");
allcountt.setText("");
}
}
public void setlook(String look) throws Exception{//设置外观效果
if(look.equals("java")){
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
SwingUtilities.updateComponentTreeUI(this);
//this.pack();
}
if(look.equals("windows")){
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
SwingUtilities.updateComponentTreeUI(this);
//this.pack();
}
if(look.equals("motif")){
UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
SwingUtilities.updateComponentTreeUI(this);
//this.pack();
}
}
class winopen extends WindowAdapter{
public void windowOpened(WindowEvent e){
try{
readerbook = reader.searchreader(users.users1.getuserid());
String s = users.users1.getuserid().trim()+"您好! 您共借了"+String.valueOf(readerbook.size())+"本书。";
JOptionPane.showMessageDialog(null,s);
}catch(Exception wine){
JOptionPane.showMessageDialog(null,wine.getMessage());
}
}
}
public void order() throws Exception{//预定图书
int respones = JOptionPane.showConfirmDialog(null,"您是否要预定此书?","确认预定",JOptionPane.OK_CANCEL_OPTION);
if(respones==JOptionPane.OK_OPTION){
if(loginframe.r1.checkborrow(loginframe.r1.getuserid())<loginframe.r1.limitcount){
int id = reader.getid();
int bookid = books1.getbookid();
String userid = loginframe.u1.getuserid();
String bookstatus = "预定";
reader r2 = new reader(id,userid,bookid,bookstatus,new Date());
bookstores s1 = bookstores.getid(r2.getbookid(),1);
s1.borrowcount+=1;
s1.currentcount-=1;
if(r2.insert()&&s1.updatebookstores(s1.id)) {
JOptionPane.showMessageDialog(null,"图书已预定");
cancelorderb.setEnabled(true);
}else{
JOptionPane.showMessageDialog(null,"图书未预定,请再试一次!");
}
}else{
JOptionPane.showMessageDialog(null,"您已借满2本,无法再预定图书!");
}
}else{}
}
public void cancelorder() throws Exception{//取消图书
int bookid = books1.getbookid();
String userid = loginframe.u1.getuserid();
reader r2 = reader.getreader(bookid,userid);
bookstores s1 = bookstores.getid(r2.getbookid(),1);
s1.borrowcount-=1;
s1.currentcount+=1;
if(r2.delete(r2.getreadid())&&s1.updatebookstores(s1.id)){
JOptionPane.showMessageDialog(null,"图书已取消");
cancelorderb.setEnabled(true);
}else{
JOptionPane.showMessageDialog(null,"图书未取消,请再试一次!");
}
}
class orderl implements ActionListener{
public void actionPerformed(ActionEvent e){
try{
order();
}catch(Exception ore){
JOptionPane.showMessageDialog(null,ore.getMessage());
}
}
}
class cancelorderl implements ActionListener{
public void actionPerformed(ActionEvent e){
try{
cancelorder();
}catch(Exception ore){
JOptionPane.showMessageDialog(null,ore.getMessage());
}
}
}
class checkl implements ActionListener{
public void actionPerformed(ActionEvent e){
try{
for(int j=0;j<book.size();j++){
for(int k=0;k<8;k++){
t1.setValueAt("",j,k);
}
}
settable();
filltext();
resettable();
check();
s = "共有"+String.valueOf(zs)+"本书,这是第"+String.valueOf(index+1)+"本。";
number.setText(s);
}catch(Exception che){
JOptionPane.showMessageDialog(null,che.getMessage());
}
}
}
void check(){//检查是否预定
try{
if(book.size()>0){
int bookid = books1.getbookid();
String userid = loginframe.u1.getuserid();
if(reader.checkborrow(userid,bookid)) cancelorderb.setEnabled(true);
else cancelorderb.setEnabled(false);
}
}catch(Exception checke){
JOptionPane.showMessageDialog(null,checke.getMessage());
}
}
public void resettable(){
for(int j=0;j<book.size();j++){
for(int k=0;k<8;k++){
t1.setValueAt(book1[j][k],j,k);
}
}
}
public static void main(String[] args){
bookviewframe b=new bookviewframe();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -