📄 delroompanel.java
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package com.redingsoft.setroomframe;import com.redingsoft.sql.SetSQL;import com.redingsoft.sunsdk.swing.OJButton;import com.redingsoft.sunsdk.swing.xtwh_table;import java.awt.BorderLayout;import java.awt.Color;import java.awt.Dimension;import java.awt.FlowLayout;import java.awt.event.*;import java.util.Vector;import javax.swing.*;import javax.swing.table.DefaultTableModel;/** * * @author Administrator */public class DelroomPanel extends JPanel implements ActionListener { //声明面板 private JPanel leftPane,centerPane,rigthPane,bottPane,mainPane; //声明表格 JTable tb1,tb2; //声明一个表模型 DefaultTableModel datamodel,datamodel2; //按钮声明 JButton bt1,bt2,bt3,bt4; //声明2个单选按钮 JRadioButton rb1,rb2; //声明一个按钮组 ButtonGroup bg; //实例化一个面板private JScrollPane sp1,sp2;//声明2个数组String[] cols={"房间编号","房间类型","房间状态","房间位置","房间电话","房间备注"};String[][] rows=new String[5][6];//声明一个数组用于存储选定行的数据String [][] selectValue=new String[1][6];//声明一个变量让TB2换行显示 int h=0; //声明向量存储SQL语句 Vector v=new Vector(); String copycodes[]=new String[5]; //构造函数初始化面 public DelroomPanel(){ //实例化面板 leftPane=new JPanel(new FlowLayout(FlowLayout.LEFT,0,0)); rigthPane=new JPanel(); centerPane=new JPanel(); bottPane=new JPanel(); mainPane=new JPanel(); //实例化按钮 bt1=new OJButton("com/redingsoft/pic/add.gif","添加","选中表格中的行增加到右侧信息栏",true); //添加按钮 bt2=new OJButton("com/redingsoft/pic/left2.gif","取消","取消到右侧信息栏",true); bt3= new OJButton("com/redingsoft/pic/b1.gif","刷新","刷新房间信息",true); bt4=new OJButton("com/redingsoft/pic/save.gif","完成","保存数据",true); //实例化2个单选按钮 rb1=new JRadioButton("按状态排序"); rb2=new JRadioButton("按类型排序"); //实例化按钮牛组 bg=new ButtonGroup(); bg.add(rb1); bg.add(rb2); //设置按钮大小 // bt1.setPreferredSize(new Dimension(50,10)); //初始化个面板的大小 leftPane.setPreferredSize(new Dimension(500,180)); rigthPane.setPreferredSize(new Dimension(500,150)); centerPane.setPreferredSize(new Dimension(490,50)); bottPane.setPreferredSize(new Dimension(400,50)); //初始化表格 datamodel =new DefaultTableModel() { @Override public boolean isCellEditable(int row, int column) { return false; } }; tb1=new JTable(datamodel); initTabel(); //初始化表格 datamodel2=new xtwh_table(rows,cols); tb2=new JTable(); tb2.setModel(datamodel2); initTb2(); //实例化滚动面板 // 实例化滚动条 sp1=new JScrollPane(tb1); //设置滚动面板初始大小 sp1.setPreferredSize(new Dimension(480,100)); sp2=new JScrollPane(tb2); sp2.setPreferredSize(new Dimension(492,120)); //添加个组件到面 centerPane.add(bt1); centerPane.add(bt2); leftPane.add(sp1); leftPane.add(centerPane); // leftPane.add(bt4); rigthPane.add(sp2); bottPane.add(rb1); bottPane.add(rb2); bottPane.add(bt3); bottPane.add(bt4); //设置左面板的参数 leftPane.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 1)); leftPane.setBorder(BorderFactory.createTitledBorder("房间信息")); //设置又面板的参数 rigthPane.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 1)); rigthPane.setBorder(BorderFactory.createTitledBorder("删除房间信息")); bottPane.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 1)); //添加到主面板上 //定义边界布局 BorderLayout border=new BorderLayout(10,10); mainPane.setLayout(border); mainPane.add(BorderLayout.NORTH,bottPane); mainPane.add(BorderLayout.CENTER,leftPane); // mainPane.add(BorderLayout.CENTER,centerPane); mainPane.add(BorderLayout.SOUTH,rigthPane); //添加侦听 AddListener(); //////////////////////////// this.add(mainPane); } //初始化表格/* * 函数名:public JScrollPane initTabel() * 参数:null * 返回值:void * 功能:制作并初始化表格 ***********************************/public void initTabel(){ //设置表格的模式 String sql="select id 房间编号,r_type_id 房间类型,state 房间状态,location 房间位置,r_tel 房间电话,remark 房间备注 from roominfo where delmark=0"; // datamodel.fireTableDataChanged(); //初始化表表模式 SetSQL.initDTM(datamodel,sql); if(datamodel.getRowCount()!=0){ //设置表模型 tb1.setModel(datamodel); } // tb=new JTable(); tb1.setRowHeight(20); tb1.setPreferredSize(new Dimension(490,500)); AddTableListener(tb1);}//初始化删除房间信息表public void initTb2(){ tb2.setRowSelectionAllowed(true); tb2.setPreferredSize(new Dimension(490,290)); }//添加侦听事件函数private void AddListener(){ bt1.addActionListener(this); //添加 bt2.addActionListener(this); //取消 bt3.addActionListener(this); //完成 bt4.addActionListener(this); //刷新 }//添加件表格侦听public void AddTableListener(final JTable tb1){ tb1.addMouseListener(new java.awt.event.MouseAdapter(){ @Override public void mouseClicked(java.awt.event.MouseEvent e){ try{ int selected=tb1.getSelectedRow(); int i=0; int colcount=0; for(i=0;i<6;i++){ selectValue[colcount][i]=(String)tb1.getValueAt(selected, i); // System.out.println(selectValue[colcount][i]); } }catch(Exception ex){ JOptionPane.showMessageDialog(null, "你还没有添加房间信息!"); } } });} public void actionPerformed(ActionEvent e) { Object o=e.getSource(); if(o==bt1){ //添加 exchange(); h++; } if(o==bt2){//取消 datamodel2.setDataVector(rows, cols) ; tb2.setModel(datamodel2); v.removeAllElements(); //移除向量中组件并清零 h=0; bt1.setEnabled(true); } if(o==bt3){ //刷 initTabel(); } if(o==bt4){//完成 try{ int i= v.size(); v.copyInto(copycodes); //将向量中的数据库语句复制到数组中 int flag=SetSQL.runTransaction(copycodes, i); if(flag==i){ JOptionPane.showMessageDialog(null, "删除"+flag+"个房间成功"); datamodel2.setDataVector(rows, cols) ; tb2.setModel(datamodel2); v.removeAllElements(); //移除向量中组件并清零 h=0; bt1.setEnabled(true); }else{ JOptionPane.showMessageDialog(null, "删除"+i+"个房间失败"); } }catch(Exception ex){ JOptionPane.showMessageDialog(null, "你还没有添加要删除房间信息!"); } } } //实现2各表中的值的传递 public void exchange(){ try{ if(h<5){ for(int i=0;i<6;i++){ tb2.setValueAt(selectValue[0][i], h, i); if(i==5){ //生成数据库语句 String sqlcodes=null; sqlcodes="delete from roominfo where id='"+selectValue[0][0]+"'"; //添加语句到V v.addElement(sqlcodes); } } }else{ JOptionPane.showMessageDialog(null, "最多同时删除5个房间"); bt1.setEnabled(false); } }catch(ArrayIndexOutOfBoundsException r){ } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -