📄 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 + -