?? houseinfo.java
字號:
import javax.swing.*;
import java.awt.*;
import com.borland.jbcl.layout.*;
import java.awt.event.*;
import java.sql.*;
public class HouseInfo
extends JFrame {
XYLayout xYLayout1 = new XYLayout();
JPanel jPanel1 = new JPanel();
XYLayout xYLayout2 = new XYLayout();
JLabel jLabel4 = new JLabel();
JLabel jLabel8 = new JLabel();
JButton btnEdit = new JButton();
JTextField txtBuyPrice = new JTextField();
JComboBox cboHouseLocation = new JComboBox();
JButton btnAdd = new JButton();
JLabel jLabel1 = new JLabel();
JLabel jLabel3 = new JLabel();
JLabel jLabel7 = new JLabel();
JButton btnDelete = new JButton();
JComboBox cboUseType = new JComboBox();
JTextField txtArea = new JTextField();
JTextField txtRoomNum = new JTextField();
JButton btnSave = new JButton();
JLabel jLabel2 = new JLabel();
JComboBox cboHeading = new JComboBox();
JLabel jLabel6 = new JLabel();
JTextField txtHouseName = new JTextField();
JButton btnExit = new JButton();
JTextField txtBuyTime = new JTextField();
JTextField txtYTArea = new JTextField();
JLabel jLabel9 = new JLabel();
JLabel jLabel5 = new JLabel();
JTextField txtOwner = new JTextField();
JLabel jLabel10 = new JLabel();
//生成數據庫操作類新實例,用于數據庫操作
DBManager db = new DBManager();
//當前操作ID
String curID = new String();
//操作類型
String operType = new String();
public HouseInfo() {
try {
jbInit();
initForm();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
public HouseInfo(String ID) {
try {
jbInit();
setCurID(ID);
initForm();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
void jbInit() throws Exception {
this.getContentPane().setLayout(xYLayout1);
xYLayout1.setWidth(467);
xYLayout1.setHeight(323);
jPanel1.setBorder(BorderFactory.createEtchedBorder());
jPanel1.setLayout(xYLayout2);
jLabel4.setFont(new java.awt.Font("宋體", 0, 12));
jLabel4.setText("購房時間:");
jLabel8.setText("房屋坐落:");
jLabel8.setFont(new java.awt.Font("宋體", 0, 12));
btnEdit.setText("修改");
btnEdit.addActionListener(new HouseInfo_btnEdit_actionAdapter(this));
btnEdit.setFont(new java.awt.Font("宋體", 0, 12));
txtBuyPrice.setText("");
txtBuyPrice.setFont(new java.awt.Font("宋體", 0, 12));
cboHouseLocation.setFont(new java.awt.Font("宋體", 0, 12));
cboHouseLocation.setEditable(false);
btnAdd.setFont(new java.awt.Font("宋體", 0, 12));
btnAdd.setText("新增");
btnAdd.addActionListener(new HouseInfo_btnAdd_actionAdapter(this));
jLabel1.setFont(new java.awt.Font("宋體", 0, 12));
jLabel1.setText("房間號:");
jLabel3.setFont(new java.awt.Font("宋體", 0, 12));
jLabel3.setText("購房價格:");
jLabel7.setFont(new java.awt.Font("宋體", 0, 12));
jLabel7.setText("陽臺面積:");
btnDelete.setText("刪除");
btnDelete.addActionListener(new HouseInfo_btnDelete_actionAdapter(this));
btnDelete.setFont(new java.awt.Font("宋體", 0, 12));
cboUseType.setFont(new java.awt.Font("宋體", 0, 12));
txtArea.setText("");
txtArea.addActionListener(new HouseInfo_txtArea_actionAdapter(this));
txtArea.setFont(new java.awt.Font("宋體", 0, 12));
txtRoomNum.setText("");
txtRoomNum.setFont(new java.awt.Font("宋體", 0, 12));
btnSave.setText("保存");
btnSave.addActionListener(new HouseInfo_btnSave_actionAdapter(this));
btnSave.setFont(new java.awt.Font("宋體", 0, 12));
jLabel2.setText("間數:");
jLabel2.setFont(new java.awt.Font("宋體", 0, 12));
cboHeading.setFont(new java.awt.Font("宋體", 0, 12));
cboHeading.setEditable(false);
jLabel6.setText("建筑面積:");
jLabel6.setFont(new java.awt.Font("宋體", 0, 12));
txtHouseName.setFont(new java.awt.Font("宋體", 0, 12));
txtHouseName.setText("");
btnExit.setText("退出");
btnExit.addActionListener(new HouseInfo_btnExit_actionAdapter(this));
btnExit.setFont(new java.awt.Font("宋體", 0, 12));
txtBuyTime.setFont(new java.awt.Font("宋體", 0, 12));
txtBuyTime.setText("");
txtYTArea.setFont(new java.awt.Font("宋體", 0, 12));
txtYTArea.setText("");
jLabel9.setText("朝向:");
jLabel9.setFont(new java.awt.Font("宋體", 0, 12));
jLabel5.setText("使用方式:");
jLabel5.setFont(new java.awt.Font("宋體", 0, 12));
txtOwner.setFont(new java.awt.Font("宋體", 0, 12));
txtOwner.setText("");
jLabel10.setFont(new java.awt.Font("宋體", 0, 12));
jLabel10.setText("住戶:");
this.setTitle("住房信息維護");
jPanel1.add(txtRoomNum, new XYConstraints(76, 89, 112, 24));
jPanel1.add(cboHouseLocation, new XYConstraints(76, 12, 332, -1));
jPanel1.add(jLabel1, new XYConstraints(13, 57, 68, -1));
jPanel1.add(txtHouseName, new XYConstraints(76, 51, 111, 24));
jPanel1.add(txtBuyTime, new XYConstraints(76, 128, 112, 24));
jPanel1.add(jLabel4, new XYConstraints(13, 133, 68, -1));
jPanel1.add(jLabel6, new XYConstraints(13, 172, 68, -1));
jPanel1.add(txtArea, new XYConstraints(76, 166, 112, 24));
jPanel1.add(jLabel7, new XYConstraints(205, 169, 68, -1));
jPanel1.add(jLabel3, new XYConstraints(205, 127, 68, -1));
jPanel1.add(jLabel5, new XYConstraints(205, 92, 68, -1));
jPanel1.add(jLabel9, new XYConstraints(205, 54, 68, -1));
jPanel1.add(cboHeading, new XYConstraints(282, 49, 126, -1));
jPanel1.add(cboUseType, new XYConstraints(282, 89, 126, -1));
jPanel1.add(txtBuyPrice, new XYConstraints(282, 124, 126, 24));
jPanel1.add(txtYTArea, new XYConstraints(282, 164, 126, 24));
jPanel1.add(jLabel2, new XYConstraints(13, 92, 53, -1));
jPanel1.add(jLabel8, new XYConstraints(13, 20, 68, -1));
jPanel1.add(btnSave, new XYConstraints(278, 240, 61, 27));
jPanel1.add(btnAdd, new XYConstraints(10, 241, 61, 27));
jPanel1.add(btnEdit, new XYConstraints(80, 241, 61, 27));
jPanel1.add(btnDelete, new XYConstraints(152, 241, 61, 27));
jPanel1.add(btnExit, new XYConstraints(347, 240, 61, 27));
jPanel1.add(jLabel10, new XYConstraints(14, 211, 44, -1));
jPanel1.add(txtOwner, new XYConstraints(76, 206, 112, 24));
this.getContentPane().add(jPanel1, new XYConstraints(18, 17, 430, 287));
}
void txtArea_actionPerformed(ActionEvent e) {
}
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);
fullBuildInfo();
setOperType("none");
db.fullCombo(cboHeading, "Heading");
db.fullCombo(cboUseType, "UseType");
//若ID不為空,則填充當前房產信息,并將【修改】和【刪除】設為可用
if (!curID.equals("")) {
fullInfo(curID);
btnSetEnabled(true, true, true, false);
}
else {
btnSetEnabled(true, false, false, false);
}
}
// 根據住房ID填充住房信息:
void fullInfo(String ID) {
//得到查詢語句
String sql = "select * from HouseInfo where ID='" + ID + "'";
try {
//得到結果集
ResultSet rs = db.getResult(sql);
if (rs.first()) {
//填充信息項內容
txtArea.setText(rs.getString("Area")); //面積
txtBuyPrice.setText(rs.getString("BuyPrice")); //購房價格
txtRoomNum.setText(rs.getString("Number")); //間數
txtBuyTime.setText(rs.getString("BuyTime")); //購房時間
txtHouseName.setText(rs.getString("Name")); //房間號
txtYTArea.setText(rs.getString("YTArea")); //陽臺面積
//調用getByField函數根據房產ID得到房產坐落,再設置房產坐落列表選擇項
cboHouseLocation.setSelectedItem(db.getByField("BuildInfo", "Location",
"ID='" + rs.getString("BuildID")+"'"));
cboHeading.setSelectedIndex(Integer.parseInt(rs.getString("Heading"))-1); //朝向
cboUseType.setSelectedIndex(Integer.parseInt(rs.getString("UseType"))-1); //使用方式
//調用getByField函數根據住戶ID得到住戶姓名,再設置住戶輸入框文本
txtOwner.setText(db.getByField("OwnerInfo", "Name","ID='" + rs.getString("OwnerID")+"'"));
}
else {
rs.close();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
//加載房產坐落信息
void fullBuildInfo() {
//sql語句中加distinct限制,以避免重復
String sql = "select distinct Location from BuildInfo ";
String content = new String();
try {
//得到結果集
ResultSet rs = db.getResult(sql);
if (rs.first()) {
//循環加載結果
rs.beforeFirst();
while (rs.next()) {
content = rs.getString("Location");
cboHouseLocation.addItem(content);
}
}
else {
rs.close();
}
//設置選中項為空
cboHouseLocation.setSelectedItem(null);
}
catch (Exception e) {
e.printStackTrace();
}
}
//根據坐落得到房產ID
//住房信息表中房產信息項存儲的是房產ID,所以在保存時首先要將房產坐落轉化為房產ID
String getLocationID(String location) {
String sql = "select * from BuildInfo where location='" + location + "'";
String ID = new String();
try {
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -