1、public class mainframe extends JFrame implements ActionListenerprivate JToolBar tb;String btntitle=增加,查询删除退出;String tabeltitle=图书编号书名定价出版社JButton btn=new JButton4;JTable table; DefaultTableModel model;public mainframe() super(图书管理系统); /初始化各个按钮 tb=new JToolBar(工具栏 for(int i=0;ibtntitle.length;i+) btn
2、i=new JButton(btntitlei); btni.addActionListener(this); tb.add(btni); /创建表模型 model=new DefaultTableModel(tabeltitle,10); table=new JTable(model); /设定单元格的高度 table.setRowHeight(25); add(tb,BorderLayout.NORTH ); add(new JScrollPane(table),BorderLayout.CENTER ); setSize(900,600); setDefaultCloseOperatio
3、n(JFrame.EXIT_ON_CLOSE ); setLocation(100,30); init();public void actionPerformed(ActionEvent e) mainframe m=new mainframe(); Object o=e.getSource(); if(o=btn0) adddialog ad=new adddialog(m,25,25); init(); if(o=btn1) searchdialog sd=new searchdialog(m,25,25); sd.dialog.setVisible(true); init(); if(o
4、=btn2) int result=JOptionPane.showConfirmDialog(null,确实要删除该条图书信息吗?确认,JOptionPane.YES_NO_OPTION ,JOptionPane.QUESTION_MESSAGE ); if(result=0) Connection con=dbcon.getconnectin(); Object ob=model.getValueAt(table.getSelectedRow(),0); dbcon.delete(con,ob); init(); else return ; if(o=btn3) System.exit(0
5、);public void init() Connection con=dbcon.getconnectin(); LinkedList list=null; try PreparedStatement stmt=con.prepareStatement(select * from book ResultSet rs=stmt.executeQuery(); list=new LinkedList(); while(rs.next() String id=rs.getString(1); String name=rs.getString(2); String price=rs.getStrin
6、g(3); String publish=rs.getString(4); book b=new book(id,name,Float.valueOf(price),publish); list.add(b); model.setRowCount(0); for(int i=0;list.size(); book b=(book)list.get(i); Object str=b.getid(),b.getname(),b.getprice(),b.getpublish(); model.addRow(str); catch (SQLException e) / TODO Auto-gener
7、ated catch block e.printStackTrace(); public static void main(String args)new mainframe().show();*2.Adddialog.java添加模块public class adddialog implements ActionListener JLabel label=new JLabel4;JTextField txt=new JTextField4;String ltitle=图书名称单 价出 版 社String btitle=上一个下一个确 定取 消JDialog dialog;JPanel p;L
8、inkedList list;public adddialog(JFrame f,int x,int y) dialog=new JDialog(f,true); dialog.setLocation(x,y); p=new JPanel(); list=new LinkedList();/ p.setBorder(BorderFactory.createTitledBorder(请选择操作);/ p.setBorder(BorderFactory.createLineBorder(Color.red );/ p.setBorder(BorderFactory.createBevelBorde
9、r(0, Color.red , Color.green ); p.setBorder(BorderFactory.createBevelBorder(0, Color.red , Color.green , Color.blue , Color.yellow );ltitle.length ; labeli=new JLabel(ltitlei); labeli.setFont(new Font(宋体,Font.PLAIN ,18); txti=new JTextField(); btni=new JButton(btitlei); btni.setFont(new Font(幼圆,Font
10、.PLAIN ,15); dialog.add(labeli); dialog.add(txti); dialog.add(btni); dialog.setLayout(null); p.setBounds(10,150,310,160); p.setBackground(Color.green ); label0.setBounds(10,10,90,25);txt0.setBounds(110,10,200,25); label1.setBounds(10,45,90,25);txt1.setBounds(110,45,200,25); label2.setBounds(10,80,90
11、,25);txt2.setBounds(110,80,200,25); label3.setBounds(10,115,90,25);txt3.setBounds(110,115,200,25); dialog.add(p); btn0.setBounds(60,180,80,25);btn1.setBounds(175,180,80,25); btn2.setBounds(60,250,80,25);btn3.setBounds(175,250,80,25); dialog.setSize(400,400); dialog.show(); dialog.setDefaultCloseOper
12、ation(JFrame.DO_NOTHING_ON_CLOSE ); int size=list.size();/ init();/ list.add(b);/ if(e.getSource()=btn0)/ / book b1=(book)list.get(size);/ size-;/ txt0.setText(b1.getid();/ txt1.setText(b1.getname();/ txt2.setText(Float.toString(b1.getprice();/ txt3.setText(b1.getpublish();/ / if(e.getSource()=btn1)
13、/ / book b2=(book)list.get(size);/ size+;/ txt0.setText(b2.getid();/ txt1.setText(b2.getname();/ txt2.setText(Float.toString(b2.getprice();/ txt3.setText(b2.getpublish(); if(e.getSource()=btn2) String id=txt0.getText(); String name=txt1.getText(); float price=getprice(txt2.getText(); String publish=
14、txt3.getText(); Object str=id,name,price,publish; dbcon.insert(dbcon.getconnectin(), str);private float getprice(String str) float price=0; try price=Float.valueOf(str); catch(Exception ex) JOptionPane.showMessageDialog(null,价格必须是数字,请重新输入!错误,JOptionPane.WARNING_MESSAGE ); return price;/设定上一个,下一个按钮的可
15、用性/private void init()/if(list.size()=0)/ btn0.setEnabled(false);/ btn1.setEnabled(false);/else/ btn0.setEnabled(true);/ btn1.setEnabled(true);/3.searchdialog.java 查找模块public class searchdialog implements ActionListener ,ItemListenerJRadioButton radio=new JRadioButton3;ButtonGroup group;String rtitl
16、e= 确定(Yes)取消(Esc)JTextField txtfield=new JTextField3;JButton btn=new JButton2;searchresult st;public searchdialog(JFrame f,int x,int y) /初始化单选按钮 group=new ButtonGroup(); st=new searchresult();radio.length; radioi=new JRadioButton(rtitlei); radioi.addActionListener(this); radioi.setFont(new Font( rad
17、ioi.addItemListener(this); group.add(radioi); txtfieldi=new JTextField(); txtfieldi.setEditable(false);2; dialog =new JDialog(f, /初始化 radio0.setSelected(true); txtfield0.setEditable(true ); /设定各个组件的边界 radio0.setBounds(10,10,90,25);txtfield0.setBounds(110,10,250,25); radio1.setBounds(10,45,90,25);txt
18、field1.setBounds(110,45,250,25); radio2.setBounds(10,80,90,25);txtfield2.setBounds(110,80,250,25);radio.length ; dialog.add(radioi); dialog.add(txtfieldi); btn0.setBounds(20,135,120,25);btn1.setBounds(200,135,120,25); dialog.add(btn0);dialog.add(btn1); dialog.setSize(400,250); dialog.setDefaultClose
19、Operation(JFrame.DISPOSE_ON_CLOSE ); if(e.getSource()=btn0) st.init(); Connection con=dbcon.getconnectin(); PreparedStatement stmt=null; ResultSet rs=null; LinkedList list=new LinkedList(); try if(radio0.isSelected() stmt=con.prepareStatement(select * from book where bid like %+txtfield0.getText().t
20、rim()+% if(radio1.isSelected()select * from book where bname like +txtfield1.getText().trim()+ if(radio2.isSelected()select * from book where bpublish like +txtfield2.getText().trim()+ rs=stmt.executeQuery(); while(rs.next() String id=rs.getString(1); String name=rs.getString(2); String price=rs.get
21、String(3); String publish=rs.getString(4); book b=new book(id,name,Float.valueOf(price),publish); list.add(b); book b=(book)list.get(i); Object ob=b.getid(),b.getname(),b.getprice(),b.getpublish(); st.model.addRow(ob); 总共找到+list.size()+条记录!信息,JOptionPane.INFORMATION_MESSAGE ); st.show(); catch (SQLE
22、xception e1) / TODO Auto-generated catch block e1.printStackTrace(); public void itemStateChanged(ItemEvent e) if(radioi.isSelected() txtfieldi.setEditable(true); else txtfieldi.setEditable(false);4.searchresult.java查找结果public class searchresult extends JFrame implements ActionListener JTable table; String rtitle=单价 JDialog dialog;public searchresult()super(查询结果 model=new DefaultTableModel(rt
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1