📄 ownerinfo.java
字号:
import javax.swing.*;
import java.awt.*;
import com.borland.jbcl.layout.*;
import java.awt.event.*;
import java.sql.*;
public class OwnerInfo
extends JFrame {
JButton btnExit = new JButton();
JLabel jLabel8 = new JLabel();
JButton btnAdd = new JButton();
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JButton btnSave = new JButton();
JTextField txtOrg = new JTextField();
JTextField txtWorkTime = new JTextField();
JLabel jLabel9 = new JLabel();
JButton btnDelete = new JButton();
JButton btnEdit = new JButton();
JTextField txtBirthday = new JTextField();
JTextField txtName = new JTextField();
JPanel jPanel1 = new JPanel();
XYLayout xYLayout2 = new XYLayout();
JLabel jLabel10 = new JLabel();
JComboBox cboGender = new JComboBox();
XYLayout xYLayout1 = new XYLayout();
DBManager db = new DBManager(); //数据库操作
String operType = new String(); //操作类型
String curID = new String();
JButton btnViewFamily = new JButton();
public OwnerInfo() {
try {
jbInit();
initForm();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
public OwnerInfo(String ID) {
try {
jbInit();
setCurID(ID);
initForm();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
void jbInit() throws Exception {
jPanel1.setBorder(BorderFactory.createEtchedBorder());
jPanel1.setLayout(xYLayout2);
txtName.setFont(new java.awt.Font("宋体", 0, 12));
txtName.setText("");
txtBirthday.setText("");
txtBirthday.setFont(new java.awt.Font("宋体", 0, 12));
btnEdit.setText("修改");
btnEdit.addActionListener(new OwnerInfo_btnEdit_actionAdapter(this));
btnEdit.setFont(new java.awt.Font("宋体", 0, 12));
btnDelete.setText("删除");
btnDelete.addActionListener(new OwnerInfo_btnDelete_actionAdapter(this));
btnDelete.setFont(new java.awt.Font("宋体", 0, 12));
jLabel9.setText("工作时间:");
jLabel9.setFont(new java.awt.Font("宋体", 0, 12));
txtWorkTime.setText("");
txtWorkTime.setFont(new java.awt.Font("宋体", 0, 12));
txtOrg.setFont(new java.awt.Font("宋体", 0, 12));
txtOrg.setText("");
btnSave.setText("保存");
btnSave.addActionListener(new OwnerInfo_btnSave_actionAdapter(this));
btnSave.setFont(new java.awt.Font("宋体", 0, 12));
jLabel2.setText("单位:");
jLabel2.setFont(new java.awt.Font("宋体", 0, 12));
jLabel1.setFont(new java.awt.Font("宋体", 0, 12));
jLabel1.setText("出生年月:");
btnAdd.setFont(new java.awt.Font("宋体", 0, 12));
btnAdd.setText("新增");
btnAdd.addActionListener(new OwnerInfo_btnAdd_actionAdapter(this));
jLabel8.setText("姓名:");
jLabel8.setRequestFocusEnabled(true);
jLabel8.setFont(new java.awt.Font("宋体", 0, 12));
btnExit.setText("退出");
btnExit.addActionListener(new OwnerInfo_btnExit_actionAdapter(this));
btnExit.setFont(new java.awt.Font("宋体", 0, 12));
this.getContentPane().setLayout(xYLayout1);
jLabel10.setText("性别:");
jLabel10.setFont(new java.awt.Font("宋体", 0, 12));
cboGender.setFont(new java.awt.Font("宋体", 0, 12));
xYLayout1.setWidth(435);
xYLayout1.setHeight(218);
this.setTitle("房主信息维护");
btnViewFamily.setFont(new java.awt.Font("宋体", 0, 12));
btnViewFamily.addActionListener(new OwnerInfo_btnViewFamily_actionAdapter(this));
btnViewFamily.setText("查看家庭信息");
btnViewFamily.addActionListener(new OwnerInfo_btnViewFamily_actionAdapter(this));
jPanel1.add(jLabel1, new XYConstraints(13, 57, 68, 18));
jPanel1.add(txtBirthday, new XYConstraints(75, 51, 112, 24));
jPanel1.add(jLabel9, new XYConstraints(203, 54, 61, 17));
jPanel1.add(jLabel2, new XYConstraints(13, 92, 53, 17));
jPanel1.add(jLabel8, new XYConstraints(13, 20, 49, 18));
jPanel1.add(txtName, new XYConstraints(75, 16, 111, 24));
jPanel1.add(jLabel10, new XYConstraints(203, 22, 49, 18));
jPanel1.add(txtOrg, new XYConstraints(75, 91, 175, 24));
jPanel1.add(cboGender, new XYConstraints(267, 19, 112, 24));
jPanel1.add(txtWorkTime, new XYConstraints(267, 51, 112, 24));
jPanel1.add(btnEdit, new XYConstraints(78, 134, 61, 27));
jPanel1.add(btnAdd, new XYConstraints(14, 134, 59, 27));
jPanel1.add(btnDelete, new XYConstraints(143, 134, 61, 27));
jPanel1.add(btnSave, new XYConstraints(254, 134, 61, 27));
jPanel1.add(btnExit, new XYConstraints(319, 134, 61, 27));
jPanel1.add(btnViewFamily, new XYConstraints(266, 91, 113, 27));
this.getContentPane().add(jPanel1, new XYConstraints(17, 19, 400, 180));
}
void btnExit_actionPerformed(ActionEvent e) {
if (!operType.equals("none")) {
int ir = CommonDialog.showDialog(3, "房产管理系统", "当前操作尚未保存,确定要退出吗?");
if (ir == 2) {
return;
}
}
this.dispose();
}
void setCurID(String ID) {
curID = ID;
}
void setOperType(String type) {
operType = type;
}
void btnAdd_actionPerformed(ActionEvent e) {
if (!operType.equals("none")) {
int ir = CommonDialog.showDialog(3, "房产管理系统", "当前操作尚未保存,确定要新增吗?");
if (ir == 2) {
return;
}
}
groupSetEnabled(true);
btnSetEnabled(true, false, false, true);
clearData();
setCurID("");
setOperType("add");
}
void initForm() {
groupSetEnabled(false);
db.fullCombo(cboGender, "Gender");
setOperType("none");
if (!curID.equals("")) {
fullInfo(curID);
btnSetEnabled(true, true, true, false);
}
else {
btnSetEnabled(true, false, false, false);
}
}
void fullInfo(String ID) {
String sql = "select * from OwnerInfo where ID='" + ID + "'";
try {
ResultSet rs = db.getResult(sql);
if (rs.first()) {
txtName.setText(rs.getString("Name")); //姓名
cboGender.setSelectedIndex(Integer.parseInt(rs.getString("Gender")) - 1);//性别
txtBirthday.setText(rs.getString("Birthday"));//出生年月
txtWorkTime.setText(rs.getString("WorkTime"));//参加工作时间
txtOrg.setText(rs.getString("Org"));//工作单位
}
else {
rs.close();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
void groupSetEnabled(boolean enabled) {
txtName.setEnabled(enabled);
cboGender.setEnabled(enabled);
txtBirthday.setEnabled(enabled);
txtWorkTime.setEnabled(enabled);
txtOrg.setEnabled(enabled);
}
void btnSetEnabled(boolean add, boolean edit, boolean delete, boolean save) {
btnAdd.setEnabled(add);
btnEdit.setEnabled(edit);
btnDelete.setEnabled(delete);
btnSave.setEnabled(save);
}
void clearData() {
txtName.setText("");
cboGender.setSelectedItem(null);
txtBirthday.setText("");
txtWorkTime.setText("");
txtOrg.setText("");
}
boolean checkData() {
if (txtName.getText().trim().equals("")) {
CommonDialog.showDialog(CommonDialog.OK, "房产管理系统", "姓名不能为空!");
txtName.setFocusable(true);
return false;
}
return true;
}
void btnSave_actionPerformed(ActionEvent e) {
if (!checkData()) {
return;
}
String sql = new String();
String id = new String(); //人员ID
String gender = new String(); //性别
if (cboGender.getSelectedIndex() >= 0) {
gender = db.CodeDesConvert("Gender", "",cboGender.getSelectedItem().toString(), 0);
}
if (operType.equals("add")) {
id = db.getID("BuildInfo","ID");
setCurID(id);
sql = "insert into OwnerInfo values('" + id + "','" + txtName.getText() +
"','" + gender + "','" + txtBirthday.getText() + "','" +
txtWorkTime.getText() + "','" + txtOrg.getText() + "')";
}
else if (operType.equals("edit")) {
sql = "update OwnerInfo set Name='" + txtName.getText() + "',";
sql = sql + " Gender='" + gender + "',";
sql = sql + " Birthday='" + txtBirthday.getText() + "',";
sql = sql + " WorkTime='" + txtWorkTime.getText() + "',";
sql = sql + " Org='" + txtOrg.getText() + "'";
sql = sql + " where ID='" + curID + "'";
}
if (db.executeSql(sql)) {
CommonDialog.showDialog(CommonDialog.OK, "房产管理系统", "新增房主信息成功!");
groupSetEnabled(false);
btnSetEnabled(true, true, true, false);
setOperType("none");
}
else {
CommonDialog.showDialog(CommonDialog.OK, "房产管理系统", "新增房主信息失败!");
}
}
void btnEdit_actionPerformed(ActionEvent e) {
groupSetEnabled(true);
btnSetEnabled(true, true, true, true);
setOperType("edit");
}
void btnDelete_actionPerformed(ActionEvent e) {
if (!curID.equals("")) {
int ir = CommonDialog.showDialog(3, "房产管理系统", "确定要删除当前记录吗?");
if (ir == 2) {
return;
}
String sql = "delete from OwnerInfo where ID='" + curID + "'";
if (db.executeSql(sql)) {
CommonDialog.showDialog(CommonDialog.OK, "房产管理系统", "删除记录成功!");
clearData();
groupSetEnabled(false);
btnSetEnabled(true, false, false, false);
setOperType("none");
}
else {
CommonDialog.showDialog(CommonDialog.OK, "房产管理系统", "删除记录失败,请重试!");
}
}
}
//查看家庭信息
void btnViewFamily_actionPerformed(ActionEvent e) {
//判断当前人员是否为空
if (curID.equals("")) {
CommonDialog.showDialog(CommonDialog.OK, "房产管理系统", "当前人员为空,不能查看!");
}
else {
//根据当前人员生成查询语句
String sql = "select * from FamilyInfo where OwnerID='" + curID +
"' order by ID";
try {
ResultSet rs = db.getResult(sql);
if (rs.first()) {
//若结果集不为空,生成Query类的新实例,并传入sql参数,显示家属列表
Query query = new Query(sql);
Dimension dlgSize = query.getPreferredSize();
Dimension frmSize = getSize();
Point loc = getLocation();
query.setLocation( (frmSize.width - dlgSize.width) / 2 + loc.x,
(frmSize.height - dlgSize.height) / 2 + loc.y);
query.pack();
query.show();
}
else {
//若结果集为空,说明当前人员没有家庭信息,给出提示信息
CommonDialog.showDialog(CommonDialog.OK, "房产管理系统", "当前人员没有家庭信息!");
rs.close();
}
}
catch (Exception f) {
f.printStackTrace();
}
}
}
}
class OwnerInfo_btnExit_actionAdapter
implements java.awt.event.ActionListener {
OwnerInfo adaptee;
OwnerInfo_btnExit_actionAdapter(OwnerInfo adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btnExit_actionPerformed(e);
}
}
class OwnerInfo_btnAdd_actionAdapter
implements java.awt.event.ActionListener {
OwnerInfo adaptee;
OwnerInfo_btnAdd_actionAdapter(OwnerInfo adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btnAdd_actionPerformed(e);
}
}
class OwnerInfo_btnSave_actionAdapter
implements java.awt.event.ActionListener {
OwnerInfo adaptee;
OwnerInfo_btnSave_actionAdapter(OwnerInfo adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btnSave_actionPerformed(e);
}
}
class OwnerInfo_btnEdit_actionAdapter
implements java.awt.event.ActionListener {
OwnerInfo adaptee;
OwnerInfo_btnEdit_actionAdapter(OwnerInfo adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btnEdit_actionPerformed(e);
}
}
class OwnerInfo_btnDelete_actionAdapter
implements java.awt.event.ActionListener {
OwnerInfo adaptee;
OwnerInfo_btnDelete_actionAdapter(OwnerInfo adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btnDelete_actionPerformed(e);
}
}
class OwnerInfo_btnViewFamily_actionAdapter
implements java.awt.event.ActionListener {
OwnerInfo adaptee;
OwnerInfo_btnViewFamily_actionAdapter(OwnerInfo adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btnViewFamily_actionPerformed(e);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -