BorrowBook.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 BorrowBook extends JPanel implements ActionListener{
//創建分割方向為上下的JSplitePane對象
private JSplitPane jsp1=new JSplitPane(JSplitPane.VERTICAL_SPLIT,true);
private JPanel jp2=new JPanel();
//創建按鈕數組
int flag;
String sql;
DataBase db;
private JButton jb2=new JButton("確定");
private JLabel jl3=new JLabel("您要借閱或預約的書號");
private JLabel jl4=new JLabel("請輸入您的學號");
//在jsp1添加文本框
private JTextField jtxt3=new JTextField();
private JTextField jtxt4=new JTextField();
//在jp2設置單選框
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("是否預約");
}
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 BorrowBook()
{
this.setLayout(new GridLayout(1,1));
//把jsp2設置到jsp1的上部窗格
jsp1.setTopComponent(jp2);
//設置jsp1的下部窗格
jsp1.setBottomComponent(jspn);
//設置jsp1,jsp2中分割條的初始位置
jsp1.setDividerLocation(100);//設置分割控件位置
jsp1.setDividerSize(4);//設置分割控件寬度
jp2.setLayout(null);
jb2.setBounds(380,20,100,20);//設置按鈕的大小與位置
//將按鈕添加進JPanel
jp2.add(jb2);
jb2.addActionListener(this);
//設置JLabel的坐標
jl3.setBounds(80,60,130,20);
jl4.setBounds(330,60,100,20);
//把JLabel添加進JPanel
jp2.add(jl3);
jp2.add(jl4);
jtxt3.setBounds(220,60,100,20);
jtxt4.setBounds(430,60,100,20);
jp2.add(jtxt3);
jp2.add(jtxt4);
for(int i=0;i<2;i++)
{
jrbArray[i].setBounds(70+i*150,20,150,20);
jp2.add(jrbArray[i]);
bg.add(jrbArray[i]);
}
this.add(jsp1);
//設置窗體的標題,大小位置及可見性
this.setBounds(10,10,800,600);
this.setVisible(true);
}
//為事件加載的監聽器加上處理事件
public void actionPerformed(ActionEvent e){
if(e.getSource()==jb2){
if(jtxt4.getText().equals("")){//為輸入為空的情況進行處理
JOptionPane.showMessageDialog(this,"輸入不能為空,請重新輸入!!!",
"信息",JOptionPane.INFORMATION_MESSAGE);
return;
}
//查詢學號文本中所輸學號是否存在于STUDENT表中
sql="select * from STUDENT where StuNO="+Integer.parseInt(jtxt4.getText().trim());
db=new DataBase();
Vector<Vector> vtemp = new Vector<Vector>();
if(true){
JOptionPane.showMessageDialog(this,"輸入了錯誤的學號","消息",
JOptionPane.INFORMATION_MESSAGE);
}
else{
JOptionPane.showMessageDialog(this,"學號正確","消息",
JOptionPane.INFORMATION_MESSAGE);
}
}}}
~~~