📄 dbsfpanel.java
字号:
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.table.*;
import java.util.*;
import edu.njust.cs.*;
public class DBSFPanel extends CommonPanel{
public DBSFPanel(MainApp f,Connection con){
super(f,con);
f.labStatusContent.setText(" 您正在进行电表收费");
columnNames=new String[] {"电表编号","缴费年月",
"缴费时间","缴费金额","收费人员"};
dataType=new Class[] {String.class,String.class,
Timestamp.class,Double.class,String.class};
createTable();
//将新增按钮定义为缴费功能
this.btnAdd.setText(" 缴费 ");
//按需要设置每列宽度
this.setTableColumnWidth(0,100);
this.setTableColumnWidth(1,100);
this.setTableColumnWidth(2,200);
this.setTableColumnWidth(3,200);
this.setTableColumnWidth(4,200);
//使用DoubleRender渲染器,使得数值保持两位小数
TableColumn col=table.getColumn(columnNames[3]);
col.setCellRenderer(new DoubleRender());
}
public Object []constructLineForDBAdd(DBSFEditor d){
Object []lineForDBAdd=new Object[model.getColumnCount()];
lineForDBAdd[0]=d.getDBID();
lineForDBAdd[1]=d.getYearMonth();
lineForDBAdd[2]=d.getJFSJ();
lineForDBAdd[3]=d.getJFJE();
lineForDBAdd[4]=d.getSFRY();
return lineForDBAdd;
}
public Object []constructLineForDBUpdate(DBSFEditor d){
Object []lineForDBUpdate=new Object[model.getColumnCount()];
lineForDBUpdate[0]=d.getJFSJ();
lineForDBUpdate[1]=d.getJFJE();
lineForDBUpdate[2]=d.getSFRY();
lineForDBUpdate[3]=d.getDBID();
lineForDBUpdate[4]=d.getYearMonth();
return lineForDBUpdate;
}
public void add(){//缴费
//创建查询条件面板
final JTextField txtName=new JTextField();
txtName.addActionListener(tfl);
JTextField txtYearMonth=new JTextField();
txtYearMonth.addActionListener(tfl);
JPanel sp=new JPanel();
sp.setLayout(new GridBagLayout());
LayoutUtil.add(sp,GridBagConstraints.HORIZONTAL,
GridBagConstraints.CENTER,100,0,0,0,1,1,new JLabel("电表编号"));
LayoutUtil.add(sp,GridBagConstraints.HORIZONTAL,
GridBagConstraints.CENTER,100,0,0,1,1,1,txtName);
LayoutUtil.add(sp,GridBagConstraints.HORIZONTAL,
GridBagConstraints.CENTER,100,0,0,2,1,1,new JLabel("缴费年月"));
LayoutUtil.add(sp,GridBagConstraints.HORIZONTAL,
GridBagConstraints.CENTER,100,0,0,3,1,1,txtYearMonth);
//txtName获得输入焦点
SwingUtilities.invokeLater(new Runnable(){
public void run(){
txtName.requestFocus();
}
});
//创建查询对话框
String[] options = { "确定", "取消"};
int result = JOptionPane.showOptionDialog(
father, // 父组件
sp, // 查询条件面板
"查询条件", // 对话框标题
JOptionPane.DEFAULT_OPTION, // 选项类型
JOptionPane.QUESTION_MESSAGE, // 消息类型
null, //图标
options, // 按钮
options[0] // 缺省按钮
);
if(result==JOptionPane.OK_OPTION){ //用户按下确定按钮
String querySql="select * from DBCZT where DBID='"+
txtName.getText()+"' and "+"CZYearMonth ='"+
txtYearMonth.getText()+"' and isChecked='"+"Y"+"'";
String []cn=new String[] {"电表编号","出帐年月","应收电费"};
Class []dt=new Class[] {String.class,String.class,Double.class};
CustomTableModel tempModel=new CustomTableModel(0,cn.length,cn,dt);
SqlUtil.readDBToTable(con,querySql,tempModel,dt);
if(tempModel.getRowCount()<=0)
JOptionPane.showMessageDialog(this,
"没有相应的出帐记录或是未审核通过!",
"提示",JOptionPane.INFORMATION_MESSAGE);
else{
DBSFEditor d=new DBSFEditor(father,"新增",tempModel,true);
d.show();
if(d.getActionCode()==EditorAction.OK)
{
querySql="select * from JFXXT where JFYearMonth=? and DBID=?";
Object []keys={d.getYearMonth(),d.getDBID()};
if(SqlUtil.isRecordExist(con,querySql,keys))
{
JOptionPane.showMessageDialog(this,"电表"+
d.getDBID()+" "+d.getYearMonth()+
"的缴费记录已经存在","提示",
JOptionPane.INFORMATION_MESSAGE);
return;
}
String insertSql="insert into JFXXT VALUES (?,?,?,?,?)";
Object []lineForDBAdd=constructLineForDBAdd(d);
if(SqlUtil.addRowToDB(con,insertSql,lineForDBAdd)){
Object []lineForTable=SqlUtil.getLineForTableFromLineForDB(lineForDBAdd,dataType);
model.addRow(lineForTable);
table.changeSelection(model.getRowCount()-1,0,false,false);
JOptionPane.showMessageDialog(this,"缴费成功!",
"提示",
JOptionPane.INFORMATION_MESSAGE);
}
else
JOptionPane.showMessageDialog(this,"新增时出错!",
"提示",
JOptionPane.INFORMATION_MESSAGE);
}
//
}
}
}
public void modify(){
int selected=table.getSelectedRow();
if(selected>=0&&selected<model.getRowCount()){
DBSFEditor d=new DBSFEditor(father,"修改",null,true);
d.show();
if(d.getActionCode()==EditorAction.OK) {
String updateSql="update JFXXT set JFSJ=?,JFJE=?,GYID=?"+
" WHERE DBID=? and JFYearMonth=?";
Object [] lineForDBUpdate=constructLineForDBUpdate(d);
if(SqlUtil.updateRowInDB(con,updateSql,lineForDBUpdate)){
selected=table.getSelectedRow();
model.removeRow(selected);
Object []lineForTable=SqlUtil.getLineForTableFromLineForDB(
constructLineForDBAdd(d),dataType);
model.insertRow(selected,lineForTable);
table.changeSelection(selected,0,false,false);
}
else
JOptionPane.showMessageDialog(this,"修改属性时出错!",
"提示",
JOptionPane.INFORMATION_MESSAGE);
}
}
else
JOptionPane.showMessageDialog(this,"请先选定需审核的记录!",
"提示",
JOptionPane.INFORMATION_MESSAGE);
}
//查询
public void search(){
//创建查询条件面板
final JTextField txtName=new JTextField();
txtName.addActionListener(tfl);
JTextField txtYearMonth=new JTextField();
txtYearMonth.addActionListener(tfl);
JPanel sp=new JPanel();
sp.setLayout(new GridBagLayout());
LayoutUtil.add(sp,GridBagConstraints.HORIZONTAL,
GridBagConstraints.CENTER,100,0,0,0,1,1,new JLabel("电表名称"));
LayoutUtil.add(sp,GridBagConstraints.HORIZONTAL,
GridBagConstraints.CENTER,100,0,0,1,1,1,txtName);
LayoutUtil.add(sp,GridBagConstraints.HORIZONTAL,
GridBagConstraints.CENTER,100,0,0,2,1,1,new JLabel("缴费年月"));
LayoutUtil.add(sp,GridBagConstraints.HORIZONTAL,
GridBagConstraints.CENTER,100,0,0,3,1,1,txtYearMonth);
//txtName获得输入焦点
SwingUtilities.invokeLater(new Runnable(){
public void run(){
txtName.requestFocus();
}
});
//创建查询对话框
String[] options = { "确定", "取消"};
int result = JOptionPane.showOptionDialog(
father, // 父组件
sp, // 查询条件面板
"查询条件", // 对话框标题
JOptionPane.DEFAULT_OPTION, // 选项类型
JOptionPane.QUESTION_MESSAGE, // 消息类型
null, //图标
options, // 按钮
options[0] // 缺省按钮
);
if(result==JOptionPane.OK_OPTION){ //用户按下确定按钮
String querySql="select * from JFXXT left outer join DBT ON"+
" DBT.DBid=JFXXT.DBID where DBT.DBName"+
" like '%"+txtName.getText()+"%' and "+
"JFXXT.JFYearMonth like '%"+txtYearMonth.getText()+"%'";
SqlUtil.readDBToTable(con,querySql,model,dataType);
}
}
//删除指定记录
public void delete(){
int selected=table.getSelectedRow();
if(selected>=0&&selected<model.getRowCount()){
int selection=JOptionPane.showConfirmDialog (this,
"您确定删除预该条记录?",
"确认",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE);
if (selection==JOptionPane.YES_OPTION){
String strIDToDelete=model.getValueAt(selected,0).toString().trim();
String strYearMonthToDelete=model.getValueAt(selected,1).toString().trim();
String deleteSql="delete from JFXXT where DBID=? and JFYearMonth=?";
Object []keys={strIDToDelete,strYearMonthToDelete};
if(SqlUtil.deleteFromDB(con,deleteSql,keys))
model.removeRow(selected);
else
JOptionPane.showMessageDialog(this,"删除时出错!",
"提示",
JOptionPane.INFORMATION_MESSAGE);
}
}
else
JOptionPane.showMessageDialog(this,"请选定一行,然后再删!",
"提示",
JOptionPane.INFORMATION_MESSAGE);
}
//内部类,用于显示/编辑雇员信息的一个模式对话框
class DBSFEditor extends CommonEditor {
private JLabel labDBID=new JLabel(" 电表编号 ");
private JTextField txtDBID=new JTextField();
private JLabel labYearMonth=new JLabel(" 缴费年月 ");
private JTextField txtYearMonth=new JTextField();
private JLabel labJFSJ=new JLabel(" 缴费时间 ");
private JTextField txtJFSJ=new JTextField();
private JLabel labJFJE=new JLabel(" 缴费金额 ");
private JTextField txtJFJE=new JTextField();
private JLabel labSFRY=new JLabel(" 收费人员 ");
private JTextField txtSFRY=new JTextField();
public DBSFEditor(MainApp f,String s,CustomTableModel input,boolean b){
super(f,s,b,DBSFPanel.this.table);
//面板p中显示缴费信息
JPanel p=new JPanel();
p.setBorder(BorderFactory.createLoweredBevelBorder());
p.setLayout(new GridBagLayout());
LayoutUtil.add(p,GridBagConstraints.NONE,
GridBagConstraints.CENTER,0,0,0,0,1,1,labDBID,insets);
LayoutUtil.add(p,GridBagConstraints.HORIZONTAL,
GridBagConstraints.CENTER,100,0,1,0,1,1,txtDBID,insets);
LayoutUtil.add(p,GridBagConstraints.NONE,
GridBagConstraints.CENTER,0,0,2,0,1,1,labYearMonth,insets);
LayoutUtil.add(p,GridBagConstraints.HORIZONTAL,
GridBagConstraints.CENTER,100,0,3,0,1,1,txtYearMonth,insets);
LayoutUtil.add(p,GridBagConstraints.NONE,
GridBagConstraints.CENTER,0,0,0,1,1,1,labJFSJ,insets);
LayoutUtil.add(p,GridBagConstraints.HORIZONTAL,
GridBagConstraints.CENTER,100,0,1,1,1,1,txtJFSJ,insets);
LayoutUtil.add(p,GridBagConstraints.NONE,
GridBagConstraints.CENTER,0,0,2,1,1,1,labJFJE,insets);
LayoutUtil.add(p,GridBagConstraints.HORIZONTAL,
GridBagConstraints.CENTER,100,0,3,1,1,1,txtJFJE,insets);
LayoutUtil.add(p,GridBagConstraints.NONE,
GridBagConstraints.CENTER,0,0,0,2,1,1,labSFRY,insets);
LayoutUtil.add(p,GridBagConstraints.HORIZONTAL,
GridBagConstraints.CENTER,100,0,1,2,1,1,txtSFRY,insets);
txtSFRY.addActionListener(tfl);
getContentPane().add(p,BorderLayout.CENTER);
//将对话框窗口定位在父窗口的居中位置
setSizeAndPosition(this,450,150);
//依据增加或是修改,设置pre next按钮是否可用
setPreNextButton(s);
this.txtDBID.setEditable(false);
this.txtYearMonth.setEditable(false);
this.txtJFSJ.setEditable(false);
this.txtJFJE.setEditable(false);
if(s.equals("新增")){
this.txtDBID.setText(input.getValueAt(0,0).toString());
this.txtYearMonth.setText(input.getValueAt(0,1).toString());
this.txtJFSJ.setText(DateUtil.getYMDHMSFromGC(
new GregorianCalendar()));
this.txtJFJE.setText(input.getValueAt(0,2).toString());
}
}
//将表格中选中的记录读入编辑框
public void initEditor(){
int selected=table.getSelectedRow();
this.txtDBID.setText((String)model.getValueAt(selected,0));
this.txtYearMonth.setText((String)model.getValueAt(selected,1));
if(model.getValueAt(selected,2)==null)
this.txtJFSJ.setText("");
else
this.txtJFSJ.setText(DateUtil.getYMDHMSFromGC(
DateUtil.getGCFromTS((Timestamp)model.getValueAt(selected,2))));
if(model.getValueAt(selected,3)==null)
this.txtJFJE.setText("");
else
this.txtJFJE.setText(df.format(model.getValueAt(selected,3)));
this.txtSFRY.setText((String)model.getValueAt(selected,4));
}
public String getDBID(){
return txtDBID.getText().trim();
}
public String getYearMonth(){
return txtYearMonth.getText().trim();
}
//将电量、电费由字符串类型转化为Double类型
//如果不是合法的数值字符串,取值Double.NEGATIVE_INFINITY
public Double getJFJE(){
double result=Double.NEGATIVE_INFINITY;
try{
result=Double.parseDouble(txtJFJE.getText().trim());
}catch(NumberFormatException e){
result=Double.NEGATIVE_INFINITY;
}
return new Double(result);
}
public Timestamp getJFSJ(){
return DateUtil.getTSFromYMDHMS(this.txtJFSJ.getText());
}
public String getSFRY(){
return this.txtSFRY.getText();
}
public int getActionCode(){
return this.actionCode;
}
public void okClicked(){
//隐藏对话框
this.actionCode=EditorAction.OK;
this.setVisible(false);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -