?? bookinfomation.java
字號(hào):
//圖書(shū)資料
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.sql.*;
import java.io.*;
import java.util.*;
public class BookInfomation extends JFrame implements ActionListener
{ public JToolBar JTB;
public JButton btn1,btn2,btn3,btn4,btn5,btn7,btn8,btn9;
public JLabel statusLB,statusLB1,statusLB2,statusLB3;
public JPanel jp1,statusPane,statusPane1,statusPane2,statusPane3;
public int books,exit,borrowed,not;
public JTable jta;
public Statement stmt,stmt1,stmt2;
public Connection con,con1,con2;
public JFrame frame;
public Object rowData[][];
public Toolkit tool;
public Dimension wndsize;
public BookInfomation()
{
JFrame.setDefaultLookAndFeelDecorated(true);
frame=new JFrame("圖書(shū)資料管理");
ImageIcon icon=new ImageIcon("images/home.gif");
frame.setIconImage(icon.getImage());
frame.setResizable(false);
Font font1=new Font("宋體",Font.PLAIN,15);
Container content=frame.getContentPane();
content.setLayout(null);
tool=frame.getToolkit();
wndsize=tool.getScreenSize();
int width=tool.getScreenSize().width;
int height=tool.getScreenSize().height;
//--初始化工具欄各個(gè)組件--
btn1=new JButton("查詢(xún)",new ImageIcon("images/findd.gif"));
btn2=new JButton("全部信息",new ImageIcon("images/war.gif"));
btn3=new JButton("新書(shū)錄入",new ImageIcon("images/add.gif"));
btn4=new JButton("更改資料",new ImageIcon("images/BOOKS02.gif"));
btn5=new JButton("注銷(xiāo)資料",new ImageIcon("images/EmptyRec.gif"));
btn7=new JButton("打印預(yù)覽",new ImageIcon("images/text.gif"));
btn8=new JButton("導(dǎo)出數(shù)據(jù)",new ImageIcon("images/save.gif"));
btn9=new JButton("退出",new ImageIcon("images/ren1.gif"));
//--設(shè)置工具欄各個(gè)組件的提示--
btn1.setToolTipText("查詢(xún)圖書(shū)信息");
btn2.setToolTipText("全部圖書(shū)信息");
btn3.setToolTipText("新書(shū)錄入");
btn4.setToolTipText("更改圖書(shū)資料");
btn5.setToolTipText("注銷(xiāo)圖書(shū)資料");
btn7.setToolTipText("打印預(yù)覽");
btn8.setToolTipText("導(dǎo)出數(shù)據(jù)");
btn9.setToolTipText("退出");
//--為工具欄中各個(gè)組件添加事件--
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
btn4.addActionListener(this);
btn5.addActionListener(this);
btn7.addActionListener(this);
btn8.addActionListener(this);
btn9.addActionListener(this);
//--為工具欄中各個(gè)組件設(shè)置字體--
btn1.setFont(font1);
btn2.setFont(font1);
btn3.setFont(font1);
btn4.setFont(font1);
btn5.setFont(font1);
btn7.setFont(font1);
btn8.setFont(font1);
btn9.setFont(font1);
//--添加工具欄各個(gè)組件--
JTB=new JToolBar();
JTB.addSeparator();
JTB.addSeparator();
JTB.add(btn1);
JTB.add(btn2);
JTB.addSeparator();
JTB.add(btn3);
JTB.add(btn4);
JTB.add(btn5);
JTB.addSeparator();
JTB.add(btn7);
JTB.add(btn8);
JTB.addSeparator();
JTB.add(btn9);
JTB.setFont(font1);
JTB.setFloatable(false);
JTB.setBorder(BorderFactory.createLoweredBevelBorder());
JTB.setBounds(0,2,width-10,40);
//--初始化基本設(shè)置各個(gè)組件--
Object columnNames[]={"書(shū)名","編號(hào)","條形碼","分類(lèi)法","作者","類(lèi)型","價(jià)格","出版社名稱(chēng)","頁(yè)碼","書(shū)架名稱(chēng)","出版日期","入庫(kù)日期","庫(kù)存量","借閱次數(shù)","圖書(shū)狀態(tài)","操作員","摘要"};
rowData=new Object[1000][17];
jta=new JTable(rowData,columnNames);
jta.setRowHeight(20);
jta.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
jta.setPreferredScrollableViewportSize(new Dimension(500,30));
JScrollPane scrollPane=new JScrollPane(jta,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
//--設(shè)置各個(gè)組件的大小和位置--
jta.setBounds(10,25,1000,height-140);
scrollPane.setBounds(10,25,width-40,height-140);
try{ view();}
catch(SQLException ed){}
//--設(shè)置各個(gè)組件的字體--
jta.setFont(font1);
//--將各個(gè)組件添加到容器中--
jp1=new JPanel();
jp1.setLayout(null);
jp1.add(scrollPane);
jp1.setBorder(BorderFactory.createTitledBorder("基本設(shè)置"));
jp1.setBounds(8,45,width-20,height-105);
//--創(chuàng)建狀態(tài)欄--
statusPane=new JPanel();
statusPane1=new JPanel();
statusPane2=new JPanel();
statusPane3=new JPanel();
statusPane.setLayout(null);
statusPane1.setLayout(null);
statusPane2.setLayout(null);
statusPane3.setLayout(null);
//--初始化狀態(tài)欄中各個(gè)組件--
statusLB=new JLabel("圖書(shū)總數(shù)量為:"+books+"(冊(cè))",JLabel.CENTER);
statusLB1=new JLabel("在館圖書(shū)數(shù)量為:"+exit+"(冊(cè))",JLabel.CENTER);
statusLB2=new JLabel("已借出圖書(shū)數(shù)量為:"+borrowed+"(冊(cè))",JLabel.CENTER);
statusLB3=new JLabel("已掛失圖書(shū)數(shù)量為:"+not+"(冊(cè))",JLabel.CENTER);
//--設(shè)置狀態(tài)欄中各個(gè)組件的字體--
statusLB.setFont(font1);
statusLB1.setFont(font1);
statusLB2.setFont(font1);
statusLB3.setFont(font1);
//--設(shè)置狀態(tài)欄中各個(gè)組件的大小和位置--
statusLB.setBounds(0,0,width/4,25);
statusLB1.setBounds(0,0,width/4,25);
statusLB2.setBounds(0,0,width/4,25);
statusLB3.setBounds(0,0,width/4,25);
statusPane.setBounds(0,height-59,width/4,25);
statusPane1.setBounds(width/4,height-59,width/4,25);
statusPane2.setBounds(width/2,height-59,width/4,25);
statusPane3.setBounds((3*width)/4,height-59,width/4,25);
//--添加狀態(tài)欄--
statusPane.add(statusLB);
statusPane.setBorder(BorderFactory.createLoweredBevelBorder());
statusPane1.add(statusLB1);
statusPane1.setBorder(BorderFactory.createLoweredBevelBorder());
statusPane2.add(statusLB2);
statusPane2.setBorder(BorderFactory.createLoweredBevelBorder());
statusPane3.add(statusLB3);
statusPane3.setBorder(BorderFactory.createLoweredBevelBorder());
//--把各個(gè)組件添加到窗口中--
content.add(JTB);
content.add(jp1);
content.add(statusPane);
content.add(statusPane1);
content.add(statusPane2);
content.add(statusPane3);
frame.setSize(wndsize);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{ Object obj=e.getSource();
if(obj==btn1)//圖書(shū)查詢(xún)按鈕事件
{new BookInfo(this);}
if(obj==btn2)//全部信息按鈕事件
{try{ setnull();}
catch(SQLException ed){}
try{ view();}
catch(SQLException ed){}
}
if(obj==btn3)//新書(shū)錄入按鈕事件
{Book book=new Book(this);
book.jtfd2.setEnabled(true);
book.jtfd3.setEnabled(true);
book.btn2.setEnabled(false);
book.btn5.setEnabled(false);
book.btn6.setEnabled(false);
book.btn7.setEnabled(false);
}
if(obj==btn4)//圖書(shū)資料更新按鈕事件
{Book book=new Book(this);
book.frame.setTitle("圖書(shū)資料更新");
book.jtfd2.setEnabled(false);
book.jtfd3.setEnabled(false);
book.btn1.setEnabled(false);
book.btn2.setEnabled(false);
book.btn7.setEnabled(false);
}
if(obj==btn5)//圖書(shū)資料刪除按鈕事件
{Book book=new Book(this);
book.frame.setTitle("圖書(shū)出庫(kù)");
book.jtfd2.setEnabled(true);
book.jtfd3.setEnabled(true);
book.btn1.setEnabled(false);
book.btn5.setEnabled(false);
book.btn6.setEnabled(false);
}
if(obj==btn7)//打印預(yù)覽按鈕事件
{new BookInfo(this);}
if(obj==btn8)//導(dǎo)出數(shù)據(jù)按鈕事件
{new BookInfo(this);}
if(obj==btn9)//退出按鈕事件
{frame.dispose();}
}
/////////////////////設(shè)置圖書(shū)信息表格為空///////////////////////////////////////////////////////////////////
public void setnull() throws SQLException
{int i=0;
try{
String strurl =
"jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=DataBase/Library.mdb";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con= DriverManager.getConnection(strurl);
stmt = con.createStatement();
ResultSet rs=stmt.executeQuery("SELECT * From 圖書(shū)資料");
while(rs.next())
{
rowData[i][0]=null;rowData[i][1]=null;rowData[i][2]=null;
rowData[i][3]=null;rowData[i][4]=null;rowData[i][5]=null;
rowData[i][6]=null;rowData[i][7]=null;rowData[i][8]=null;
rowData[i][9]=null;rowData[i][10]=null;rowData[i][11]=null;
rowData[i][12]=null;rowData[i][13]=null;rowData[i][14]=null;
rowData[i][15]=null;rowData[i][16]=null;
i++;
validate();
}
rowData[i][0]=null;rowData[i][1]=null;rowData[i][2]=null;
rowData[i][3]=null;rowData[i][4]=null;rowData[i][5]=null;
rowData[i][6]=null;rowData[i][7]=null;rowData[i][8]=null;
rowData[i][9]=null;rowData[i][10]=null;rowData[i][11]=null;
rowData[i][12]=null;rowData[i][13]=null;rowData[i][14]=null;
rowData[i][15]=null;rowData[i][16]=null;
jta.repaint();
}
catch(Exception g)
{g.printStackTrace();}
}
/////////////////////連接表格和數(shù)據(jù)庫(kù)///////////////////////////////////////////////////////////////////
public void view() throws SQLException
{ //--連接表格和數(shù)據(jù)庫(kù)--
int i=0;
books=0;exit=0;borrowed=0;not=0;
try{
String strurl1 =
"jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=DataBase/Library.mdb";
try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");}
catch (ClassNotFoundException eg) {}
con1= DriverManager.getConnection(strurl1);
stmt1= con1.createStatement();
ResultSet rs1 = stmt1.executeQuery("select * from 圖書(shū)資料 ORDER BY 編號(hào)");
while(rs1.next())
{String bookname=rs1.getString(1);
String bianhao=rs1.getString(2);
String bookcode=rs1.getString(3);
String fenlei=rs1.getString(4);
String zuozhe=rs1.getString(5);
String type=rs1.getString(6);
String price=rs1.getString(7);
String press=rs1.getString(8);
String page=rs1.getString(9);
String sheefname=rs1.getString(10);
String chuban=rs1.getString(11);
String ruku=rs1.getString(12);
String stock=rs1.getString(13);
String borrow=rs1.getString(14);
String status=rs1.getString(15);
String oparator=rs1.getString(16);
String key=rs1.getString(17);
books++;
if(status.equals("在架"))
{exit++;}
else if(status.equals("已借出"))
{borrowed++;}
else if(status.equals("已掛失"))
{not++;}
rowData[i][0]=bookname;
rowData[i][1]=bianhao;
rowData[i][2]=bookcode;
rowData[i][3]=fenlei;
rowData[i][4]=zuozhe;
rowData[i][5]=type;
rowData[i][6]=price;
rowData[i][7]=press;
rowData[i][8]=page;
rowData[i][9]=sheefname;
rowData[i][10]=chuban;
rowData[i][11]=ruku;
rowData[i][12]=stock;
rowData[i][13]=borrow;
rowData[i][14]=status;
rowData[i][15]=oparator;
rowData[i][16]=key;
i++;
}
jta.repaint();
con1.close();
}
catch(Exception es){//JOptionPane.showMessageDialog(null, "數(shù)據(jù)庫(kù)中沒(méi)有成績(jī)信息", "警告",
//JOptionPane.WARNING_MESSAGE);
es.printStackTrace();}
}
public static void main(String[]args)
{ try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}
catch(Exception e) {
e.printStackTrace();
}
new BookInfomation();
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -