ExceedTime.java
~~~
package zyw.admin;
import zyw.tools.DataBase;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ExceedTime extends JPanel implements ActionListener
{
private JLabel jl=new JLabel("請輸入您的學號");
private JTextField jtf=new JTextField();//創建文本框
private JLabel jl1=new JLabel("請輸入您要交的款數");
private JTextField jtf1=new JTextField();
//創建按鈕
private JButton jb=new JButton("交費");
private JButton jb1=new JButton("查詢欠費");
public ExceedTime()
{
this.setLayout(null);//設置布局為空布局
this.add(jl);//將JLabel添加進JPanel
this.add(jtf);//將JTextField添加進JPanel
this.add(jl1);
this.add(jtf1);//將JTextField添加進JPanel
//將JButton添加進JPanel
this.add(jb);
this.add(jb1);
//分別設置JLabel,JTextField,JButton的大小位置
jl.setBounds(50,30,120,20);
jtf.setBounds(170,30,120,20);
jl1.setBounds(50,70,120,20);
jtf1.setBounds(170,70,120,20);
jb.setBounds(180,110,100,25);
//為按鈕添加事件監聽器
jb.addActionListener(this);
jb1.addActionListener(this);
jb1.setBounds(50,110,100,25);
//設置窗體的大小位置
this.setBounds(300,300,600,650);
this.setVisible(true);
}
//為事件加載的監聽器加上處理事件
public void actionPerformed(ActionEvent e)
{
int day=0;//初始化天數變量
DataBase db=new DataBase();
String sno=(String)jtf.getText().trim();//定義文本框內容變量
if(sno.equals("")){//如果輸入學號為空進行提示
JOptionPane.showMessageDialog(this,"學號不能為空!!!",
"信息",JOptionPane.INFORMATION_MESSAGE);
return;
}
if(sno.matches("\\D")){//定義學號格式為數字組合
JOptionPane.showMessageDialog(this,"學號只能為數字!!!",
"信息",JOptionPane.INFORMATION_MESSAGE);
return;
}
String sql="select DelayTime from EXCEEDTIME where StuNO="
+Integer.parseInt(sno);
try{//對結果集進行異常處理
int flag=0;
if(flag==0){//結果集為空給出提示
JOptionPane.showMessageDialog(this,"您所借的書沒有超期,不需要還款!!",
"信息",JOptionPane.INFORMATION_MESSAGE);
return;
}
}
catch(Exception ex){ex.printStackTrace();}
if(e.getSource()==jb1){//事件源為"查詢欠款"按鈕
if(day>0){//提示欠款數
JOptionPane.showMessageDialog(this,"您欠費"+day*0.1+"元!",
"信息",JOptionPane.INFORMATION_MESSAGE);
return;
}
else{//如果沒有欠款,提示
JOptionPane.showMessageDialog(this,"您所借的書沒有超期,不需要還款!!",
"信息",JOptionPane.INFORMATION_MESSAGE);
return;
}
}
else if(e.getSource()==jb){//事件源為"交費"按鈕
if(jtf1.getText().trim().equals("")){//繳費金額為空的提示
JOptionPane.showMessageDialog(this,"請輸入繳款金額!!",
"消息",JOptionPane.INFORMATION_MESSAGE);
return;
}
int k=JOptionPane.showConfirmDialog(this,"是否繳費?",
"消息",JOptionPane.YES_NO_OPTION);
if(k==JOptionPane.YES_OPTION){//選擇對話框,當選擇"是"時,將提示操作步驟
int ii=Integer.parseInt(jtf1.getText().trim());
if(ii<(day*0.1)){
sql="update exceedtime set delaytime=delaytime-"+ii/0.1+" where stuNO="+Integer.parseInt(sno);
db=new DataBase();
int i=5;
if(i==1){//輸出還應該繳費數的提示
JOptionPane.showMessageDialog(this,"你已成功交費"+ii+"元,您還需繳納"+(day*0.1-ii)+"元",
"消息",JOptionPane.INFORMATION_MESSAGE);
return;
}
else{//選擇"否",提示繳費失敗
JOptionPane.showMessageDialog(this,"對不起,繳費失敗!!!",
"消息",JOptionPane.INFORMATION_MESSAGE);
return;
}
}
else{//提示繳費成功
JOptionPane.showMessageDialog(this,"你已成功交費"+day*0.1+"元",
"消息",JOptionPane.INFORMATION_MESSAGE);
jtf.setText("");
sql="delete from EXCEEDTIME where StuNO="+Integer.parseInt(sno);
sql="update STUDENT set Permitted='是' where StuNO="+Integer.parseInt(sno);
}
}
}}}
~~~