📄 lendorborrowframe.java
字号:
//借书和还书窗口
import java.awt.*;
import javax.swing.*;
import java.net.URL;
import java.sql.*;
import java.awt.event.*;
import java.applet.*;
public class LendOrBorrowFrame extends JFrame implements ActionListener{
static final long serialVersionUID=80;
JLabel cardlabel,booklabel,thislabel,havelabel;
JButton validatebutton,addbutton,retractbutton,clearbutton;
JButton commitbutton;
JTextField cardfield,bookfield;
JTable thistable,havetable;
JScrollPane thisscroll,havescroll;
JProgressBar thisprogress,haveprogress;
Container container;
Object[][] thistime=new Object[8][5],havetime=new Object[8][5];
Object[] columnName={"书号","书名","出版社","作者","类型"};
Connection con;
Statement sql;
Dimension screenSize;
Toolkit tool;
AudioClip audio;
URL audiourl;
Image myimage;
boolean lend;
String message=null;
int canlend,haveborrow,count,canbook;
PopupListener pl;
JPopupMenu popup;
JMenuItem borrowitem;
LendOrBorrowFrame(String title,boolean lend,Connection con){
super(title);
this.lend=lend;
tool=getToolkit();
URL url = getClass().getResource("/images/lendorborrow.gif");
if(url!=null){
myimage=tool.getImage(url);
setIconImage(myimage);
}
screenSize=tool.getScreenSize();
setResizable(false);
setSize(394,419);
container=getContentPane();
container.setBackground(Color.ORANGE);
container.setLayout(null);
cardlabel=new JLabel("读者卡号");
booklabel=new JLabel("书号");
if(lend){
thislabel=new JLabel("本次借书");
}
else{
thislabel=new JLabel("本次还书");
borrowitem=new JMenuItem("还书");
borrowitem.setIcon(new ImageIcon(getClass().getResource("/images/icon/borrow.png")));
borrowitem.addActionListener(this);
popup=new JPopupMenu();
popup.add(borrowitem);
pl=new PopupListener(popup);
}
havelabel=new JLabel("已借图书");
validatebutton=new JButton("验证");
clearbutton=new JButton("清除");
addbutton=new JButton("添加");
retractbutton=new JButton("撤消");
commitbutton=new JButton("全部提交");
validatebutton.addActionListener(this);
clearbutton.addActionListener(this);
addbutton.addActionListener(this);
retractbutton.addActionListener(this);
commitbutton.addActionListener(this);
cardfield=new JTextField(12);
bookfield=new JTextField(12);
thistable=new JTable(thistime,columnName);
havetable=new JTable(havetime,columnName);
if(!lend){
havetable.addMouseListener(pl);
}
thisscroll=new JScrollPane(thistable);
havescroll=new JScrollPane(havetable);
thisprogress=new JProgressBar(JProgressBar.HORIZONTAL,0,20);
haveprogress=new JProgressBar(JProgressBar.HORIZONTAL,0,20);
thisprogress.setStringPainted(true);
haveprogress.setStringPainted(true);
container.add(cardlabel);
cardlabel.setBounds(20,10,60,30);
container.add(cardfield);
cardfield.setBounds(80,15,140,20);
container.add(validatebutton);
validatebutton.setBounds(230,12,70,25);
container.add(clearbutton);
clearbutton.setBounds(310,12,70,25);
container.add(booklabel);
booklabel.setBounds(30,40,60,25);
container.add(bookfield);
bookfield.setBounds(80,45,140,20);
container.add(addbutton);
addbutton.setBounds(230,42,70,25);
container.add(retractbutton);
retractbutton.setBounds(310,42,70,25);
container.add(thislabel);
thislabel.setBounds(5,70,60,30);
container.add(thisscroll);
thisscroll.setBounds(0,95,365,120);
container.add(thisprogress);
thisprogress.setBounds(365,95,25,120);
container.add(havelabel);
havelabel.setBounds(5,215,60,30);
container.add(havescroll);
havescroll.setBounds(0,240,365,120);
container.add(haveprogress);
haveprogress.setBounds(365,240,25,120);
container.add(commitbutton);
commitbutton.setBounds(0,360,390,30);
validate();
try{
sql=con.createStatement();
}
catch(SQLException e2){
JOptionPane.showMessageDialog(this,"计算机已与服务器断开");
}
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e2){
try{
sql.close();
}
catch(SQLException e1){
JOptionPane.showMessageDialog(null,"计算机已与服务器断开");
}
dispose();
}
});
setLocation((screenSize.width-getSize().width)/2,(screenSize.height-getSize().height)/2+50);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
String card=cardfield.getText().trim();
String book=bookfield.getText().trim();
if(e.getSource()==validatebutton){
bookfield.setText(null);
if(card.length()!=0){
String validate="SELECT * FROM Reader_Info WHERE ReaderCardID = "+"'"+card+"'";
try{
ResultSet rs=sql.executeQuery(validate);
int m=0;
if(rs.next()){
audiourl=getClass().getResource("/sound/Ding.wav");
if(audiourl!=null){
audio=Applet.newAudioClip(audiourl);
audio.play();
}
resetthistable();
resethavetable();
String havelend="SELECT * FROM Book_Info,Lend_Book WHERE ReaderCardID = "+"'"+card+"'"+" AND Lend_Book.BookID = Book_Info.BookID";
ResultSet res=sql.executeQuery(havelend);
while(res.next()){
if(m==havetime.length){
container.remove(havescroll);
Object[][] temp=havetime;
havetime=new Object[temp.length*2][5];
havetable=new JTable(havetime,columnName);
if(!lend){
havetable.addMouseListener(pl);
}
havescroll=new JScrollPane(havetable);
container.add(havescroll);
havescroll.setBounds(0,240,365,120);
validate();
for(int i=0;i<temp.length;i++){
for(int j=0;j<5;j++){
havetime[i][j]=temp[i][j];
}
}
}
for(int i=0;i<5;i++){
havetime[m][i]=res.getString(i+1);
}
m++;
}
haveprogress.setValue(m);
haveprogress.setString(""+m+"");
thisprogress.setValue(0);
thisprogress.setString(""+0);
havetable.repaint();
thistable.repaint();
count=0;
message=card;
String purview="SELECT BorrowNumber,LendBook FROM Reader_Info,Reader_Type WHERE ReaderCardID = "+"'"+message+"'"+" AND Reader_Info.ReaderType = Reader_Type.ReaderType";
ResultSet result=sql.executeQuery(purview);
if(result.next()){
haveborrow=result.getInt(1);
canlend=result.getInt(2);
canbook=canlend-haveborrow;
}
if(lend){
this.setTitle(message+"正在借书,还可以借"+canbook+"本书.");
}
else{
this.setTitle(message+"正在还书");
}
}
else{
JOptionPane.showMessageDialog(this,"没有这个读者");
}
}
catch(SQLException e2){
JOptionPane.showMessageDialog(this,"计算机已与服务器断开");
return;
}
}
}
else if(e.getSource()==addbutton){
if(lend){
if(book.length()!=0){
if(message!=null){
if(canlend-haveborrow>0){
boolean havehave=false;
for(int i=0;i<thistime.length;i++){
if(thistime[i][0]!=null){
String what=thistime[i][0].toString().trim();
if(what.equals(book)){
havehave=true;
break;
}
}
}
if(!havehave){
int n;
String samebook="SELECT * FROM Lend_Book WHERE ReaderCardID = "+message+" AND BookID = "+book;
String lastbook="SELECT * FROM Book_Info WHERE BookID = "+book;
try{
ResultSet rs=sql.executeQuery(samebook);
if(rs.next()){
JOptionPane.showMessageDialog(this,"这个读者借了一本同样的书还未还","警告对话框",JOptionPane.WARNING_MESSAGE);
return;
}
ResultSet resultset=sql.executeQuery(lastbook);
if(resultset.next()){
n=resultset.getInt(9);
if(n==0){
JOptionPane.showMessageDialog(this,"此书已全部出借");
return;
}
boolean full=true;
for(int i=0;i<thistime.length;i++){
if(thistime[i][0]==null){
for(int j=0;j<5;j++){
thistime[i][j]=resultset.getString(j+1);
}
count++;
haveborrow++;
canbook--;
setTitle(message+"正在借书,还可以借"+canbook+"本书.");
thisprogress.setValue(count);thisprogress.setString(""+count+"");
full=false;
thistable.repaint();
break;
}
}
if(full){
extend();
for(int j=0;j<5;j++){
thistime[thistime.length/2][j]=resultset.getString(j+1);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -