?? addroompanel.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.*;import java.awt.event.*;import java.util.Vector;import javax.swing.*;import javax.swing.table.TableModel;/** * * @author Administrator */public class AddroomPanel extends JPanel implements ActionListener,FocusListener{ //聲明標簽又能給與顯示輸入提示信息 private JLabel jl1,jl2,jl3,jl4,jl5,jl6,jl7,jl8; //聲明文本框用于用戶輸入 JTextField tf1,tf2,tf3,tf4,tf5,tf6,tf7,tf8; //聲明2個單選按鈕 JRadioButton rb1,rb2; //聲明一個按鈕組 ButtonGroup gb; //聲明一個下拉列表框 JComboBox jcb; //聲明面板 JPanel leftPanel,rigthPanel,bottPanel,mainPanel; //為每個文本框聲明一個面板 JPanel jp1,jp2,jp3,jp4,jp5,jp6,jp7,jp8; // 聲明一個滾動面板 JScrollPane sp; //聲明一個表格用來顯示輸入信息 JTable tb; //聲明按鈕 JButton bt1,bt2,bt3,bt4; //聲明一個2個向量來初始化表格 String[] col={"房間編號","類型編號","房間狀態(tài)","房間位置","房間電話","房間備注","房間計時"}; String[][] row=new String[5][7]; //聲明一個表數(shù)據(jù)模式 TableModel dataModel; private int count; //創(chuàng)建一個向量存儲數(shù)據(jù)庫語句 Vector v=new Vector(); String [] sqlcodes=new String[5]; //構(gòu)造方法 AddroomPanel(){ //實例化標簽 jl1=new JLabel("*房間編號"); jl2=new JLabel("*類型編號"); jl3=new JLabel("*房間狀態(tài)"); jl4=new JLabel("*房間位置"); jl5=new JLabel("*房間電話"); jl6=new JLabel("房間備注"); jl7=new JLabel("*房間計時"); jl8=new JLabel("提示信息"); //初始化下拉列表 jcb=new JComboBox(); String sql="select id from roomtype where delmark=0"; SetSQL.initJComboBox(jcb, sql); //實例化文本框 tf1=new JTextField(10); //房間編號 // tf2=new JTextField(10); //房間類型編號 tf3=new JTextField(10); //房間位置 tf4=new JTextField(10); //房間電話 tf5=new JTextField(10); //房間備注 tf6=new JTextField(10); //房間計時 tf6.setEditable(false); //實例化單選按鈕 rb1=new JRadioButton("可供"); //房間狀態(tài) rb2=new JRadioButton("不可供"); rb1.setSelected(true); //將按鈕添加按鈕組 gb=new ButtonGroup(); gb.add(rb1); gb.add(rb2); //實例化按鈕 bt1=new OJButton("com/redingsoft/pic/add.gif","添加","臨時將數(shù)據(jù)暫存在表格中",true); //田間房間到臨時表格里 bt2=new OJButton("com/redingsoft/pic/save.gif","保存","將數(shù)據(jù)提交到數(shù)據(jù)庫",true); //添加到數(shù)據(jù)庫 bt3=new OJButton("com/redingsoft/pic/recall.gif","取消","取消添加操作",true); //退出此操作 //初始化表格和面板 sp=initTabel(); /////////////////////////////////////////////////// //實例化個面板 jp1=new JPanel(); //放置房間編號 jp2=new JPanel(); //放置房間類型 jp3=new JPanel(); //放置房間狀態(tài) jp4=new JPanel(); //房間位置 jp5=new JPanel(); //房間電話 jp6=new JPanel(); //房間備注 jp7=new JPanel(); //房間計時 jp8=new JPanel(); //提示信息放置區(qū) /////////////////////////////////////////////// leftPanel=new JPanel(new FlowLayout(FlowLayout.CENTER,25,8)); //設置面板的初始大小 leftPanel.setAutoscrolls(true); leftPanel.setPreferredSize(new Dimension(200,210)) ; BorderLayout flow=new BorderLayout(); rigthPanel=new JPanel(); bottPanel=new JPanel(); // mainPanel=new JPanel(new BorderLayout()); //添加組件到面板 jp1.add(jl1); //房間編號 jp1.add(tf1); jp2.add(jl2); //類型編號 // jp2.add(tf2); jp2.add(jcb); jp3.add(jl3); jp3.add(rb1); //房間狀態(tài) jp3.add(rb2); jp4.add(jl4); //房間位置 jp4.add(tf3); jp5.add(jl5); //房間電話 jp5.add(tf4); jp6.add(jl6); //房間備注 jp6.add(tf5); jp7.add(jl7); //房間計時 jp7.add(tf6); jp8.add(jl8); //顯示提示信息 //將房間信息組件添加到leftPane leftPanel.add(jp1); //房間編號 leftPanel.add(jp2); //類型編號 leftPanel.add(jp3); //房間狀態(tài) leftPanel.add(jp4); //房間位置 leftPanel.add(jp5); //房間電話 leftPanel.add(jp6); //房間備注 leftPanel.add(jp7); //房間計時 leftPanel.add(jp8); //提示信息 //設置jp8 jp8.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.RED, 1), "提示信息")); jp8.setPreferredSize(new Dimension(190,70)); //設置左面板邊框 leftPanel.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 5)); leftPanel.setBorder(BorderFactory.createTitledBorder("房間信息添加")); //將滾動面板添加到rightPanel rigthPanel.add(sp); //設置有面板邊框 rigthPanel.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 5)); rigthPanel.setBorder(BorderFactory.createTitledBorder("房間信息")); rigthPanel.setSize(100, 50); //將按鈕添加到bottPanel bottPanel.add(bt1); //添加 bottPanel.add(bt2); //保存 bottPanel.add(bt3); //刪除 //添加偵聽 AddListener(); //社會按鈕禁用 bt1.setEnabled(false); bt2.setEnabled(false); bottPanel.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 1)); this.setLayout(flow); this.add(leftPanel,BorderLayout.EAST); //添加主面板到 this.add(rigthPanel,BorderLayout.WEST); this.add( bottPanel,BorderLayout.SOUTH); // this.setSize(600, 500); //設置tf1獲得焦點 tf1.requestFocusInWindow(); } //添加偵聽 private void AddListener(){ tf1.addFocusListener(this); //房間編號 tf3.addFocusListener(this); tf4.addFocusListener(this); tf5.addFocusListener(this); tf6.addFocusListener(this); bt1.addActionListener(this); //添加 bt2.addActionListener(this); //保存 bt3.addActionListener(this); //退出 } //初始化表格/* * 函數(shù)名:public JScrollPane initTabel() * 參數(shù):null * 返回值:void * 功能:制作并初始化表格 ***********************************/public JScrollPane initTabel(){ //實例化表格并讓表格不可編輯 TableModel datamodel=new xtwh_table(row,col); tb=new JTable(); //設置表模型 tb.setModel(datamodel); tb.setRowHeight(20); tb.setPreferredSize(new Dimension(360,360)); // 實例化滾動條 sp=new JScrollPane(tb); //設置滾動面板初始大小 sp.setPreferredSize(new Dimension(360,360)); //添加表格到centerPnae //################################################################ return sp;}//實現(xiàn)偵聽 public void actionPerformed(ActionEvent e) { //獲得按鈕的名稱 Object o=e.getSource(); String id=tf1.getText().trim(); //房間編號值 String type=(String) jcb.getSelectedItem(); //房間類型 String state=null; //房間狀態(tài) if(rb1.isSelected()==true){ state=rb1.getText(); }else{ state=rb2.getText(); } String location=tf3.getText().trim(); //房間位置 String tel=tf4.getText().trim(); //房間電話 String remark=tf5.getText().trim(); //房間備注 String statetime=tf6.getText().trim(); //房間計時 if(o==bt1){ //添加 insertData(id,type,state,location,tel,remark,statetime); String sqlcode="insert into roominfo(id,r_type_id,state,location,r_tel,remark,statetime) values" +"('"+id+"','"+type+"','"+state+"','"+location+"','"+tel+"','"+remark+"','"+statetime+"')"; v.addElement(sqlcode); count++; }//end if(o==bt1) if(o==bt2){ //保存 if(v.isEmpty()){ JOptionPane.showMessageDialog(null, "請?zhí)砑訑?shù)據(jù)后在保存", "提示", JOptionPane.WARNING_MESSAGE); }else{ v.copyInto(sqlcodes); int flag=SetSQL.runTransaction(sqlcodes, count); if(v.size()>flag){ JOptionPane.showMessageDialog(null, "房間信息田間失敗請與網(wǎng)絡管理員聯(lián)系", "提示", JOptionPane.WARNING_MESSAGE); }else{ jl8.setText("房間信息添加"+flag+"間成功"); //重置組件按鈕 v.removeAllElements(); //一處向量的中所有組件并將其大小設為0 bt1.setEnabled(true); tf1.setText(""); tf3.setText(""); tf4.setText(""); tf5.setText(""); tf6.setText(" "); count=0; }//end if(v.size()>flag) } //end if(v.isEmpty) }//en if(o==bt2) if(o==bt3){ //取消 for(int i=0; i<sqlcodes.length;i++){ for(int j=0;j<=6;j++) { tb.setValueAt("", i, j); } } v.removeAllElements(); //一處向量的中所有組件并將其大小設為0 count=0; //將表格索引位置歸零 } } public void focusGained(FocusEvent e) { Object o=e.getSource(); if(o==tf1){ jl8.setForeground(Color.BLUE); jl8.setFont(new Font("宋體",Font.BOLD,15)); jl8.setText("帶*號的不可以為空"); } if(o==tf6){ jl8.setText("請輸入數(shù)值"); } } public void focusLost(FocusEvent e) { Object o=e.getSource(); if(o==tf1){ if(tf1.getText().equals("")){ bt1.setEnabled(false); bt2.setEnabled(false); } }//end if(o==tf1) if(o==tf3){ if(tf3.getText().equals("")){ } }if(o==tf4){ //房間電話 if(tf4.getText().equals("")){ bt1.setEnabled(false); bt2.setEnabled(false); }else{ bt1.setEnabled(true); bt2.setEnabled(true); } }if(o==tf6){ } } //向表中添加數(shù)據(jù) /* * 函數(shù)名:private void insertData(String id,String type,String state,String location ,String tel,String remark String statetime) * 參數(shù): id 房間編號 type 房間類型 state房間狀態(tài) location 房間位置 tel 房間電話 remark 房間備注 statetime 狀態(tài)計時 * 返回值:void * 功能:向表格中插入數(shù)據(jù) */private void insertData(String id,String type,String state,String location ,String tel,String remark, String statetime){ if(count==5){ bt1.setEnabled(false); JOptionPane.showMessageDialog(this, "一次最多可添加5個房間", "提示", JOptionPane.WARNING_MESSAGE); }else{ tb.setValueAt(id, count, 0); tb.setValueAt(type, count, 1); tb.setValueAt(state, count, 2); tb.setValueAt(location, count, 3); tb.setValueAt(tel,count, 4); tb.setValueAt(remark,count, 5); tb.setValueAt(statetime, count, 6); } }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -