图书管理系统
学习到
1,图片跟着窗口大小拉伸
2,新建窗口时的显示方式
3,添加表格要拉动窗口才看到,.validate();秒杀之
借书
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import java.awt.event.ActionEvent; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Vector; import javax.swing.JButton; import javax.swing.JOptionPane; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.DefaultTableModel; import javax.swing.JLabel; public class Borrow extends Find { JScrollPane scrollPane ; JTable table2; Object shuju[][]; JButton button; private JLabel lblNewLabel; public Borrow() { button = new JButton("借一本"); button.setBounds(384, 232, 113, 27); add(button); table2=new JTable(); Query query=new Query(); query.Connection(); query.setTableName("borrowtable"); query.setSQL("select * from borrowtable"); shuju=query.getRecord(query.getSQL()); // for(int i=0;i<1;i++){ // for(int j=0;j<2;j++){ // System.out.print(shuju[i][j]+" "); // } // } table2.setModel(new DefaultTableModel(shuju, new Object[]{"借书人","书本isbn"})); query.Release(); scrollPane=new JScrollPane(table2); scrollPane.setBounds(713, 179, 193, 276); add(scrollPane); validate(); button.addActionListener(this); lblNewLabel = new JLabel("先查找,再借"); lblNewLabel.setBounds(504, 227, 150, 37); add(lblNewLabel); } public void actionPerformed(ActionEvent e) { super.actionPerformed(e); if(e.getActionCommand()=="借一本"){ int row=table.getSelectedRow(); DefaultTableModel model1 = (DefaultTableModel) table.getModel(); String s1=JOptionPane.showInputDialog(this,"输入借书同学的名字"); String s2=table.getValueAt(row, 0).toString(); // System.out.println(s2); System.out.println(s1); if(s1!=null){ Query query=new Query(); query.Connection(); query.setSQL("select 现有数量 from booktable where isbn='"+s2+"'"); ResultSet rs1=query.exeQuery(query.getSQL()); int number=0; boolean isBorrow=true; try { rs1.next(); // String temp=rs1.getString(0); number=Integer.parseInt(rs1.getString(1)); if(number<1){ isBorrow=false; JOptionPane.showMessageDialog(this, "没有库存了"); } } catch (SQLException e1) { // TODO 自动生成的 catch 块 e1.printStackTrace(); } if(isBorrow){ number--;//借一本 DefaultTableModel model2 = (DefaultTableModel) table2.getModel(); Vector row1 = new Vector(); // 数据行向量,使用它的add()添加元素,比如整数、String、Object等,有几行就new几个行向量 Vector row2 =new Vector(); // System.out.println(s1); // System.out.println(s2); row1.add(s1); row2.add(s2); Vector data = new Vector(); // 数据行向量集,因为列表不止一行,往里面添加数据行向量,添加方法add(row) data.add(row1); data.add(row2); model2.addRow(data); table2.repaint(); query.setSQL("insert into borrowtable values('"+s1+"','"+s2+"')"); query.exeUpdate(query.getSQL()); String temp=String.valueOf(number); query.setSQL("update booktable set 现有数量='"+temp+"' where isbn='"+s2+"'"); query.exeUpdate(query.getSQL()); model1.setValueAt(temp, row, 7); table.repaint(); } } } } }
删除图书
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import java.awt.event.ActionEvent; import javax.swing.JButton; import javax.swing.table.DefaultTableModel; public class DeleteBook extends Find{ DeleteBook(){ JButton button1 = new JButton("删除"); button1.setBounds(381, 232, 113, 27); add(button1); button1.addActionListener(this); } public void actionPerformed(ActionEvent e){ super.actionPerformed(e); if(e.getActionCommand()=="删除"){ int a=table.getSelectedRow(); String s1=table.getValueAt(a, 0).toString(); Query query=new Query(); query.Connection(); query.setSQL("delete from booktable where isbn='"+s1+"'"); query.exeUpdate(query.getSQL()); query.Release(); DefaultTableModel model = (DefaultTableModel) table.getModel(); model.removeRow(a); table.repaint(); } } }
查找图书
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ButtonGroup; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JRadioButton; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.JTextField; import javax.swing.table.DefaultTableModel; public class Find extends JPanel implements ActionListener{ private JTextField text1; private JTextField text2; JTable table; JRadioButton button1 = new JRadioButton("精确查找"); JRadioButton button2 = new JRadioButton("模糊查找"); ButtonGroup group=new ButtonGroup(); JScrollPane scrollPane; Object biaoTou[]={"isbn","书名","作者","出版社","价格","出版日期","库存数量","现有数量"}; Object shuju[][]; DefaultTableModel model; /** * Create the panel. */ public Find() { setLayout(null); button1.setBounds(79, 103, 129, 27); add(button1); button2.setBounds(238, 103, 157, 27); add(button2); JLabel label = new JLabel("书名"); label.setBounds(64, 169, 72, 18); add(label); text1 = new JTextField(); text1.setBounds(139, 166, 189, 24); add(text1); text1.setColumns(10); JLabel label_1 = new JLabel("作者"); label_1.setBounds(64, 237, 53, 18); add(label_1); text2 = new JTextField(); text2.setBounds(139, 234, 189, 24); add(text2); text2.setColumns(10); JButton button = new JButton("确定"); button.setBounds(382, 165, 113, 27); add(button); button.addActionListener(this); group.add(button1); group.add(button2); button1.setSelected(true); } @Override public void actionPerformed(ActionEvent e) { if(e.getActionCommand()=="确定"){ // TODO 自动生成的方法存根 Query query=new Query(); query.Connection(); query.setTableName("booktable"); String s1=text1.getText().trim(); String s2=text2.getText().trim(); if(button1.isSelected()){ if(s1.equals("")&&s2.equals("")){ query.setSQL("select * from booktable"); }else if(s1.equals("")&&!s2.equals("")){ query.setSQL("select * from booktable where 作者='"+s2+"'"); }else if(!s1.equals("")&&s2.equals("")){ query.setSQL("select * from booktable where 书名='"+s1+"'"); }else if(!s1.equals("")&&!s2.equals("")){ query.setSQL("select * from booktable where 作者='"+s2+"' and 书名='"+s1+"'"); } } else if(button2.isSelected()){ if(s1.equals("")&&s2.equals("")){ query.setSQL("select * from booktable"); }else if(s1.equals("")&&!s2.equals("")){ query.setSQL("select * from booktable where 作者 like '"+s2+"%'"); }else if(!s1.equals("")&&s2.equals("")){ query.setSQL("select * from booktable where 书名 like '"+s1+"%'"); }else if(!s1.equals("")&&!s2.equals("")){ query.setSQL("select * from booktable where 作者 like '"+s2+"%' and 书名 like'"+s1+"%'"); } } shuju=query.getRecord(query.getSQL()); query.Release(); table=new JTable(); model=new DefaultTableModel(shuju, biaoTou); table.setModel(model); scrollPane=new JScrollPane(table); scrollPane.setBounds(46, 276, 556, 185); add(scrollPane); table.repaint(); validate(); // for(int i=0;i<shuju.length;i++){ // for(int j=0;j<8;j++){ // System.out.print(shuju[i][j]+" "); // } // System.out.println(); // } } } }
图书入库
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class In extends JPanel implements ActionListener { private JTextField text1; private JTextField text2; private JTextField text3; private JTextField text4; private JTextField text5; private JTextField text6; private JTextField text7; private JTextField text8; /** * Create the panel. */ public In() { setLayout(null); JLabel lblIsbn = new JLabel("isbn"); lblIsbn.setBounds(130, 35, 72, 18); add(lblIsbn); JLabel label = new JLabel("书名"); label.setBounds(130, 91, 72, 18); add(label); JLabel label_1 = new JLabel("作者"); label_1.setBounds(130, 157, 72, 18); add(label_1); JLabel label_2 = new JLabel("价格"); label_2.setBounds(130, 269, 72, 18); add(label_2); JLabel label_3 = new JLabel("出版日期"); label_3.setBounds(130, 336, 72, 18); add(label_3); JLabel label_4 = new JLabel("原有库存"); label_4.setBounds(130, 404, 72, 18); add(label_4); JLabel label_5 = new JLabel("现有库存"); label_5.setBounds(130, 460, 72, 18); add(label_5); JButton button = new JButton("确认"); button.setBounds(172, 508, 113, 27); add(button); button.addActionListener(this); JLabel label_6 = new JLabel("出版社"); label_6.setBounds(130, 221, 72, 18); add(label_6); text1 = new JTextField(); text1.setBounds(227, 32, 130, 24); add(text1); text1.setColumns(10); text2 = new JTextField(); text2.setBounds(228, 88, 129, 24); add(text2); text2.setColumns(10); text3 = new JTextField(); text3.setBounds(229, 151, 128, 24); add(text3); text3.setColumns(10); text4 = new JTextField(); text4.setBounds(227, 218, 130, 24); add(text4); text4.setColumns(10); text5 = new JTextField(); text5.setBounds(227, 266, 130, 24); add(text5); text5.setColumns(10); text6 = new JTextField(); text6.setBounds(227, 333, 130, 24); add(text6); text6.setColumns(10); text7 = new JTextField(); text7.setBounds(227, 401, 130, 24); add(text7); text7.setColumns(10); text8 = new JTextField(); text8.setBounds(227, 457, 130, 24); add(text8); text8.setColumns(10); } @Override public void actionPerformed(ActionEvent e) { // TODO 自动生成的方法存根 String[] s=new String[10]; s[0]=text1.getText(); s[1]=text2.getText(); s[2]=text3.getText(); s[3]=text4.getText(); s[4]=text5.getText(); s[5]=text6.getText(); s[6]=text7.getText(); s[7]=text8.getText(); Query query=new Query(); query.Connection(); query.setSQL("insert into booktable values('"+s[0]+"','"+s[1]+"','"+s[2]+"','"+s[3]+"','"+s[4]+"','"+s[5]+"','"+s[6]+"','"+s[7]+"')"); query.exeUpdate(query.getSQL()); query.Release(); } }
管理员界面
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JMenu; import javax.swing.JMenuItem; public class Librarian extends Read implements ActionListener{ JMenuItem in,show,borrow,returnbook,deletebook,updatebook; private JMenu menu; private JMenuItem menuItem; private JMenuItem menuItem_1; public static void main(String argsp[]){ Librarian frame = new Librarian(); frame.setVisible(true); } public Librarian(){ in=new JMenuItem("图书入库"); borrow=new JMenuItem("借书操作"); returnbook=new JMenuItem("还书操作"); deletebook=new JMenuItem("删除图书"); updatebook=new JMenuItem("更新图书"); menu1.add(updatebook); menu1.add(in); menu1.add(deletebook); menu = new JMenu("借书还书"); menuBar.add(menu); menu.add(borrow); menu.add(returnbook); returnbook.addActionListener(this); borrow.addActionListener(this); in.addActionListener(this); deletebook.addActionListener(this); updatebook.addActionListener(this); } public void actionPerformed(ActionEvent e) { super.actionPerformed(e); if(e.getActionCommand()=="图书入库"){ contentPane.add("In",new In()); card.show(contentPane, "In"); } else if(e.getActionCommand()=="借书操作"){ contentPane.add("Borrow",new Borrow()); card.show(contentPane, "Borrow"); // System.out.println("11"); } else if(e.getActionCommand()=="还书操作"){ contentPane.add("ReturnBook",new ReturnBook()); card.show(contentPane, "ReturnBook"); // System.out.println("22"); } else if(e.getActionCommand()=="删除图书"){ contentPane.add("DeleteBook",new DeleteBook()); card.show(contentPane, "DeleteBook"); // System.out.println("22"); } else if(e.getActionCommand()=="更新图书"){ contentPane.add("UpdateBook",new UpdateBook()); card.show(contentPane, "UpdateBook"); // System.out.println("22"); } } }
数据库查询
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import java.sql.*; import javax.swing.JOptionPane; public class Query { String tableName=""; //表名 String SQL; //SQL语句 Connection con; Statement sm; ResultSet rs=null; Object[][] a; public Query() { try{ Class.forName("com.mysql.jdbc.Driver"); } catch(ClassNotFoundException e) { JOptionPane.showMessageDialog(null, e.getMessage(),"连接出错",JOptionPane.ERROR_MESSAGE); } } public void setTableName(String s) { tableName=s.trim(); } public String getTableName(){ return tableName; } public Connection getCon(){ return con; } public void setSQL(String SQL) { this.SQL=SQL.trim(); } public void Connection() { try { String uri="jdbc:mysql://localhost:3306/bookman"; String id="kooing"; String password=""; con=DriverManager.getConnection(uri,id,password); } catch(SQLException e) { JOptionPane.showMessageDialog(null, e.getMessage(),"数据库连接出错",JOptionPane.ERROR_MESSAGE); } } public String getSQL(){ return SQL; } public ResultSet exeQuery(String s){ try { sm=con.createStatement(); rs=sm.executeQuery(s); // sm.close(); } catch(SQLException e) { JOptionPane.showMessageDialog(null, e.getMessage(),"ִ执行SQL出错",JOptionPane.ERROR_MESSAGE); } return rs; } public int exeUpdate(String s){ int count=0; try { sm=con.createStatement(); count=sm.executeUpdate(s); // sm.close(); } catch(SQLException e) { JOptionPane.showMessageDialog(null, e.getMessage(),"ִ执行SQL出错",JOptionPane.ERROR_MESSAGE); } return count; } public void Release(){ try{ // rs.close(); sm.close(); con.close(); } catch(SQLException e) { JOptionPane.showMessageDialog(null, e.getMessage(),"关闭连接出错",JOptionPane.ERROR_MESSAGE); } } public int getRows(String s){ try{ sm=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY); rs=sm.executeQuery(s); rs.last(); return(rs.getRow()); } catch(SQLException e){return 0;} } public Object[][] getRecord(String s){ try{ DatabaseMetaData metadata=con.getMetaData(); ResultSet tableMessage=metadata.getColumns(null, null, tableName, null); int col=0; //得到结果集的列数 while(tableMessage.next()){ col++; } //得到结果集的行数 int n=getRows(s); a=new Object[n][col]; rs=sm.executeQuery(s); int i=0; while(rs.next()){ for(int k=1;k<=col;k++){ a[i][k-1]=rs.getString(k); } i++; } } catch(SQLException e){} return a; } }
读者类
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import java.awt.BorderLayout; import java.awt.EventQueue; import java.awt.Image; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ComponentAdapter; import java.awt.event.ComponentEvent; import java.net.URL; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import java.awt.CardLayout; public class Read extends JFrame implements ActionListener { JPanel contentPane; private JLabel back=new JLabel(); private JFrame frame=this; static private String id,password; JMenuBar menuBar ; JMenu menu1; CardLayout card; JPanel backPanel=new JPanel(); UpdateId updateId=new UpdateId(); /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { Read frame = new Read(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public Read() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 1020, 666); menuBar = new JMenuBar(); menu1 = new JMenu("图书管理"); setJMenuBar(menuBar); menuBar.add(menu1); JMenuItem Find = new JMenuItem("图书查询"); menu1.add(Find); JMenu menu2 = new JMenu("账号管理"); menuBar.add(menu2); JMenuItem UpdateId = new JMenuItem("修改密码"); menu2.add(UpdateId); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); backPanel.add(back); URL url = this.getClass().getResource("/44.jpg"); ImageIcon addIcon = new ImageIcon(url); back.setIcon(addIcon); // contentPane.add(back, "name_4101392509658"); card=new CardLayout(); contentPane.setLayout(card); // Find find=new Find(); // System.out.println("aaa"+id); // System.out.println("aaa"+password); contentPane.add("backPanel",backPanel); contentPane.add("UpdateId",updateId); Find.addActionListener(this); UpdateId.addActionListener(this); this.addComponentListener(new ComponentAdapter(){ //为主面板添加窗口监听器 @Override public void componentResized(ComponentEvent e) {//窗体变化背景图跟着变 try{ backPanel.remove(back); // System.out.println("111"); back = new JLabel(); // back.setBounds(frame.getX(),frame.getY(),frame.getWidth(), frame.getHeight()); URL url = this.getClass().getResource("/44.jpg"); ImageIcon addIcon = new ImageIcon(url); addIcon.setImage(addIcon.getImage().getScaledInstance(frame.getWidth(), frame.getHeight(),Image.SCALE_DEFAULT )); back.setIcon(addIcon); back.setSize(frame.getWidth(), frame.getHeight()); backPanel.add(back); } catch(Exception e1){ System.out.println(e1.getMessage()); } } }); } static void setId(String a,String b){ id=a; password=b; } @Override public void actionPerformed(ActionEvent e) { // TODO 自动生成的方法存根 if(e.getActionCommand()=="图书查询"){ contentPane.add("Find",new Find()); card.show(contentPane, "Find"); } else if(e.getActionCommand()=="修改密码"){ updateId.setId(id, password); card.show(contentPane, "UpdateId"); } } }
注册账号
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import java.awt.BorderLayout; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ButtonGroup; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JRadioButton; import javax.swing.JTextField; import javax.swing.border.EmptyBorder; public class Register extends JDialog implements ActionListener{ private final JPanel contentPanel = new JPanel(); private JTextField text1; private JTextField text2; ButtonGroup group1=new ButtonGroup(); JRadioButton radio1 = new JRadioButton("管理员"); JRadioButton radio2 = new JRadioButton("读者"); /** * Launch the application. */ public static void main(String[] args) { try { Register dialog = new Register(); dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); dialog.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } /** * Create the dialog. */ public Register() { setBounds(100, 100, 486, 354); getContentPane().setLayout(new BorderLayout()); contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5)); getContentPane().add(contentPanel, BorderLayout.CENTER); contentPanel.setLayout(null); setModal(true); JLabel label = new JLabel("用户"); label.setBounds(61, 60, 72, 18); contentPanel.add(label); JLabel label_1 = new JLabel("密码"); label_1.setBounds(61, 115, 72, 18); contentPanel.add(label_1); radio1.setBounds(121, 152, 82, 27); contentPanel.add(radio1); radio2.setBounds(226, 152, 157, 27); contentPanel.add(radio2); text1 = new JTextField(); text1.setBounds(147, 57, 157, 24); contentPanel.add(text1); text1.setColumns(10); text2 = new JTextField(); text2.setBounds(147, 112, 157, 24); contentPanel.add(text2); text2.setColumns(10); { JPanel buttonPane = new JPanel(); buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT)); getContentPane().add(buttonPane, BorderLayout.SOUTH); { JButton okButton = new JButton("OK"); okButton.setActionCommand("OK"); buttonPane.add(okButton); getRootPane().setDefaultButton(okButton); okButton.addActionListener(this); } group1.add(radio1); group1.add(radio2); radio1.setSelected(true); } } @Override public void actionPerformed(ActionEvent e) { // TODO 自动生成的方法存根 String s1=text1.getText(); String s2=text2.getText(); String s3=""; if(radio1.isSelected()) s3="librarian"; else s3="read"; if(!s1.equals("")&&!s2.equals("")&&!s3.equals("")){ Query query=new Query(); query.Connection(); query.setTableName("idTable"); query.setSQL("insert into idTable values('"+s1+"','"+s2+"','"+s3+"')"); if(query.exeUpdate(query.getSQL())!=-1){ JOptionPane.showMessageDialog(this, "注册成功"); } query.Release(); dispose(); } else JOptionPane.showMessageDialog(this, "请正确填写"); } }
还书
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.ResultSet; import java.sql.SQLException; import javax.swing.JButton; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.DefaultTableModel; public class ReturnBook extends JPanel implements ActionListener{ JScrollPane scrollPane ; JTable table2; Object shuju[][]; JButton button; public ReturnBook() { setLayout(null); button = new JButton("还书"); button.setBounds(177, 353, 63, 27); add(button); button.addActionListener(this); table2=new JTable(); Query query=new Query(); query.Connection(); query.setTableName("borrowtable"); query.setSQL("select * from borrowtable"); shuju=query.getRecord(query.getSQL()); table2.setModel(new DefaultTableModel(shuju, new Object[]{"借书人","书本isbn"})); query.Release(); scrollPane=new JScrollPane(table2); scrollPane.setBounds(96, 44, 193, 276); add(scrollPane); } @Override public void actionPerformed(ActionEvent e) { // TODO 自动生成的方法存根 DefaultTableModel model=(DefaultTableModel) table2.getModel(); int row=table2.getSelectedRow(); String s1=table2.getValueAt(row, 1).toString(); String s2=table2.getValueAt(row, 0).toString(); Query query=new Query(); query.Connection(); query.setSQL("select 现有数量 from booktable where isbn='"+s1+"'"); ResultSet rs=query.exeQuery(query.getSQL()); int number=0; try { rs.next(); number=Integer.parseInt(rs.getString(1)); number++; } catch (SQLException e1) { // TODO 自动生成的 catch 块 e1.printStackTrace(); } String s=String.valueOf(number); query.setSQL("update booktable set 现有数量='"+s+"' where isbn='"+s1+"'"); query.exeUpdate(query.getSQL()); query.setSQL("delete from borrowtable where readid='"+s2+"' and bookid='"+s1+"'"); query.exeUpdate(query.getSQL()); model.removeRow(row); table2.repaint(); } }
登陆界面
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import java.awt.BorderLayout; import java.awt.EventQueue; import java.awt.Image; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ComponentAdapter; import java.awt.event.ComponentEvent; import java.net.URL; import java.sql.ResultSet; import javax.swing.ButtonGroup; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JLayeredPane; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JRadioButton; import javax.swing.JTextField; import javax.swing.border.EmptyBorder; import java.awt.Font; public class SignIn extends JFrame implements ActionListener { private JPanel contentPane; private JTextField text1; private JTextField text2; private JLabel back = new JLabel("New label"); private JFrame frame=this; JRadioButton radio1 = new JRadioButton("管理员"); JRadioButton radio2 = new JRadioButton("读者"); ButtonGroup group=new ButtonGroup(); String work; Query query=new Query(); /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { SignIn frame = new SignIn(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public SignIn() { radio1.setSelected(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 1052, 692); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); contentPane.setLayout(new BorderLayout(0, 0)); setContentPane(contentPane); JLayeredPane panel1 = new JLayeredPane(); contentPane.add(panel1, BorderLayout.CENTER); JPanel panel = new JPanel(); panel.setBounds(86, 273, 437, 298); panel1.add(panel); panel.setLayout(null); JLabel label = new JLabel("账号"); label.setBounds(14, 64, 53, 18); panel.add(label); JLabel label_1 = new JLabel("密码"); label_1.setBounds(14, 95, 72, 18); panel.add(label_1); text1 = new JTextField(); text1.setBounds(57, 61, 135, 24); panel.add(text1); text1.setColumns(10); text2 = new JTextField(); text2.setBounds(57, 95, 135, 24); panel.add(text2); text2.setColumns(10); JLabel label_2 = new JLabel("备注:没有账号的用户先填写下面信息再点注册按钮"); label_2.setFont(new Font("方正兰亭超细黑简体", Font.PLAIN, 16)); label_2.setBounds(0, -12, 437, 84); panel.add(label_2); JButton button1 = new JButton("登陆"); button1.setBounds(0, 186, 113, 27); panel.add(button1); JButton button2 = new JButton("注册"); button2.setBounds(127, 186, 113, 27); panel.add(button2); back.setBounds(0, -49, 1063, 747); URL url = this.getClass().getResource("/11.jpeg"); ImageIcon addIcon = new ImageIcon(url); back.setIcon(addIcon); panel1.add(back); panel.setOpaque(false); radio1.setBounds(42, 133, 79, 27); group.add(radio1); radio2.setBounds(127, 133, 107, 27); group.add(radio2); panel.add(radio1); panel.add(radio2); radio1.setOpaque(false); radio2.setOpaque(false); radio1.addActionListener(this); radio2.addActionListener(this); button1.addActionListener(this); button2.addActionListener(this); this.addComponentListener(new ComponentAdapter(){ //为主面板添加窗口监听器 @Override public void componentResized(ComponentEvent e) {//窗体变化背景图跟着变 try{ panel1.remove(back); // System.out.println("111"); back = new JLabel("New label"); // back.setBounds(frame.getX(),frame.getY(),frame.getWidth(), frame.getHeight()); URL url = this.getClass().getResource("/11.jpeg"); ImageIcon addIcon = new ImageIcon(url); addIcon.setImage(addIcon.getImage().getScaledInstance(frame.getWidth(), frame.getHeight(),Image.SCALE_DEFAULT )); back.setIcon(addIcon); back.setSize(frame.getWidth(), frame.getHeight()); panel1.add(back); } catch(Exception e1){ System.out.println(e1.getMessage()); } } }); } @Override public void actionPerformed(ActionEvent e) { // TODO 自动生成的方法存根 if(e.getActionCommand()=="注册"){ // Register register1=new Register(); Register.main(null); } else if(e.getActionCommand()=="登陆"){ query.Connection(); query.setTableName("idTable"); String s1=text1.getText(); String s2=text2.getText(); String s3=null; if(radio1.isSelected()) s3="librarian"; else s3="read"; if(s1!=null&&s2!=null&&s3=="read"){ try{ query.setSQL("select * from idTable where id='"+s1+"' AND passwork='"+s2+"'"); ResultSet rs=query.exeQuery(query.getSQL()); if(rs.next()){ Read.setId(s1, s2); Read read1=new Read(); read1.main(null); frame.setVisible(false); query.Release(); } else{ JOptionPane.showMessageDialog(frame, "账号密码错误"); text1.setText(null); text2.setText(null); } } catch(Exception e1){ System.out.println(e1.getMessage()); } } else if(s1!=null&&s2!=null&&s3=="librarian"){ try{ query.setSQL("select * from idTable where id='"+s1+"' AND passwork='"+s2+"'"); ResultSet rs=query.exeQuery(query.getSQL()); if(rs.next()){ Librarian.setId(s1, s2); Librarian librarian1=new Librarian(); librarian1.main(null); frame.setVisible(false); query.Release(); } else{ JOptionPane.showMessageDialog(frame, "账号密码错误"); text1.setText(null); text2.setText(null); } } catch(Exception e1){ System.out.println(e1.getMessage()); } } } } }
更新图书
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import java.awt.event.ActionEvent; import javax.swing.CellEditor; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.table.DefaultTableModel; public class UpdateBook extends Find{ public UpdateBook() { JButton button = new JButton("保存修改"); button.setBounds(382, 232, 113, 27); add(button); button.addActionListener(this); JLabel lblenter = new JLabel("注意:1)一次只能改一个。"); lblenter.setBounds(327, 198, 500, 27); add(lblenter); } public void actionPerformed(ActionEvent e){ super.actionPerformed(e); if(e.getActionCommand()=="保存修改"){ DefaultTableModel model = (DefaultTableModel) table.getModel(); int row=table.getSelectedRow(); int col=table.getSelectedColumn(); // table.setEnabled(false); CellEditor ce =table.getCellEditor(row, col); ce.stopCellEditing(); //取消所选单元格的编辑状态 String s1=model.getValueAt(row, col).toString(); String s2=model.getValueAt(row, 0).toString(); // System.out.println(s1); // System.out.println(s2); Query query=new Query(); query.Connection(); query.setSQL("update booktable set "+biaoTou[col]+"='"+s1+"' where isbn='"+s2+"'"); query.exeUpdate(query.getSQL()); query.Release(); table.repaint(); } } }
改账号
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; public class UpdateId extends JPanel implements ActionListener{ private JTextField text1; private JTextField text2; String id; String password; /** * Create the panel. */ public UpdateId() { setLayout(null); JLabel label = new JLabel("新账号"); label.setBounds(42, 78, 72, 18); add(label); JLabel label_1 = new JLabel("新密码"); label_1.setBounds(42, 140, 72, 18); add(label_1); text1 = new JTextField(); text1.setBounds(111, 75, 112, 24); add(text1); text1.setColumns(10); text2 = new JTextField(); text2.setBounds(111, 137, 112, 24); add(text2); text2.setColumns(10); JButton button = new JButton("确定"); button.setBounds(73, 214, 113, 27); add(button); button.addActionListener(this); } @Override public void actionPerformed(ActionEvent e) { // TODO 自动生成的方法存根 String s1=text1.getText(); String s2=text2.getText(); System.out.println(s1); System.out.println(s2); if(!s1.equals("")&&!s2.equals("")){ Query query=new Query(); query.Connection(); query.setSQL("update idtable set id='"+s1+"' ,passwork='"+s2+"' where id='"+id+"' AND passwork='"+password+"'"); query.exeUpdate(query.getSQL()); query.Release(); } else JOptionPane.showMessageDialog(this, "请正确填写"); // System.out.println(s1); // System.out.println(s2); // System.out.println(id); // System.out.println(password); } void setId(String a,String b){ id=a; password=b; } }