SearchBook.java
~~~
package zyw.admin;
import zyw.tools.DataBase;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
public class SearchBook extends JPanel implements ActionListener{
int flag;
String sql;
DataBase db;
//創建分割方向為上下的JSplitePane對象
private JSplitPane jsp=new JSplitPane(JSplitPane.VERTICAL_SPLIT,true);
private JPanel jpt=new JPanel();//創建JPanel對象
private JPanel jpb=new JPanel();
//創建表示下拉列表框數據模型的字符串數組
private String[] str={"書名","出版社","作者","購買時間"};
private JComboBox jcb=new JComboBox(str);//創建下拉列表框
private JButton jb=new JButton("提交"); //創建按鈕
private JLabel[] jlArray=new JLabel[]{
new JLabel(" 書 名"),
new JLabel(" 作 者"),
new JLabel("出版社")
};
private JTextField[] jtxtArray=new JTextField[]{//創建文本框
new JTextField(),new JTextField(),
new JTextField(),new JTextField()
};
private JRadioButton[] jrbArray={//創建單選按鈕
new JRadioButton("簡單查詢",true),
new JRadioButton("高級查詢")
};
private ButtonGroup bg=new ButtonGroup();//創建按鈕組
Vector<String> head = new Vector<String>();
{//定義表頭
head.add("書號");head.add("書名");
head.add("作者");head.add("出版社");
head.add("購進時間");head.add("是否借閱");
head.add("是否預約");
}
Vector<Vector> data=new Vector<Vector>();//定義檢索出的書的基本信息
DefaultTableModel dtm=new DefaultTableModel(data,head); //創建表格模型
JTable jt=new JTable(dtm); //創建Jtable對象
JScrollPane jspn=new JScrollPane(jt);//將JTable封裝到滾動窗格
public SearchBook(){
this.setLayout(new GridLayout(1,1));//設置查詢圖書界面為網格布局
//設置整個RetrunBook界面上下部分均為空布局管理器
jpt.setLayout(null);
jpb.setLayout(null);
//設置單選框的大小、位置,并添加事件監聽器
jpt.add(jcb);
jcb.setBounds(160,20,150,20);
jcb.addActionListener(this);
//添加JButton設置其大小位置并添加事件監聽器
jpt.add(jb);
jb.setBounds(560,20,120,20);
jb.addActionListener(this);
for(int i=0;i<2;i++){//對單選按鈕進行設置
jrbArray[i].setBounds(20,20+i*40,100,20);
jpt.add(jrbArray[i]);
jrbArray[i].addActionListener(this);
bg.add(jrbArray[i]);
}
for(int i=0;i<3;i++){//設置標簽和文本框的坐標,并將其添加進JPanel
jlArray[i].setBounds(120+i*200,60,80,20);
jtxtArray[i].setBounds(200+i*180,60,120,20);
jpt.add(jtxtArray[i]);
jpt.add(jlArray[i]);
}
for(int i=0;i<3;i++){//設置文本框為不可用
jtxtArray[i].setEditable(false);
}
//設置文本框的坐標,并添加進jpt
jtxtArray[3].setBounds(350,20,120,20);
jpt.add(jtxtArray[3]);
jsp.setTopComponent(jpt);//把jpt設置到jsp的上部窗格
jsp.setBottomComponent(jspn);
jsp.setDividerSize(4);
this.add(jsp);
jsp.setDividerLocation(100);//設置jsp中分割條的初始位置
//設置窗體的大小位置及可見性
this.setBounds(3,10,600,400);
this.setVisible(true);
}
//為事件加載的監聽器加上處理事件
public void actionPerformed(ActionEvent e){
if(jrbArray[0].isSelected()){//"簡單查詢"單選按鈕被選中
jtxtArray[3].setEditable(true);
for(int i=0;i<jtxtArray.length-1;i++){//設置文本框為不可編輯
jtxtArray[i].setEditable(false);
}
if(jcb.getSelectedIndex()>=0&&jcb.getSelectedIndex()<4){
jtxtArray[3].requestFocus();
if(e.getSource()==jb){//如果事件源為"提交"按鈕,則執行檢索
String str=jtxtArray[3].getText().trim();
if(str.equals("")){
JOptionPane.showMessageDialog(this,"請輸入必要的信息!!!",
"消息",JOptionPane.INFORMATION_MESSAGE);
return;
}
if(jcb.getSelectedIndex()==0){//根據書名進行查詢
sql="select * from BOOK where BookName='"+str+"'";
jtxtArray[3].setText("");
}
else if(jcb.getSelectedIndex()==1){//根據出版社進行查詢
sql="select * from BOOK where Publishment='"+str+"'";
jtxtArray[3].setText("");
}
else if(jcb.getSelectedIndex()==2){//根據作者進行查詢
sql="select * from BOOK where Author='"+str+"'";
jtxtArray[3].setText("");
}
else{//根據購進時間進行查詢
sql="select * from BOOK where BuyTime='"+str+"'";
jtxtArray[3].setText("");
}
db=new DataBase();;
//從表中檢索成功后,把查到的書的所有信息顯示在界面下部分的表中
Vector<Vector> vtemp = new Vector<Vector>();
dtm.setDataVector(vtemp,head); //更新table
jt.updateUI();
jt.repaint();
}
}
}
if(jrbArray[1].isSelected()){//"高級查詢"單選按鈕被選中
jtxtArray[0].requestFocus(); //獲得輸入焦點
jtxtArray[3].setEditable(false);
for(int i=0;i<jtxtArray.length-1;i++){//將高級查詢所涉及的文本框設為可編輯
jtxtArray[i].setEditable(true);
}
if(e.getSource()==jb){//點擊"提交"按鈕
int bz=this.seniorSearch();
if(bz!=0){return;}
db=new DataBase();
//從表中檢索成功后,把查到的書的所有信息顯示在界面下部分的表中
Vector<Vector> vtemp = new Vector<Vector>();
dtm.setDataVector(vtemp,head);//更新table
jt.updateUI();
jt.repaint();
}
}
}
public int seniorSearch(){
int flag=0;//設置標志位
String str0=jtxtArray[0].getText().trim();
String str1=jtxtArray[1].getText().trim();
String str2=jtxtArray[2].getText().trim();
if(str0.equals("")&&str1.equals("")&&str2.equals("")){//文本框輸入為空
JOptionPane.showMessageDialog(this,"請輸入必要的信息!!!",
"消息",JOptionPane.INFORMATION_MESSAGE);
flag++;
}
if(((!str0.equals(""))&&(str1.equals(""))&&(str2.equals("")))
||((str0.equals(""))&&(!str1.equals(""))&&(str2.equals("")))
||((str0.equals(""))&&(str1.equals(""))&&(!str2.equals("")))){
JOptionPane.showMessageDialog(this,"請使用簡單查詢!!!",
"消息",JOptionPane.INFORMATION_MESSAGE);
flag++;
}
if((!str0.equals(""))&&(!str1.equals(""))&&(str2.equals(""))){//書名和作者組合
sql="select * from BOOK where BookName='"+str0+"' and Author='"+str1+"'";
jtxtArray[0].setText("");jtxtArray[1].setText("");
}
if((!str0.equals(""))&&(str1.equals(""))&&(!str2.equals(""))){//書名和出版社組合
sql="select * from Book where BookName='"+str0+"' and Publishment='"+str2+"'";
jtxtArray[0].setText("");jtxtArray[2].setText("");
}
if((str0.equals(""))&&(!str1.equals(""))&&(!str2.equals(""))){//作者與出版社組合
sql="select * from Book where Author='"+str1+"' and Publishment='"+str2+"'";
jtxtArray[1].setText("");jtxtArray[2].setText("");
}
if((!str0.equals(""))&&(!str1.equals(""))&&(!str2.equals(""))){//三者組合
sql="select * from Book where BookName='"+str0
+"' and Publishment='"+str2+"' and Author='"+str1+"'";
jtxtArray[0].setText("");jtxtArray[1].setText("");jtxtArray[2].setText("");
}
return flag;
}
}
~~~