import javax.swing.*;
import java.awt.*;
import com.borland.jbcl.layout.*;
import java.awt.event.*;
import java.sql.*;
public class BuildInfo
extends JFrame {
XYLayout xYLayout1 = new XYLayout();
JPanel jPanel1 = new JPanel();
XYLayout xYLayout2 = new XYLayout();
JLabel jLabel4 = new JLabel();
JButton btnEdit = new JButton();
JButton btnAdd = new JButton();
JLabel jLabel1 = new JLabel();
JLabel jLabel3 = new JLabel();
JButton btnDelete = new JButton();
JButton btnSave = new JButton();
JLabel jLabel2 = new JLabel();
JTextField txtLocation = new JTextField();
JTextField txtUseArea = new JTextField();
JLabel jLabel7 = new JLabel();
JComboBox cboHouseType = new JComboBox();
JTextField txtArea = new JTextField();
JTextField txtBuildTime = new JTextField();
JLabel jLabel6 = new JLabel();
JButton btnExit = new JButton();
JTextField txtHouseNum = new JTextField();
JComboBox cboHouseUse = new JComboBox();
JLabel jLabel5 = new JLabel();
DBManager db = new DBManager(); //数据库操作
String operType = new String(); //操作类型
String curID = new String();
public BuildInfo() {
try {
jbInit();
initForm();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
public BuildInfo(String ID) {
try {
jbInit();
setCurID(ID);
initForm();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
void jbInit() throws Exception {
this.getContentPane().setLayout(xYLayout1);
xYLayout1.setWidth(436);
xYLayout1.setHeight(255);
jPanel1.setBorder(BorderFactory.createEtchedBorder());
jPanel1.setLayout(xYLayout2);
jLabel4.setFont(new java.awt.Font("宋体", 0, 12));
jLabel4.setText("房屋套数:");
btnEdit.setText("修改");
btnEdit.addActionListener(new BuildInfo_btnEdit_actionAdapter(this));
btnEdit.setFont(new java.awt.Font("宋体", 0, 12));
btnAdd.setFont(new java.awt.Font("宋体", 0, 12));
btnAdd.setText("新增");
btnAdd.addActionListener(new BuildInfo_btnAdd_actionAdapter(this));
jLabel1.setFont(new java.awt.Font("宋体", 0, 12));
jLabel1.setText("坐落位置:");
jLabel3.setFont(new java.awt.Font("宋体", 0, 12));
jLabel3.setText("房屋用途:");
btnDelete.setText("删除");
btnDelete.addActionListener(new BuildInfo_btnDelete_actionAdapter(this));
btnDelete.setFont(new java.awt.Font("宋体", 0, 12));
btnSave.setText("保存");
btnSave.addActionListener(new BuildInfo_btnSave_actionAdapter(this));
btnSave.setFont(new java.awt.Font("宋体", 0, 12));
jLabel2.setText("建成年代:");
jLabel2.setFont(new java.awt.Font("宋体", 0, 12));
txtLocation.setFont(new java.awt.Font("宋体", 0, 12));
txtLocation.setText("");
txtUseArea.setFont(new java.awt.Font("宋体", 0, 12));
txtUseArea.setText("");
jLabel7.setFont(new java.awt.Font("宋体", 0, 12));
jLabel7.setText("使用面积:");
cboHouseType.setFont(new java.awt.Font("宋体", 0, 12));
cboHouseType.setDoubleBuffered(false);
txtArea.setText("");
txtArea.setFont(new java.awt.Font("宋体", 0, 12));
txtBuildTime.setText("");
txtBuildTime.setFont(new java.awt.Font("宋体", 0, 12));
jLabel6.setText("占地面积:");
jLabel6.setFont(new java.awt.Font("宋体", 0, 12));
btnExit.setText("退出");
btnExit.addActionListener(new BuildInfo_btnExit_actionAdapter(this));
btnExit.setFont(new java.awt.Font("宋体", 0, 12));
txtHouseNum.setFont(new java.awt.Font("宋体", 0, 12));
txtHouseNum.setText("");
cboHouseUse.setFont(new java.awt.Font("宋体", 0, 12));
cboHouseUse.setEditable(false);
jLabel5.setText("房屋类型:");
jLabel5.setFont(new java.awt.Font("宋体", 0, 12));
this.setTitle("房产信息维护");
this.getContentPane().add(jPanel1, new XYConstraints(17, 16, 403, 222));
jPanel1.add(txtLocation, new XYConstraints(77, 12, 311, 24));
jPanel1.add(jLabel2, new XYConstraints(11, 60, 68, -1));
jPanel1.add(txtBuildTime, new XYConstraints(77, 55, 112, 24));
jPanel1.add(cboHouseUse, new XYConstraints(269, 55, 112, -1));
jPanel1.add(jLabel1, new XYConstraints(11, 17, 68, -1));
jPanel1.add(jLabel3, new XYConstraints(196, 56, 68, -1));
jPanel1.add(jLabel4, new XYConstraints(11, 97, 68, -1));
jPanel1.add(txtHouseNum, new XYConstraints(77, 94, 112, 24));
jPanel1.add(jLabel6, new XYConstraints(11, 134, 68, -1));
jPanel1.add(txtArea, new XYConstraints(77, 133, 112, 24));
jPanel1.add(jLabel5, new XYConstraints(196, 97, 68, -1));
jPanel1.add(jLabel7, new XYConstraints(196, 135, 68, -1));
jPanel1.add(cboHouseType, new XYConstraints(269, 93, 112, -1));
jPanel1.add(txtUseArea, new XYConstraints(269, 129, 112, 24));
jPanel1.add(btnEdit, new XYConstraints(88, 175, 61, 27));
jPanel1.add(btnDelete, new XYConstraints(156, 175, 61, 27));
jPanel1.add(btnAdd, new XYConstraints(20, 175, 61, 27));
jPanel1.add(btnSave, new XYConstraints(255, 175, 61, 27));
jPanel1.add(btnExit, new XYConstraints(320, 175, 61, 27));
}
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 initForm() {
groupSetEnabled(false);
db.fullCombo(cboHouseType, "HouseType");
db.fullCombo(cboHouseUse, "HouseUsage");
setOperType("none");
//若ID不为空,则填充当前房产信息,并将【修改】和【删除】设为可用
if (!curID.equals("")) {
fullInfo(curID);
btnSetEnabled(true, true, true, false);
}
else {
btnSetEnabled(true, false, false, false);
}
}
void fullInfo(String ID) {
String sql = "select * from BuildInfo where ID='" + ID + "'";
try {
ResultSet rs = db.getResult(sql);
if (rs.first()) {
txtArea.setText(rs.getString("Area"));
txtBuildTime.setText(rs.getString("Time"));
txtHouseNum.setText(rs.getString("Number"));
txtLocation.setText(rs.getString("Location"));
txtUseArea.setText(rs.getString("UseArea"));
cboHouseType.setSelectedIndex(Integer.parseInt(rs.getString("Type"))-1);
cboHouseUse.setSelectedIndex(Integer.parseInt(rs.getString("Usage"))-1);
}
else {
rs.close();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
void groupSetEnabled(boolean enabled) {
txtArea.setEnabled(enabled);
txtBuildTime.setEnabled(enabled);
txtHouseNum.setEnabled(enabled);
txtLocation.setEnabled(enabled);
txtUseArea.setEnabled(enabled);
cboHouseType.setEnabled(enabled);
cboHouseUse.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() {
txtArea.setText("");
txtBuildTime.setText("");
txtHouseNum.setText("");
txtLocation.setText("");
txtUseArea.setText("");
cboHouseType.setSelectedItem(null);
cboHouseUse.setSelectedItem(null);
}
boolean checkData() {
if (txtLocation.getText().trim().equals("")) {
CommonDialog.showDialog(CommonDialog.OK, "房产管理系统", "坐落位置不能为空!");
txtLocation.setFocusable(true);
return false;
}
return true;
}
void btnSave_actionPerformed(ActionEvent e) {
if (!checkData()) {
return;
}
String sql = new String();
String id = new String();
String houseType = new String();
String houseUse = new String();
String houseNum = new String();
String area = new String();
String useArea = new String();
if (cboHouseType.getSelectedIndex() >= 0) {
houseType = db.CodeDesConvert("HouseType", "",
cboHouseType.getSelectedItem().toString(),
0);
}
if (cboHouseUse.getSelectedIndex() >= 0) {
houseUse = db.CodeDesConvert("HouseUsage", "",
cboHouseUse.getSelectedItem().toString(), 0);
}
if (!txtHouseNum.getText().equals("")) {
houseNum = txtHouseNum.getText().trim();
}
if (!txtArea.getText().equals("")) {
area = txtArea.getText().trim();
}
if (!txtUseArea.getText().equals("")) {
useArea = txtUseArea.getText().trim();
}
if (operType.equals("add")) {
id = db.getID("BuildInfo");
setCurID(id);
sql = "insert into BuildInfo values('" + id + "','" +
txtLocation.getText() + "','" + txtBuildTime.getText() +
"','" + houseUse + "','" + houseNum + "','" + houseType + "','" +
area + "','" + useArea + "')";
}
else if (operType.equals("edit")) {
sql = "update BuildInfo set Location='" + txtLocation.getText() + "',";
sql = sql + " BuildTime='" + txtBuildTime.getText() + "',";
sql = sql + " Usage='" + houseUse + "',";
sql = sql + " Number='" + houseNum + "',";
sql = sql + " Type='" + houseType + "',";
sql = sql + " Area='" + area + "',";
sql = sql + " UseArea='" + useArea + "'";
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 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 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 BuildInfo 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, "房产管理系统", "删除记录失败,请重试!");
}
}
}
}
class BuildInfo_btnExit_actionAdapter
implements java.awt.event.ActionListener {
BuildInfo adaptee;
BuildInfo_btnExit_actionAdapter(BuildInfo adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btnExit_actionPerformed(e);
}
}
class BuildInfo_btnAdd_actionAdapter
implements java.awt.event.ActionListener {
BuildInfo adaptee;
BuildInfo_btnAdd_actionAdapter(BuildInfo adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btnAdd_actionPerformed(e);
}
}
class BuildInfo_btnSave_actionAdapter
implements java.awt.event.ActionListener {
BuildInfo adaptee;
BuildInfo_btnSave_actionAdapter(BuildInfo adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btnSave_actionPerformed(e);
}
}
class BuildInfo_btnEdit_actionAdapter
implements java.awt.event.ActionListener {
BuildInfo adaptee;
BuildInfo_btnEdit_actionAdapter(BuildInfo adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btnEdit_actionPerformed(e);
}
}
class BuildInfo_btnDelete_actionAdapter
implements java.awt.event.ActionListener {
BuildInfo adaptee;
BuildInfo_btnDelete_actionAdapter(BuildInfo adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btnDelete_actionPerformed(e);
}
}