📄 buildinfo.java~111~
字号:
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");
}
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();
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", "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 + "'";
System.out.println(sql);
}
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 + -