⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bookinfomation.java

📁 JAVA 图书馆管理系统 是个学习JAVA 语言的好材料
💻 JAVA
字号:
//图书资料
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("图书资料管理");
  	    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;

        //--初始化工具栏各个组件--
        btn1=new JButton("查询",new ImageIcon("images/findd.gif"));
		btn2=new JButton("全部信息",new ImageIcon("images/war.gif"));
		btn3=new JButton("新书录入",new ImageIcon("images/add.gif"));
		btn4=new JButton("更改资料",new ImageIcon("images/BOOKS02.gif"));
		btn5=new JButton("注销资料",new ImageIcon("images/EmptyRec.gif"));
	    btn7=new JButton("打印预览",new ImageIcon("images/text.gif"));
		btn8=new JButton("导出数据",new ImageIcon("images/save.gif"));
	    btn9=new JButton("退出",new ImageIcon("images/ren1.gif"));

	    //--设置工具栏各个组件的提示--
	    btn1.setToolTipText("查询图书信息");
		btn2.setToolTipText("全部图书信息");
		btn3.setToolTipText("新书录入");
		btn4.setToolTipText("更改图书资料");
		btn5.setToolTipText("注销图书资料");
	    btn7.setToolTipText("打印预览");
	    btn8.setToolTipText("导出数据");
	    btn9.setToolTipText("退出");

	    //--为工具栏中各个组件添加事件--
	    btn1.addActionListener(this);
	    btn2.addActionListener(this);
	    btn3.addActionListener(this);
	    btn4.addActionListener(this);
	    btn5.addActionListener(this);
	    btn7.addActionListener(this);
		btn8.addActionListener(this);
	    btn9.addActionListener(this);

	    //--为工具栏中各个组件设置字体--
	    btn1.setFont(font1);
		btn2.setFont(font1);
		btn3.setFont(font1);
		btn4.setFont(font1);
		btn5.setFont(font1);
		btn7.setFont(font1);
		btn8.setFont(font1);
		btn9.setFont(font1);

        //--添加工具栏各个组件--
        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);

        //--初始化基本设置各个组件--
        Object columnNames[]={"书名","编号","条形码","分类法","作者","类型","价格","出版社名称","页码","书架名称","出版日期","入库日期","库存量","借阅次数","图书状态","操作员","摘要"};
        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);

		//--设置各个组件的大小和位置--
		jta.setBounds(10,25,1000,height-140);
        scrollPane.setBounds(10,25,width-40,height-140);
        try{ view();}
	    catch(SQLException ed){}
		//--设置各个组件的字体--
		jta.setFont(font1);

		//--将各个组件添加到容器中--
		jp1=new JPanel();
		jp1.setLayout(null);
		jp1.add(scrollPane);
		jp1.setBorder(BorderFactory.createTitledBorder("基本设置"));
        jp1.setBounds(8,45,width-20,height-105);

        //--创建状态栏--
		      statusPane=new JPanel();
		      statusPane1=new JPanel();
		      statusPane2=new JPanel();
		      statusPane3=new JPanel();
		      statusPane.setLayout(null);
		      statusPane1.setLayout(null);
		      statusPane2.setLayout(null);
		      statusPane3.setLayout(null);

		      //--初始化状态栏中各个组件--
		      statusLB=new JLabel("图书总数量为:"+books+"(册)",JLabel.CENTER);
		      statusLB1=new JLabel("在馆图书数量为:"+exit+"(册)",JLabel.CENTER);
		      statusLB2=new JLabel("已借出图书数量为:"+borrowed+"(册)",JLabel.CENTER);
		      statusLB3=new JLabel("已挂失图书数量为:"+not+"(册)",JLabel.CENTER);
		      //--设置状态栏中各个组件的字体--
		      statusLB.setFont(font1);
			  statusLB1.setFont(font1);
			  statusLB2.setFont(font1);
		      statusLB3.setFont(font1);

		      //--设置状态栏中各个组件的大小和位置--
		      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);

		      //--添加状态栏--
		      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());


	    //--把各个组件添加到窗口中--
        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)//图书查询按钮事件
	  {new BookInfo(this);}
	  if(obj==btn2)//全部信息按钮事件
	  {try{ setnull();}
	    catch(SQLException ed){}
		try{ view();}
	    catch(SQLException ed){}
	  }
	  if(obj==btn3)//新书录入按钮事件
	  {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)//图书资料更新按钮事件
	  {Book book=new Book(this);
	   book.frame.setTitle("图书资料更新");
	   book.jtfd2.setEnabled(false);
       book.jtfd3.setEnabled(false);
	   book.btn1.setEnabled(false);
	   book.btn2.setEnabled(false);
	   book.btn7.setEnabled(false);
	  }
	  if(obj==btn5)//图书资料删除按钮事件
	  {Book book=new Book(this);
	   book.frame.setTitle("图书出库");
	   book.jtfd2.setEnabled(true);
       book.jtfd3.setEnabled(true);
	   book.btn1.setEnabled(false);
	   book.btn5.setEnabled(false);
	   book.btn6.setEnabled(false);
	  }
	  if(obj==btn7)//打印预览按钮事件
	  {new BookInfo(this);}
	  if(obj==btn8)//导出数据按钮事件
	  {new BookInfo(this);}
	  if(obj==btn9)//退出按钮事件
	  {frame.dispose();}
	}
/////////////////////设置图书信息表格为空///////////////////////////////////////////////////////////////////
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 图书资料");
     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();}

}
/////////////////////连接表格和数据库///////////////////////////////////////////////////////////////////
    public void view() throws SQLException
    { //--连接表格和数据库--
				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 图书资料 ORDER BY 编号");
					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, "数据库中没有成绩信息", "警告",
							                                   //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();
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -