?? buildinfo.java
字號:
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();
//當前操作ID
String curID = new String();
//操作類型
String operType = new String();
//構造函數
public BuildInfo() {
try {
jbInit();
//初始化窗口相關數據
initForm();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
//傳入參數ID
public BuildInfo(String ID) {
try {
jbInit();
//設置當前操作ID
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 setCurID(String ID) {
curID = ID;
}
void setOperType(String type) {
operType = type;
}
//初始化窗口數據
void initForm() {
//設置所有輸入框為不可編輯
groupSetEnabled(false);
//調用fullCombo函數加載房屋類型和房屋用途列表
db.fullCombo(cboHouseType, "HouseType");
db.fullCombo(cboHouseUse, "HouseUsage");
//設置操作類行為none
setOperType("none");
//若ID不為空,則填充當前房產信息,并將【修改】和【刪除】設為可用
if (!curID.equals("")) {
fullInfo(curID);
btnSetEnabled(true, true, true, false);
}
else {
//若ID為空,設置【新增】可用,其他不可用
btnSetEnabled(true, false, false, false);
}
}
//根據房產ID填充房產信息
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 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(""); //設置當前操作ID為空
setOperType("add"); //設置操作類型為add
}
//編輯按鈕響應事件
void btnEdit_actionPerformed(ActionEvent e) {
//設置輸入框和按鈕的可用性
groupSetEnabled(true);
btnSetEnabled(true, true, true, true);
setOperType("edit"); //設置操作類型為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, "房產管理系統", "刪除記錄失敗,請重試!");
}
}
}
//保存按鈕響應事件
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();
//房屋類型和用途在BuildInfo中以代碼的形式保存,所以要調用CodeDesConvert函數根據其代碼描述得到其代碼值
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
id = db.getID("BuildInfo", "ID");
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 + " Time='" + 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 btnExit_actionPerformed(ActionEvent e) {
//進行新增或修改操作時,確定是否退出
if (!operType.equals("none")) {
int ir = CommonDialog.showDialog(3, "房產管理系統", "當前操作尚未保存,確定要退出嗎?");
if (ir == 2) {
return;
}
}
this.dispose();
}
}
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);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -