?? addmemberpane.java
字號:
/* * 文件名: AddMemberPane * 說明:添加會員信息 */package com.redingsoft.customerinfo;import com.redingsoft.sql.SetSQL;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.sql.ResultSet;import java.sql.SQLException;import javax.swing.*;/** * * @author Administrator */public class AddMemberPane extends JDialog implements ActionListener { //聲明標簽 private JLabel jl1,jl2,jl3,jl4,jl5,jl6; //聲明文本框 private JTextField tf1,tf2,tf3,tf4,tf5,tf6; //聲明2按鈕 private JButton bt1,bt2; //聲明面板 private JPanel jp1,jp2,mainPane; //構(gòu)造方法 public AddMemberPane (){ //實例化標簽 jl1=new JLabel("*會員編號*"); jl2=new JLabel("*會員姓名*"); jl3=new JLabel("*會員性別*"); jl4=new JLabel("*會員電話*"); jl5=new JLabel("*會員地址*"); jl6=new JLabel(" 會員備注 "); //實例化文本框 tf1=new JTextField(15); //會員編號 tf2=new JTextField(15); //姓名 tf3=new JTextField(15); //性別 tf4=new JTextField(15); //電話 tf5=new JTextField(15); //地址 tf6=new JTextField(15); //備注 // tf1=new JTextField(); //實例化按鈕 bt1=new JButton("保存"); bt2=new JButton("重置"); //實例化面板 jp1=new JPanel(new FlowLayout(FlowLayout.CENTER,10,10)); jp2=new JPanel(new FlowLayout(FlowLayout.CENTER,1,10)); mainPane=new JPanel(new BorderLayout()); //將組件添加到面板上 jp1.add(jl1); //添加會員編號 jp1.add(tf1); jp1.add(jl2); //添加會員姓名 jp1.add(tf2); jp1.add(jl3); //添加會員性別 jp1.add(tf3); jp1.add(jl4); //添加會員電話 jp1.add(tf4); jp1.add(jl5); //添加會員地址 jp1.add(tf5); jp1.add(jl6); //添加會員備注 jp1.add(tf6); jp2.add(bt1); //添加 jp2.add(bt2); //重置 bt1.setContentAreaFilled(false); //透明按鈕 bt2.setContentAreaFilled(false); // 設置面板大小和邊框 jp1.setPreferredSize(new Dimension(25,250)); jp2.setPreferredSize(new Dimension(200,70)); jp1.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 1)); jp1.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.GRAY), "會員信息")); // jp2.setBackground(Color.LIGHT_GRAY); // jp1.setBackground(Color.LIGHT_GRAY); //添加偵聽 AddListener(); //添加到主面板 mainPane.add(BorderLayout.NORTH,jp1); mainPane.add(BorderLayout.SOUTH,jp2); //添加到窗口 this.add(mainPane); this.setSize(300, 350); this.setTitle("添加會員"); this.setResizable(false); this.setDefaultCloseOperation(DISPOSE_ON_CLOSE); } /***************************** public static void main(String[] args){ new AddMemberPane(); } *******************************/ //添加偵聽事件 private void AddListener(){ bt1.addActionListener(this); bt2.addActionListener(this); } public void actionPerformed(ActionEvent e) { //throw new UnsupportedOperationException("Not supported yet."); Object o=e.getSource(); if(o==bt1){ if(tf1.getText().trim().equals("")){ WarnMsg("提示","會員編號不可以為空!"); }else{ String id=tf1.getText().trim(), name=tf2.getText().trim(), sex=tf3.getText().trim(), tel=tf4.getText().trim(), adress=tf5.getText().trim(), remark=tf6.getText().trim(); String sql="insert into member(m_id,m_name,m_sex,m_tel,m_adress,remark) values('" + id+"','"+name+"','"+sex+"','"+tel+"','"+adress+"','"+remark+"')"; String sqls="select m_name from member where m_id='"+id+"'"; int a=checkrs(sqls); //檢查數(shù)據(jù)是否重復 if(a!=0){ WarnMsg("提示","會員編號已存在,請從新添加。"); }else{ int i=SetSQL.executeUpdate(sql); if(i==1){ WarnMsg("提示","添加成功"); }else{ WarnMsg("提示","添加失敗"); } }//end if(a!=0) }//end if(tf1.getText().trim().equals("")) }//en if(o==bt1) if(o==bt2){ //重置 reset(); tf1.requestFocusInWindow(); } }/******************************************************* * 函數(shù)名稱:WarnMsgPane(String title ,String msg ,component null) * 參數(shù):tile 提示標題 msg 提示消息 顯示他的父組件 * 功能:為錯誤的輸入和連接錯誤做出提示 ************************************************************/ public static void WarnMsg(String title,String msg){ JOptionPane.showMessageDialog(null,msg,title,JOptionPane.WARNING_MESSAGE); } /****************************************** * 函數(shù)名: checkrs(String sql) * 參數(shù):sql 要執(zhí)行的數(shù)據(jù)庫語句 * 返回值:int * 功能:查詢數(shù)據(jù)庫中是否有此條件的記錄 * ***********************************/ public static int checkrs(String sql){ ResultSet rs=SetSQL.executeQuery(sql); int count=0; try { while (rs.next()) { count++; } } catch (SQLException ex) { } return count; }//重置按鈕 public void reset(){ tf1.setText(null); tf2.setText(null); tf3.setText(null); tf4.setText(null); tf5.setText(null); tf6.setText(null); } }
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -