label[i]=newJLabel(ltitle[i]);
label[i].setFont(newFont("宋体",Font.PLAIN,18));
txt[i]=newJTextField();
btn[i]=newJButton(btitle[i]);
btn[i].addActionListener(this);
btn[i].setFont(newFont("幼圆",Font.PLAIN,15));
dialog.add(label[i]);
dialog.add(txt[i]);
dialog.add(btn[i]);
}
dialog.setLayout(null);
p.setBounds(10,150,310,160);
p.setBackground(Color.green);
label[0].setBounds(10,10,90,25);txt[0].setBounds(110,10,200,25);
label[1].setBounds(10,45,90,25);txt[1].setBounds(110,45,200,25);
label[2].setBounds(10,80,90,25);txt[2].setBounds(110,80,200,25);
label[3].setBounds(10,115,90,25);txt[3].setBounds(110,115,200,25);
dialog.add(p);
btn[0].setBounds(60,180,80,25);btn[1].setBounds(175,180,80,25);
btn[2].setBounds(60,250,80,25);btn[3].setBounds(175,250,80,25);
dialog.setSize(400,400);
dialog.show();
dialog.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
}
publicvoidactionPerformed(ActionEvente){
intsize=list.size();
//init();
//list.add(b);
//if(e.getSource()==btn[0]){
//
//bookb1=(book)list.get(size);
//size--;
//txt[0].setText(b1.getid());
//txt[1].setText(b1.getname());
//txt[2].setText(Float.toString(b1.getprice()));
//txt[3].setText(b1.getpublish());
//}
//if(e.getSource()==btn[1]){
//
//bookb2=(book)list.get(size);
//size++;
//txt[0].setText(b2.getid());
//txt[1].setText(b2.getname());
//txt[2].setText(Float.toString(b2.getprice()));
//txt[3].setText(b2.getpublish());
//}
if(e.getSource()==btn[2]){
Stringid=txt[0].getText();
Stringname=txt[1].getText();
floatprice=getprice(txt[2].getText());
Stringpublish=txt[3].getText();
Objectstr[]={id,name,price,publish};
dbcon.insert(dbcon.getconnectin(),str);
}
}
privatefloatgetprice(Stringstr){
floatprice=0;
try{
price=Float.valueOf(str);
}
catch(Exceptionex){
JOptionPane.showMessageDialog(null,"价格必须是数字,请重新输入!
","错误",JOptionPane.WARNING_MESSAGE);
}
returnprice;
}
//设定上一个,下一个按钮的可用性
//privatevoidinit(){
//if(list.size()==0)
//{
//btn[0].setEnabled(false);
//btn[1].setEnabled(false);
//}
//else{
//btn[0].setEnabled(true);
//btn[1].setEnabled(true);
//}
//}
}
3.searchdialog.java查找模块
packagebookmanager;
importjavax.swing.*;
importjava.awt.*;
importjava.awt.event.*;
importjava.sql.Connection;
importjava.sql.PreparedStatement;
importjava.sql.ResultSet;
importjava.sql.SQLException;
importjava.util.LinkedList;
publicclasssearchdialogimplementsActionListener,ItemListener{
JRadioButton[]radio=newJRadioButton[3];
ButtonGroupgroup;
String[]rtitle={"图书编号","图书名称","出版社"};
String[]btitle={"确定(Yes)","取消(Esc)"};
JTextFieldtxtfield[]=newJTextField[3];
JButtonbtn[]=newJButton[2];
JDialogdialog;
searchresultst;
publicsearchdialog(JFramef,intx,inty){
//初始化单选按钮
group=newButtonGroup();
st=newsearchresult();
for(inti=0;iradio[i]=newJRadioButton(rtitle[i]);
radio[i].addActionListener(this);
radio[i].setFont(newFont("宋体",Font.PLAIN,15));
radio[i].addItemListener(this);
group.add(radio[i]);
txtfield[i]=newJTextField();
txtfield[i].setEditable(false);
}
for(inti=0;i<2;i++){
btn[i]=newJButton(btitle[i]);
btn[i].addActionListener(this);
btn[i].setFont(newFont("宋体",Font.PLAIN,15));
}
dialog=newJDialog(f,"查询",true);
//初始化
radio[0].setSelected(true);
txtfield[0].setEditable(true);
dialog.setLocation(x,y);
dialog.setLayout(null);
//设定各个组件的边界
radio[0].setBounds(10,10,90,25);txtfield[0].setBounds(110,10,250,25);
radio[1].setBounds(10,45,90,25);txtfield[1].setBounds(110,45,250,25);
radio[2].setBounds(10,80,90,25);txtfield[2].setBounds(110,80,250,25);
for(inti=0;idialog.add(radio[i]);
dialog.add(txtfield[i]);
}
btn[0].setBounds(20,135,120,25);btn[1].setBounds(200,135,120,25);
dialog.add(btn[0]);dialog.add(btn[1]);
dialog.setSize(400,250);
dialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
publicvoidactionPerformed(ActionEvente){
if(e.getSource()==btn[0]){
st.init();
Connectioncon=dbcon.getconnectin();
PreparedStatementstmt=null;
ResultSetrs=null;
LinkedListlist=newLinkedList();
try{
if(radio[0].isSelected()){
stmt=con.prepareStatement("select*frombookwherebidlike'%"+txtfield[0].getText().trim()+"%'");
}
if(radio[1].isSelected()){
stmt=con.prepareStatement("select*frombookwherebnamelike'%"+txtfield[1].getText().trim()+"%'");
}
if(radio[2].isSelected()){
stmt=con.prepareStatement("select*frombookwherebpublishlike'%"+txtfield[2].getText().trim()+"%'");
}
rs=stmt.executeQuery();
while(rs.next()){
Stringid=rs.getString
(1);
Stringname=rs.getString
(2);
Stringprice=rs.getString(3);
Stringpublish=rs.getString(4);
bookb=newbook(id,name,Float.valueOf(price),publish);
list.add(b);
}
for(inti=0;ibookb=(book)list.get(i);
Objectob[]={b.getid(),b.getname(),b.getprice(),b.getpublish()};
st.model.addRow(ob);
}
JOptionPane.showMessageDialog(null,"总共找到"+list.size()+"条记录!
","信息",JOptionPane.INFORMATION_MESSAGE);
st.show();
}catch(SQLExceptione1){
//TODOAuto-generatedcatchblock
e1.printStackTrace();
}
}
}
publicvoiditemStateChanged(ItemEvente){
for(inti=0;iif(radio[i].isSelected()){
txtfield[i].setEditable(true);
}
elsetxtfield[i].setEditable(false);
}
}
}
4.searchresult.java查找结果
packagebookmanager;
importjavax.swing.*;
importjavax.swing.table.DefaultTableModel;
importjava.awt.*;
importjava.awt.event.*;
publicclasssearchresultextendsJFrameimplementsActionListener{
JTabletable;
DefaultTableModelmodel;
String[]rtitle={"图书编号","图书名称","单价","出版社"};
JDialogdialog;
publicsearchresult(){
super("查询结果");
model=newDefaultTableModel(rtitl