?? 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 + -