📄 manadiglog.java
字号:
* 功能:为各个文本框和按钮添加事件 ***********************************************/ public void addListener(){ tf1.addFocusListener(this);//房间编号 tf2.addFocusListener(this); //房间类型 tf3.addFocusListener(this); //房间床位 tf4.addFocusListener(this); //房间价格 tf5.addFocusListener(this); //房间押金 bt1.addActionListener(this); //添加 bt2.addActionListener(this);//保存 bt3.addActionListener(this);//删除 bt4.addActionListener(this);//取消 } //提示类public class WarnMsgPane { //声明对话框 // JOptionPane numOp,StrOp; String str; Boolean flag=false; /* **************************************************** * 函数名:public void numCheck(String num ) * 函数参数:String num 输入框传来的字符 * 返回值:void * 工能:用正则表达式检查输入数据的合法性 * ********************************************************/ @SuppressWarnings("static-access") public Boolean numCheck(String str){ int i = 0; // Pattern pattern=Pattern.compile("\\d"); if(str.equalsIgnoreCase("")){ // tf4.requestFocusInWindow(); return flag=false; }else{ //实例化正则氏 Pattern p = Pattern.compile("[0-9]+|[0-9]+.[0-9]+"); Matcher m = p.matcher(str); boolean b = m.matches(); if(!b) { tf4.requestFocusInWindow(); return flag=false; }//end if(!b) }//end if(str.equals("")) return flag=true; } /***************************************************** * 函数名:public Boolean emptyCheck(String str){} * 参数:str要验证的内容 * 返回值:Boolean * 功能:验证录入的不是数字 * *************************************************/ // public Boolean emptyCheck(String str){ if(str.equals("")){ return flag=false; }else{ return flag=true; }//end if(str.equals("")) }}//初始化表格/* * 函数名:public JScrollPane initTabel() * 参数:null * 返回值:void * 功能:制作并初始化表格 ***********************************/public JScrollPane initTabel(){ //实例化表格并让表格不可编辑 TableModel datamodel=new xtwh_table(rows,colname); tb=new JTable(); //设置表模型 tb.setModel(datamodel); tb.setPreferredSize(new Dimension(100,450)); // 实例化滚动条 sp=new JScrollPane(tb); //添加表格到centerPnae //################################################################ return sp;}/* public static void main(String args[]){ new ManaDiglog(); } *///实现按钮侦听的方法 public void actionPerformed(ActionEvent e) { Object o=e.getSource(); String id=tf1.getText(); //获得房间编号 String type=tf2.getText(); //类型 String bed =tf3.getText(); //床位 String price=tf4.getText(); //价格 String yajin=tf5.getText(); //押金 String remark=ta.getText(); //备注 if(o==bt4){ //添加按钮 insertData(id,type,bed,price,yajin,remark); //产生的SLQ语句 sqls=new String[5]; for(int i = 0;i<=count;i++){ sqls[i]="insert into roomtype(id,r_type,bed,price,foregift,remark) " + "values('"+id+"','"+type+"',"+bed+","+price+","+yajin+",'"+remark+"'" +")"; } count++; }//end if(o==bt4) try{ if(o==bt1){ //保存按钮 if(count==0){ JOptionPane.showMessageDialog(null, "保存新的房间类型失败," +"请先添加数据", "错误", JOptionPane.ERROR_MESSAGE); }else{ int flag=0; //标记SQl语句是否执行成功 flag=SetSQL.runTransaction(sqls,count); if(flag<count) { JOptionPane.showMessageDialog(null, "保存新的房间类型信息失败," + "请检查网络连接或联系管理员", "错误", JOptionPane.ERROR_MESSAGE); }else{ jl9.setText("添加房间类型成功"); tf1.setText(""); //添加完毕将所有组件清空 tf2.setText(""); tf3.setText(""); tf4.setText(""); tf5.setText(""); ta.setText(""); bt4.setEnabled(true); // tf1.requestFocusInWindow(); }//end if(flag) }//end if(count) }//end if(o==bt1) if(o==bt2){ //删除房间 // ImageIcon image = new ImageIcon(ManaDiglog.class.getResource("pic/u03.gif"));//创建一个图片对象. //存储对话框的返回值num String num=JOptionPane.showInputDialog(null, "请输入房间类型想删除的房间类型编号:", "删除房间",JOptionPane.INFORMATION_MESSAGE); // System.out.print(num); if(num!=null){ String delSql="delete from roomtype where id='"+num+"'"; int count=0; //用于标记SQL语句是否执行成功 count= SetSQL.executeUpdate(delSql); if(count==0){ jl9.setText("删除房间类型"+num+"失败!请先删除相应的房间"); }else{ jl9.setText("删除房间类型"+num+"成功"); }//end if(count==0) } //end if(num!=null) }//end if(o==bt2) if(o==bt3){ //取消添加 int flag=JOptionPane.showConfirmDialog(null,"你确定要退出操作吗?","提示",JOptionPane.YES_NO_OPTION); if(flag==JOptionPane.YES_OPTION){ this.dispose(); } }//end if(o==bt3) }catch(Exception ex){ ex.printStackTrace(); JOptionPane.showMessageDialog(null, "保存新的房间类型信息失败," + "请检查网络连接或联系管理员", "错误", JOptionPane.ERROR_MESSAGE); } } //实现焦点侦听的方法 public void focusGained(FocusEvent e) { Object o=e.getSource(); if(o==tf1){ if(tf1.getText().equals("")){ jl9.setText("带*号的不可以为空"); bt4.setEnabled(false); }else{ bt4.setEnabled(true); } }//end if(o==tf1||o==tf2||o==tf3||o==tf5) } public void focusLost(FocusEvent e) { Object o=e.getSource(); Boolean flag=false; //标记提示信息显示 String text1=tf1.getText(); //获得文本框的内容 // String text2=tf2.getText(); // String text3=tf3.getText(); String text4=tf4.getText(); // String text5=tf5.getText(); WarnMsgPane pane=new WarnMsgPane(); if(o==tf4){ flag= pane.numCheck(text4); //得到判断房间价格是数值 if(flag){ }else{ jl9.setText("请输入数字数值"); }//end if(flag) }//end if(o==tf1) if(o==tf1){ flag=pane.emptyCheck(text1); if(!flag){ jl9.setText("请输入房间类型编号"); // bt4.setEnabled(true); }else{ bt4.setEnabled(true); } //enf if(!flag) }//end if(o==tf1) } //想表格中添加数据/* * 函数名:private void insertData(String id,String type,String bed,String price ,String yajin,String remark) * 参数: id 房间编号 type 房间类型 bed房间床位 price 房间价格 yajin 房间押金 remark 房间备注 * 返回值:void * 功能:向表格中插入数据 */public void insertData(String id,String type,String bed,String price ,String yajin,String remark){ if(count==5){ bt4.setEnabled(false); JOptionPane.showMessageDialog(this, "一次最多可添加5个房间", "提示", JOptionPane.WARNING_MESSAGE); }else{ tb.setValueAt(id, count, 0); tb.setValueAt(type, count, 1); tb.setValueAt(bed, count, 2); tb.setValueAt(price, count, 3); tb.setValueAt(yajin,count, 4); tb.setValueAt(remark,count, 5); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -