📄 addreader.java~12~
字号:
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: 图书管理系统</p>
*
* <p>Description: AddReader类是继承JDialog类,主要是用来显示增加读者的对话框</p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: 浙江工业大学信息学院计算机系</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[]={"信息工程学院","化材学院","机电学院","生环学院","经贸学院"};
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;
//定义数据库操作对象
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("学号:");
jLabel1.setBounds(new Rectangle(48, 28, 51, 22));
jTextFieldName.setBounds(new Rectangle(129, 65, 96, 21));
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() {
public void actionPerformed(ActionEvent e) {
jComboBoxType_actionPerformed(e);
}
});
jLabel4.setText("性别:");
jLabel4.setBounds(new Rectangle(48, 139, 51, 22));
jRadioButtonMale.setSelected(true);
jRadioButtonMale.setText("男");
jRadioButtonMale.setBounds(new Rectangle(129, 139, 37, 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));
jLabel5.setText("电话号码:");
jLabel5.setBounds(new Rectangle(48, 176, 60, 22));
jLabel6.setText("学院:");
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("专业:");
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));
jTextFieldXueHao.setBounds(new Rectangle(129, 28, 96, 21));
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("信息工程学院")){
jComboBoxSpecialty.removeAllItems();
jComboBoxSpecialty.addItem("计算机科学与技术");
jComboBoxSpecialty.addItem("自动化");
jComboBoxSpecialty.addItem("通信工程");
jComboBoxSpecialty.addItem("电子信息工程");
jComboBoxSpecialty.addItem("电气工程及其自动化");
}
else if(xyName.equals("化材学院")){
jComboBoxSpecialty.removeAllItems();
jComboBoxSpecialty.addItem("材料科学与工程");
jComboBoxSpecialty.addItem("化学工程与工艺");
jComboBoxSpecialty.addItem("应用化学");
}
}
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, "学号不许为空!");
return;
}
if(name.equals("")){
JOptionPane.showMessageDialog(this, "姓名不许为空!");
return;
}
if(xsType.equals("")){
JOptionPane.showMessageDialog(this, "用户类型不许为空!");
return;
}
if(xyName.equals("")){
JOptionPane.showMessageDialog(this, "学院不许为空!");
return;
}
if(specialty.equals("")){
JOptionPane.showMessageDialog(this, "专业不许为空!");
return;
}
if(grade.equals("")){
JOptionPane.showMessageDialog(this, "年级不许为空!");
return;
}
//生成sql操作语句,查询要添加的用户名是否已经存在,若存在执行删除,若不存在提示并返回
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 {
//然后执行插入操作
//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+"')";
//由数据库操作对象执行数据库操作,并返回操作成功失败的提示信息
if (db.executeSql(strSQL)) {
JOptionPane.showMessageDialog(null, "成功添加!");
this.dispose();
}
else {
JOptionPane.showMessageDialog(null, " 添加失败,请重新操作!");
}
}
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="";
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -