?? addreader.java
字號:
package bookmanage;
import java.awt.*;
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Rectangle;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.*;
import java.sql.SQLException;
import java.sql.ResultSet;
/**
* <p>Title: 圖書管理系統(tǒng)</p>
*
* <p>Description: AddReader類是繼承JDialog類,主要是用來顯示增加讀者的對話框</p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: 浙江工業(yè)大學(xué)信息學(xué)院計算機系</p>
*
* @author 曾文秋
* @version 1.0
*/
public class AddReader
extends JDialog {
JLabel jLabel1 = new JLabel();
JTextField jTextFieldXueHao = new JTextField();
JTextField jTextFieldName = new JTextField();
JLabel jLabel2 = new JLabel();
JLabel jLabel3 = new JLabel();
String xueShengType[]={"本科生","研究生","碩士生","博士生","教職工"};
JComboBox jComboBoxType = new JComboBox(xueShengType);
JLabel jLabel4 = new JLabel();
JRadioButton jRadioButtonMale = new JRadioButton();
JRadioButton jRadioButtonFemale = new JRadioButton();
JTextField jTextFieldTelephone = new JTextField();
JLabel jLabel5 = new JLabel();
JLabel jLabel6 = new JLabel();
String xueYuanName[]={"信息工程學(xué)院","化材學(xué)院","機電學(xué)院","生環(huán)學(xué)院","經(jīng)貿(mào)學(xué)院"};
JComboBox jComboBoxInstitute = new JComboBox(xueYuanName);
JComboBox jComboBoxSpecialty = new JComboBox();
JLabel jLabel7 = new JLabel();
JLabel jLabel8 = new JLabel();
JTextField jTextFieldGrade = new JTextField();
ButtonGroup buttonGroupSex = new ButtonGroup();
JButton jButtonAdd = new JButton();
JButton jButtonCancel = new JButton();
String sex="男";
String xsType="";
String xyName="";
String specialty="";
String xueHao="";
String name="";
String telephone="";
String grade="";
Calendar cd=null;
String strSQL="";
ResultSet rs;
//定義數(shù)據(jù)庫操作對象
private DBManager db = new DBManager();
public AddReader(Frame owner, String title, boolean modal) {
super(owner, title, modal);
try {
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
jbInit();
pack();
}
catch (Exception exception) {
exception.printStackTrace();
}
}
public AddReader() {
this(new Frame(), "AddReader", false);
}
private void jbInit() throws Exception {
this.setResizable(false);
this.setTitle("增加讀者");
this.getContentPane().setLayout(null);
jLabel1.setText("學(xué)號:");
jLabel1.setBounds(new Rectangle(48, 28, 51, 22));
jTextFieldName.setBounds(new Rectangle(129, 65, 96, 21));
jTextFieldName.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jButtonAdd_actionPerformed(e);
}
});
jLabel2.setText("姓名:");
jLabel2.setBounds(new Rectangle(48, 65, 51, 22));
jLabel3.setText("類型:");
jLabel3.setBounds(new Rectangle(48, 102, 51, 22));
jComboBoxType.setBounds(new Rectangle(129, 102, 96, 23));
jComboBoxType.addActionListener(new ActionListener() {
jComboBoxType_actionPerformed(e);
}
});
jLabel4.setText("性別:");
jLabel4.setBounds(new Rectangle(48, 139, 51, 22));
jRadioButtonMale.setSelected(true);
jRadioButtonMale.setText("男");
jRadioButtonMale.setBounds(new Rectangle(129, 139, 50, 23));
jRadioButtonMale.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jRadioButtonMale_actionPerformed(e);
}
});
jRadioButtonFemale.setSelected(true);
jRadioButtonFemale.setText("女");
jRadioButtonFemale.setBounds(new Rectangle(181, 139, 41, 23));
jRadioButtonFemale.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jRadioButtonFemale_actionPerformed(e);
}
});
jTextFieldTelephone.setBounds(new Rectangle(129, 176, 96, 21));
jTextFieldTelephone.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jButtonAdd_actionPerformed(e);
}
});
jLabel5.setText("電話號碼:");
jLabel5.setBounds(new Rectangle(48, 176, 60, 22));
jLabel6.setText("學(xué)院:");
jLabel6.setBounds(new Rectangle(48, 213, 51, 22));
jComboBoxInstitute.setBounds(new Rectangle(129, 213, 96, 23));
jComboBoxInstitute.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jComboBoxInstitute_actionPerformed(e);
}
});
jLabel7.setText("專業(yè):");
jLabel7.setBounds(new Rectangle(48, 250, 51, 22));
jLabel8.setText("年級:");
jLabel8.setBounds(new Rectangle(48, 287, 51, 22));
jTextFieldGrade.setBounds(new Rectangle(129, 287, 96, 21));
jTextFieldGrade.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jButtonAdd_actionPerformed(e);
}
});
jTextFieldXueHao.setBounds(new Rectangle(129, 28, 96, 21));
jTextFieldXueHao.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jButtonAdd_actionPerformed(e);
}
});
jButtonAdd.setBounds(new Rectangle(49, 327, 81, 23));
jButtonAdd.setText("增加");
jButtonAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jButtonAdd_actionPerformed(e);
}
});
jButtonCancel.setBounds(new Rectangle(145, 327, 81, 23));
jButtonCancel.setText("取消");
jButtonCancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jButtonCancel_actionPerformed(e);
}
});
jComboBoxSpecialty.setBounds(new Rectangle(128, 250, 96, 23));
jComboBoxSpecialty.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jComboBoxSpecialty_actionPerformed(e);
}
});
buttonGroupSex.add(jRadioButtonMale);
buttonGroupSex.add(jRadioButtonFemale);
cd=Calendar.getInstance();
jTextFieldGrade.setText(""+cd.get(Calendar.YEAR));
this.getContentPane().add(jTextFieldXueHao);
this.getContentPane().add(jLabel7);
this.getContentPane().add(jLabel4);
this.getContentPane().add(jLabel3);
this.getContentPane().add(jLabel5);
this.getContentPane().add(jRadioButtonMale);
this.getContentPane().add(jComboBoxInstitute);
this.getContentPane().add(jRadioButtonFemale);
this.getContentPane().add(jTextFieldTelephone);
this.getContentPane().add(jTextFieldName);
this.getContentPane().add(jComboBoxType);
this.getContentPane().add(jComboBoxSpecialty);
this.getContentPane().add(jLabel8);
this.getContentPane().add(jTextFieldGrade);
this.getContentPane().add(jButtonAdd);
this.getContentPane().add(jButtonCancel);
this.getContentPane().add(jLabel1);
this.getContentPane().add(jLabel6);
this.getContentPane().add(jLabel2);
}
public void jComboBoxInstitute_actionPerformed(ActionEvent e) {
xyName=jComboBoxInstitute.getSelectedItem().toString().trim();
if(xyName.equals("信息工程學(xué)院")){
jComboBoxSpecialty.removeAllItems();
jComboBoxSpecialty.addItem("計算機科學(xué)與技術(shù)");
jComboBoxSpecialty.addItem("自動化");
jComboBoxSpecialty.addItem("通信工程");
jComboBoxSpecialty.addItem("電子信息工程");
jComboBoxSpecialty.addItem("電氣工程及其自動化");
}
else if(xyName.equals("化材學(xué)院")){
jComboBoxSpecialty.removeAllItems();
jComboBoxSpecialty.addItem("材料科學(xué)與工程");
jComboBoxSpecialty.addItem("化學(xué)工程與工藝");
jComboBoxSpecialty.addItem("應(yīng)用化學(xué)");
}
}
public void jComboBoxType_actionPerformed(ActionEvent e) {
xsType=(String)jComboBoxType.getSelectedItem();
if(xsType!=null){
xsType=xsType.trim();
}
else xsType="";
}
/**
*
* @param e ActionEvent
* @todo 重新寫插入的sql語句,未完成
*/
public void jButtonAdd_actionPerformed(ActionEvent e) {
xueHao=jTextFieldXueHao.getText().trim();
name=jTextFieldName.getText().trim();
telephone=jTextFieldTelephone.getText().trim();
grade=jTextFieldGrade.getText().trim();
if(xueHao.equals("")){
JOptionPane.showMessageDialog(this, "學(xué)號不許為空!");
return;
}
if(name.equals("")){
JOptionPane.showMessageDialog(this, "姓名不許為空!");
return;
}
if(xsType.equals("")){
JOptionPane.showMessageDialog(this, "用戶類型不許為空!");
return;
}
if(xyName.equals("")){
JOptionPane.showMessageDialog(this, "學(xué)院不許為空!");
return;
}
if(specialty.equals("")){
JOptionPane.showMessageDialog(this, "專業(yè)不許為空!");
return;
}
if(grade.equals("")){
JOptionPane.showMessageDialog(this, "年級不許為空!");
return;
}
//生成sql操作語句,查詢要添加的用戶名是否已經(jīng)存在,若存在執(zhí)行刪除,若不存在提示并返回
strSQL = "select * from buser where xuehao='" + xueHao + "'";
rs = db.getResult(strSQL);
boolean isexist = false;
try {
isexist = rs.first();
}
catch (SQLException ex1) {
}
//若用戶名存在,提示警告信息
if (isexist){
JOptionPane.showMessageDialog(this, "用戶名已存在,請重新寫一個用戶名!");
jTextFieldXueHao.setText("");
return;
}
else {
//然后執(zhí)行插入操作
//strSQL="insert into user ()values('" + xueHao + "','"+name+"','"+xsType+"','"
// +sex+"','"+telephone+"','"+xyName+"','"+specialty+"','"++"','"++"','"++"')";
strSQL="insert into buser(xuehao,name,type,sex,telephone,institute,specialty,grade)"+
"values('"+xueHao+"','"+name+"','"+xsType+"','"+sex+"','"+telephone+"','"+xyName+"','"+specialty+"','"+grade+"')";
//由數(shù)據(jù)庫操作對象執(zhí)行數(shù)據(jù)庫操作,并返回操作成功失敗的提示信息
if (db.executeSql(strSQL)) {
JOptionPane.showMessageDialog(this, "成功添加!");
this.dispose();
}
else {
JOptionPane.showMessageDialog(this, " 添加失敗,請重新操作!");
}
}
db.close();
}
public void jButtonCancel_actionPerformed(ActionEvent e) {
this.dispose();
}
public void jRadioButtonMale_actionPerformed(ActionEvent e) {
sex="男".trim();
}
public void jRadioButtonFemale_actionPerformed(ActionEvent e) {
sex="女".trim();
}
public void jComboBoxSpecialty_actionPerformed(ActionEvent e) {
specialty=(String)jComboBoxSpecialty.getSelectedItem();
if(specialty!=null){
specialty=specialty.trim();
}
else specialty="";
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -