java实现信息管理系统之宿舍水电管理
实验要求
宿舍水电管理系统
1,宿舍水电基本信息管理(增删改查)
2,水电抄表管理(增删改查)
3,水电收费管理(增删改查)
4,考虑管理员、住户两种用户,要求有登录功能
实验六:
Java 面向对象及GUI 程序设计(2)第 8~9 周,每周提交报告与程序实验目的
1,掌握面向对象的思想,用面向对象的思维分析设计 Java 程序;
2,掌握Java 类的设计方法,对象的构建和使用
3,掌握Java 的接口、继承与多态
4,掌握Swing 常用组件
5,掌握Java 文件读写方法
成果展示
1,只实现了管理员的增删改查(但其他类似)
2,删除后刷新表格数据不变或者重复加表格最后一个但文件已经删除指定信息
3,背景图片随窗口大小变化没有实现成功
需求分析
实验要求的系统不大明确,所以按基本常识简单分析一下系统actor:管理员和住户
管理员
超级管理员(Admin):
可以对管理员和住户管理,可以对水电费账单管理(这个实验里没实现管理员权限的限制但很简单就是要获取当前用户然后把当前用户传入要进入的UI(界面)里,就可以对其进行操作限制)
一般管理员:
只能管理住户
住户(Household):
可以查看修改自己信息,可以充值缴费
水电费管理和抄表管理我简单合到一起,就是水电费账单,管理员在抄表信息录入后可以勾选是否缴费。
账单(Bill);
管理员可对其增删改查,用户只能查看
以上只是简单的分析,肯定不合理,这也导致我在后面编码时老是重构代码。
系统架构
系统架构在最初的文本界面和AWT界面里我没有这个概念所以代码写的很乱,这次实验老师明确要求要分层故特地查了系统架构方面的知识和复习了C#实习的项目
一篇博客写得比较简洁易懂
在C#实习里实习老师给我们介绍的是5层架构模式:
UI——>Bll——>Dal——>DataBase(——>Model对model操作)
所以我借助demo架构我的系统
简单说一下个人对这个架构模式的理解,(可能不对@~@)
UI(user interface) 就是界面,充当系统和用户的交互接口(它把用户的请求发送给系统,系统响应请求后通过UI给用户一个答复)
model 是对你要操作的对象的一个描述/一个封装,里面包含有这个对象的属性和set/get方法(属性是private的别的类访问不了因此要这些方法【个人理解】)
DAL (data access level)数据访问层,访问database里的东西。
BLL (business logic level)业务逻辑层 实现业务逻辑的,比如用户登陆,UserBll里就有一个方法UserBLL.login()供UI调用
以我的系统来分析:
创建各个层,数据库用文本文件代替(为了代码的简洁及重复调用要建一个类Filehelper1专门操作文件的读写创建删除操作)
数据操作,我的大概思路是这样:
在dal里建立一个ArrayList ,把文件读出来放入里面,删除文件,建一个相同路径的文件,遍历数组对数据做你想要的操作后再全部放入文件中。
文件操作选择对象流操作,把一个对象序列化后放入文件。
下面是对象流读取文件的类(测试没采用单元测试简单的加main函数测试) 我是在网上查了两种方法组合调试编写成自己的FileFelper1,一个是百度知道另一个是具体忘了就不写链接出来了。
package tsc.s6.SuSheShuiDianGuanLi.dal;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import tsc.s6.SuSheShuiDianGuanLi.model.Admin;
import tsc.s6.SuSheShuiDianGuanLi.model.Bill;
import tsc.s6.SuSheShuiDianGuanLi.model.Household;
/**
* @author 唐树成
*
*/
public class FileHelper1 {
public FileHelper1() {
}
public static void writeObjec(Object obj,String path) throws IOException{
OutputStream os = null;
MyObjectOutputStream moos = null;
//File f = new File(path);
try {
File f = new File(path);//便于捕获异常
os = new FileOutputStream(path,true);
moos = MyObjectOutputStream.newInstance(f, os);
moos.writeObject(obj);
os.flush();
moos.flush();
}catch (IOException e) {
e.printStackTrace();
}finally {
os.close();
moos.close();
}
}
public static ArrayList<Object> readObject(String path)throws Exception{
ObjectInputStream ois = null;
ArrayList<Object> objectList = new ArrayList<Object>();
FileInputStream fis = new FileInputStream(path);
try {
ois = new ObjectInputStream(new BufferedInputStream(fis));
while(true) {
objectList.add(ois.readObject());
}
}catch (Exception e) {
System.out.println("Exception thrown :" + e+"(对象流方式访问文件结束抛出的异常)");
//e.printStackTrace();
}finally {
ois.close();
fis.close();
}
return objectList;
}
/**
* 此类继承ObjectOutputStream,重写writeStreamHeader()方法,以实现追加写入时去掉头部信息
*/
public static class MyObjectOutputStream extends ObjectOutputStream {
private static File f;
// writeStreamHeader()方法是在ObjectOutputStream的构造方法里调用的
// 由于覆盖后的writeStreamHeader()方法用到了f。如果直接用此构造方法创建
// 一个MyObjectOutputStream对象,那么writeStreamHeader()中的f是空指针
// 因为f还没有初始化。所以这里采用单态模式
private MyObjectOutputStream(OutputStream out, File f) throws IOException, SecurityException {
super(out);
}
// 返回一个MyObjectOutputStream对象,这里保证了new MyObjectOutputStream(out, f)
// 之前f已经指向一个File对象
public static MyObjectOutputStream newInstance(File file, OutputStream out) throws IOException {
f = file;// 本方法最重要的地方:构建文件对象,两个引用指向同一个文件对象
return new MyObjectOutputStream(out, f);
}
@Override
protected void writeStreamHeader() throws IOException {
// 文件不存在或文件为空,此时是第一次写入文件,所以要把头部信息写入。
if (!f.exists() || (f.exists() && f.length() == 0)) {
super.writeStreamHeader();
} else {
// 不需要做任何事情
}
}
}
public static boolean deleteFile(String path) {
boolean f = false;
try {
File file = new File(path);
if(file.isFile()&&file.exists()) {
f = file.delete();
}
}catch(Exception e1) {
e1.printStackTrace();
}
return f;
}
public static boolean createFile(String path) {
boolean f = false;
File file = new File(path);//删除全部管理后新建一个空文件
if(!file.exists()) {
file.getParentFile().mkdirs();
}
try {
f = file.createNewFile();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
return f;
}
/* public static void main(String[] args) throws Exception {
String path = "D:\\Eclipse\\新建 好压 7Z 压缩文件\\eclipse\\workspace\\P2016112447唐树成S6\\src\\data\\household.txt";
Household hd = new Household("hd064026","064026","T064026","唐树成",10);
ArrayList<Object> hdList = FileHelper1.readObject(path);
//FileHelper1.writeObjec(hd,path);
for(int i = 0;i < hdList.size(); i ++){
System.out.println(((Household)hdList.get(i)).getName());
System.out.println(((Household)hdList.get(i)).getAccount());
System.out.println(((Household)hdList.get(i)).getPassword());
System.out.println(((Household)hdList.get(i)).getRoomNum());
System.out.println(((Household)hdList.get(i)).getBancle());
}
}
*/
/* public static void main(String[] args) throws Exception {
Admin adm1 = new Admin("tsc","tsc123", "123456", "0");
String path = "D:\\Eclipse\\新建 好压 7Z 压缩文件\\eclipse\\workspace\\P2016112447唐树成S6\\src\\data\\admin.txt";
FileHelper1.writeObjec(adm1,path);
ArrayList<Object> adminList = FileHelper1.readObject(path);
System.out.println(adminList.size());
for(int i = 0;i < adminList.size(); i ++){
System.out.println(((Admin)adminList.get(i)).getName());
System.out.println(((Admin)adminList.get(i)).getAccountNum());
System.out.println(((Admin)adminList.get(i)).getPassword());
System.out.println(((Admin)adminList.get(i)).getType());
}
}
*/
//没问题应该是BillDal有问题
/* public static void main(String[] args) throws Exception {
String path = "D:\\Eclipse\\新建 好压 7Z 压缩文件\\eclipse\\workspace\\P2016112447唐树成S6\\src\\data\\bill.txt";
Bill bill = new Bill("2018-10-29-0","T064026","唐树成",0.5,10,1.5,10);
FileHelper1.writeObjec(bill,path);
ArrayList<Object> billList = FileHelper1.readObject(path);
System.out.println(billList.size());
for(int i = 0;i < billList.size(); i ++){
System.out.println(((Bill)billList.get(i)).getWater());
System.out.println(((Bill)billList.get(i)).getID());
System.out.println(((Bill)billList.get(i)).getDate());
System.out.println(((Bill)billList.get(i)).getTotalPrice());
System.out.println(((Bill)billList.get(i)).getReadMeterName());
}
}
*/
}
编码测试
Admin:
package tsc.s6.SuSheShuiDianGuanLi.model;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Admin implements Serializable{
private static final long serialVersionUID = 1L;
private String name;//管理姓名
private String accountNum;//管理账号
private String password;
private String type;//管理员级别0:最高级可以管理所有;1:只能管理用户
private String registerDate;//注册时间
public Admin() {
super();
}
public boolean equals(Admin adm) {
return this.getName()==adm.getName()&&this.getRegisterDate()==adm.getRegisterDate()
&&this.getAccountNum()==adm.getAccountNum()&&this.getPassword()==adm.getPassword()
&&this.getType()==adm.getType();
}
public Admin(String name,String accNum,String pw,String type) {
super();
this.name = name;
this.accountNum = accNum;
this.password = pw;
this.type = type;
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd ");//设置日期格式
this.registerDate = (String)df.format(new Date());
}
public boolean setRegisterDate() {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd ");//设置日期格式
this.registerDate = (String)df.format(new Date());
return true;
}
public String getRegisterDate() {
return registerDate;
}
public boolean setName(String n) {
this.name = n;
return true;
}
public String getName() {
return name;
}
public String getAccountNum() {
return accountNum;
}
public boolean setAccountNum(String an) {
this.accountNum = an;
return true;
}
public String getPassword() {
return password;
}
public boolean setPassword(String pw) {
this.password = pw;
return true;
}
public String getType() {
return type;
}
public boolean setType(String t) {
this.type = t;
return true;
}
}
package tsc.s6.SuSheShuiDianGuanLi.dal;
import java.io.IOException;
import java.util.ArrayList;
import tsc.s6.SuSheShuiDianGuanLi.model.Admin;
public class AdminDal {
static String path = "D:\\Eclipse\\新建 好压 7Z 压缩文件\\eclipse\\workspace\\P2016112447唐树成S6\\src\\data\\admin.txt";
static ArrayList<Object> objectList;
public AdminDal() {
}
public boolean addAdmin(Admin admin) {
try {
objectList = FileHelper1.readObject(path);
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
boolean isExist = false;
for(int i = 0;i < objectList.size();i++ ) {//检查是否有此人
if(admin.getAccountNum().equals(((Admin)objectList.get(i)).getAccountNum())){
//(admin.equals(((Admin)objectList.get(i))))
isExist = true;
}
}
if(isExist == true) {
return false;//存在,添加失败
}
else {
try {
FileHelper1.writeObjec(admin,path);
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
return true;
}
}
public ArrayList<Object> getAllAdmin(){
try {
objectList = FileHelper1.readObject(path);
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
return objectList;
}
public boolean deleteAdmin(String account) {
try {
objectList = FileHelper1.readObject(path);
//System.out.println(objectList.size());//调试需要
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
boolean isExist = false;
int j =0;
for(int i = 0;i < objectList.size();i++ ) {//检查是否有此人
if(account.equals(((Admin)objectList.get(i)).getAccountNum())){
//(admin.equals(((Admin)objectList.get(i))))
isExist = true;
j = i;
}
}
if(isExist == true) {
//此处不能将admin传入要传入下表
//System.out.println(objectList.size());//bug:没有移除admin说明FileHelper.deleteFile(path)没错
objectList.remove(j);
FileHelper1.deleteFile(path);// 删除admin.txt文件
/*不用新建admin.txt文件FileHelper.writeObjec()会自动建
但防止删除的是最后一个导致不调动该方法从而导致系统读取文件找不到文件而出现异常停止所以
下面要对数组检查一下长度如果为零则写入超级管理员这样在超级管理员误删自己后也能登陆系统
事实上新建文件该在FileHelper.writeObjec()里建并写入null这样代码更加简洁,这也是我在写其他dal时才考虑到的,但我不想改了@_@
*/
if(objectList.size()==0) {
FileHelper1.createFile(path);
try {//向文件写入超级管理员
FileHelper1.writeObjec(new Admin("superAdm","888888","666666","0"),path);
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
return true;
}
else {
for(int i = 0;i < objectList.size();i++ ) {
try {
FileHelper1.writeObjec(objectList.get(i),path);
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
return true;
}
}
else {//没有此人删除失败
//System.out.println("没有此人");
return false;
}
}
public boolean updateAdmin(String account,Admin admin2) {//admin1修改前;admin2修改后
if(deleteAdmin(account))
if(addAdmin(admin2))
return true;//删除并且添加成功
else
return false;//添加失败
else
return false;//删除失败
}
public Admin searchAdmin(String account) {
try {
objectList = FileHelper1.readObject(path);
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
boolean isExist = false;
int j = 0;
for(int i = 0;i < objectList.size();i++ ) {//检查是否有此人
if((((Admin)(objectList.get(i))).getAccountNum().equals(account))){
isExist = true;
j = i;
}
}
if(isExist == false) {//没有此人查找失败
return null;
}
else {
return (Admin)objectList.get(j);
}
}
public Admin searchAdmin(String account,String password) {
try {
objectList = FileHelper1.readObject(path);
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
boolean isExist = false;
int j = 0;
for(int i = 0;i < objectList.size();i++ ) {//检查是否有此人
if((((Admin)(objectList.get(i))).getAccountNum().equals(account))&&
(((Admin)(objectList.get(i))).getPassword().equals(password))){
isExist = true;
j = i;
}
}
if(isExist == false) {//没有此人查找失败
return null;
}
else {
return (Admin)objectList.get(j);
}
}
/*测试 public static void main(String[] args) {
AdminDal adal = new AdminDal();
Admin adm1,adm2,adm3,adm;
adm1 = new Admin("jack","0000","0000","1");adm2 = new Admin("Mike","123456","123456","0");
adm3 = new Admin("Tim","adm001","aaaaa","1");
//添加测试
if(adal.addAdmin(adm1))//正确无误
System.out.println(adm1.getName()+"添加成功");
else
System.out.println(adm1.getAccountNum()+"已被注册");
adal.addAdmin(adm2);
adal.addAdmin(adm3);
//账号查询测试
adm = adal.getAdminByAccount(adm1.getAccountNum());//正确无误
if(adm!=null) {
System.out.println(adm.getName()+"查询成功");
System.out.println(adm.getAccountNum());
System.out.println(adm.getPassWord());
System.out.println(adm.getType());
System.out.println(adm.getRegisterDate());
}
//查询全部测试
ArrayList<Object> obList = adal.getAllAdmin();//正确无误
for(int i = 0;i<obList.size();i++) {
System.out.println(((Admin)obList.get(i)).getName());
System.out.println(((Admin)obList.get(i)).getAccountNum());
System.out.println(((Admin)obList.get(i)).getPassWord());
System.out.println(((Admin)obList.get(i)).getType());
System.out.println(((Admin)obList.get(i)).getRegisterDate());
}
//删除测试
if(adal.deleteAdmin(adm3.getAccountNum()))//
System.out.println(adm3.getName()+"删除成功");
else
System.out.println(adm3.getName()+"删除失败");
//Admin.equals()重写测试正确
System.out.println("jack与自己比较:"+adm1.equals(adm1));
System.out.println("jack与Mike比较:"+adm1.equals(adm2));
//修改测试
if(adal.updateAdmin(adm1.getAccountNum(), adm3))
System.out.println(adm1.getName()+"修改成功为"+adm2.getName());
else
System.out.println("修改失败");
}
*/
/*public static void main(String[] args) {
AdminDal adal = new AdminDal();
Admin adm = new Admin("name","account","password","type");
//adm = searchAdminP("123456");
System.out.println(adm.getPassword());
System.out.println(adm.getName());
System.out.println(adm.getRegisterDate());
System.out.println(adm.getType());
}
*/
}
package tsc.s6.SuSheShuiDianGuanLi.bll;
import java.util.ArrayList;
import tsc.s6.SuSheShuiDianGuanLi.dal.AdminDal;
import tsc.s6.SuSheShuiDianGuanLi.model.Admin;
public class AdminBll {
public AdminBll() {
}
private AdminDal dal = new AdminDal();
public boolean addAdmin(Admin admin) {
return dal.addAdmin(admin);
}
public boolean deleteAdmin(String account) {
return dal.deleteAdmin(account);
}
public boolean updateAdmin(String account,Admin admin2) {//admin1修改前;admin2修改后
return dal.updateAdmin(account, admin2);
}
public ArrayList<Object> getAllAdmin(){
return dal.getAllAdmin();
}
public Admin getAdminByAccount(String account) {
return dal.searchAdmin(account);
}
public Admin admLogin(String account,String passWord) {
return dal.searchAdmin(account, passWord);
}
}
package tsc.s6.SuSheShuiDianGuanLi.ui;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JScrollPane;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import java.awt.Color;
import java.awt.SystemColor;
import java.util.ArrayList;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JPasswordField;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import tsc.s6.SuSheShuiDianGuanLi.bll.AdminBll;
import tsc.s6.SuSheShuiDianGuanLi.model.Admin;
import javax.swing.JButton;
import javax.swing.border.LineBorder;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.ImageIcon;
public class AdminFrame extends JFrame {
/**
*
*/
private static final long serialVersionUID = -6850791069979763011L;
private JPanel contentPane;
private JTextField textField_name;
private JTextField textField_account;
private JTextField textField_5;
private JTable table;
private JComboBox comboBox;
Object admin[][] = new Object[50][5];//假设最多只有50个管理员
/**
* Launch the application.
*/
public static void main(String[] args,Admin ad) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
AdminFrame frame = new AdminFrame(ad);
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
* @param ad
*/
@SuppressWarnings({ "unchecked", "serial" })
public AdminFrame(Admin ad) {
setTitle("\u7BA1\u7406");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 683, 419);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
JLabel label = new JLabel("\u6B22\u8FCE\u767B\u9646\u897F\u5357\u4EA4\u5927\u5BBF\u820D\u6C34\u7535\u7BA1\u7406\u7CFB\u7EDF");
label.setBackground(SystemColor.activeCaption);
label.setFont(UIManager.getFont("Button.font"));
label.setHorizontalAlignment(SwingConstants.CENTER);
JPanel panel_1 = new JPanel();
JPanel panel_2 = new JPanel();
JScrollPane scrollPane = new JScrollPane();
scrollPane.setViewportBorder(new LineBorder(new Color(0, 0, 0)));
GroupLayout gl_contentPane = new GroupLayout(contentPane);
gl_contentPane.setHorizontalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addComponent(label, GroupLayout.DEFAULT_SIZE, 657, Short.MAX_VALUE)
.addGroup(gl_contentPane.createSequentialGroup()
.addContainerGap()
.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 416, GroupLayout.PREFERRED_SIZE)
.addGap(37)
.addComponent(panel_1, GroupLayout.DEFAULT_SIZE, 184, Short.MAX_VALUE)
.addContainerGap())
.addGroup(gl_contentPane.createSequentialGroup()
.addComponent(panel_2, GroupLayout.DEFAULT_SIZE, 647, Short.MAX_VALUE)
.addContainerGap())
);
gl_contentPane.setVerticalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addComponent(label, GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 274, GroupLayout.PREFERRED_SIZE)
.addComponent(panel_1, 0, 0, Short.MAX_VALUE))
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(panel_2, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
table = new JTable();
AdminBll admBll = new AdminBll();
ArrayList<Object> objectList = admBll.getAllAdmin();
for(int i = 0 ;i<objectList.size();i++) {
admin[i][0] = i+1;
admin[i][1] = ((Admin)objectList.get(i)).getName();
admin[i][2] = ((Admin)objectList.get(i)).getAccountNum();
if(((Admin)objectList.get(i)).getType().equals("0")) {
admin[i][3] = "超级管理";
}
else
admin[i][3] = "一般管理";
admin[i][4] = ((Admin)objectList.get(i)).getRegisterDate();
}
JButton button_refresh = new JButton("");button_refresh.setToolTipText("刷新");button_refresh.setContentAreaFilled(false);
button_refresh.setIcon(new ImageIcon(AdminFrame.class.getResource("/resources/icon/refresh_32.png")));
button_refresh.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
//刷新表格
AdminBll admBll = new AdminBll();
ArrayList<Object> objectList = admBll.getAllAdmin();
for(int i = 0 ;i<objectList.size();i++) {
admin[i][0] = i+1;
admin[i][1] = ((Admin)objectList.get(i)).getName();
admin[i][2] = ((Admin)objectList.get(i)).getAccountNum();
if(((Admin)objectList.get(i)).getType().equals("0")) {
admin[i][3] = "超级管理";
}
else
admin[i][3] = "一般管理";
admin[i][4] = ((Admin)objectList.get(i)).getRegisterDate();
}
table.setModel(new DefaultTableModel(admin,
new String[] {
"\u5E8F\u53F7", "\u7BA1\u7406\u59D3\u540D", "\u7BA1\u7406\u8D26\u53F7", "\u7BA1\u7406\u7EA7\u522B", "\u6CE8\u518C\u65F6\u95F4"
}
) {
Class[] columnTypes = new Class[] {
Integer.class, Object.class, Object.class, Object.class, Object.class
};
public Class getColumnClass(int columnIndex) {
return columnTypes[columnIndex];
}
boolean[] columnEditables = new boolean[] {
false, false, false, false, false
};
public boolean isCellEditable(int row, int column) {
return columnEditables[column];
}
});
table.getColumnModel().getColumn(0).setResizable(false);
table.getColumnModel().getColumn(0).setPreferredWidth(36);
table.getColumnModel().getColumn(1).setResizable(false);
scrollPane.setViewportView(table);
}
});
table.setModel(new DefaultTableModel(admin,
new String[] {
"\u5E8F\u53F7", "\u7BA1\u7406\u59D3\u540D", "\u7BA1\u7406\u8D26\u53F7", "\u7BA1\u7406\u7EA7\u522B", "\u6CE8\u518C\u65F6\u95F4"
}
) {
Class[] columnTypes = new Class[] {
Integer.class, Object.class, Object.class, Object.class, Object.class
};
public Class getColumnClass(int columnIndex) {
return columnTypes[columnIndex];
}
boolean[] columnEditables = new boolean[] {
false, false, false, false, false
};
public boolean isCellEditable(int row, int column) {
return columnEditables[column];
}
});
table.getColumnModel().getColumn(0).setResizable(false);
table.getColumnModel().getColumn(0).setPreferredWidth(36);
table.getColumnModel().getColumn(1).setResizable(false);
scrollPane.setViewportView(table);
JButton button_search = new JButton("");
button_search.setToolTipText("按账号查找");
button_search.setContentAreaFilled(false);
button_search.setIcon(new ImageIcon(AdminFrame.class.getResource("/resources/icon/search_32.png")));
button_search.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//查找
AdminBll admBll = new AdminBll();
Admin adm = null;
String account = textField_account.getText();
if(account.equals("")) {
JOptionPane.showMessageDialog(null, "请输入账号");
}
else {
adm = admBll.getAdminByAccount(account);
if( adm!= null) {
textField_name.setText(adm.getName());
textField_account.setText(adm.getAccountNum());
textField_5.setText(adm.getRegisterDate());
String type = "";
if(adm.getType().equals("0"))
type = "超级管理";
else
type = "一般管理";
comboBox.setSelectedItem(type);
}
else
JOptionPane.showMessageDialog(null, "查无此人");
}
}
});
JButton button_edit = new JButton("");
button_edit.setToolTipText("修改信息");
button_edit.setContentAreaFilled(false);
button_edit.setIcon(new ImageIcon(AdminFrame.class.getResource("/resources/icon/edit_32.png")));
button_edit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//修改
AdminBll admBll = new AdminBll();
Admin adm = null;
String account = textField_account.getText();
if(account.equals("")) {
JOptionPane.showMessageDialog(null, "请输入账号");
}
else {
adm = admBll.getAdminByAccount(account);
if( adm!= null) {
admBll.deleteAdmin(account);
EditAdminDialog.main(null,adm);
}
else
JOptionPane.showMessageDialog(null, "查无此人");
}
}
});
JButton button_delete = new JButton("");
button_delete.setToolTipText("按账号删除");
button_delete.setContentAreaFilled(false);
button_delete.setIcon(new ImageIcon(AdminFrame.class.getResource("/resources/icon/trash_32.png")));
textField_account = new JTextField();
button_delete.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//删除管理员
AdminBll admBll = new AdminBll();
String account = textField_account.getText();
if(!account.equals("")) {
if(admBll.deleteAdmin(account)) {
JOptionPane.showMessageDialog(null, "删除成功");
}
else
JOptionPane.showMessageDialog(null, "查无此人,删除失败");
}
else
JOptionPane.showMessageDialog(null, "请输入账号");
}
});
JButton button_household = new JButton("");
button_household.setToolTipText("住户管理");
button_household.setContentAreaFilled(false);
button_household.setIcon(new ImageIcon(AdminFrame.class.getResource("/resources/icon/smile.png")));
button_household.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//住户管理
ManageHouseholdFrame.main(null,ad);
dispose();
}
});
JButton button_setBill = new JButton("");
button_setBill.setToolTipText("抄表管理");
button_setBill.setContentAreaFilled(false);
button_setBill.setIcon(new ImageIcon(AdminFrame.class.getResource("/resources/icon/set_bill.png")));
button_setBill.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//账单管理
ManageBillFrame.main(null, ad);//传入ad可以限制管理的操作
dispose();//aa关闭当前窗口
}
});
JButton button_exit = new JButton("");
button_exit.setToolTipText("退出系统");
button_exit.setContentAreaFilled(false);
button_exit.setIcon(new ImageIcon(AdminFrame.class.getResource("/resources/icon/close_32.png")));
button_exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);//退出系统
}
});
JButton button_add1 = new JButton("");
button_add1.setToolTipText("添加");
button_add1.setContentAreaFilled(false);
button_add1.setIcon(new ImageIcon(AdminFrame.class.getResource("/resources/icon/add_32.png")));
comboBox = new JComboBox();
button_add1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
//添加管理
AdminBll admBll = new AdminBll();
Admin adm = null;
String type = (String)comboBox.getSelectedItem();
if(type.equals("超级管理")) {
type = "0";
}
else
type = "1";
if(!textField_name.getText().equals("")&&!textField_account.getText().equals("")) {
adm = new Admin(textField_name.getText(),textField_account.getText(),"000000",type);
}
else
JOptionPane.showMessageDialog(null, "请输入数据完整且密码保持一致");
if(adm!=null) {
if(admBll.addAdmin(adm)) {
JOptionPane.showMessageDialog(null, "添加成功");
}
else
JOptionPane.showMessageDialog(null, "已有该管理账号,添加失败");
}
}
});
GroupLayout gl_panel_2 = new GroupLayout(panel_2);
gl_panel_2.setHorizontalGroup(
gl_panel_2.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel_2.createSequentialGroup()
.addGap(19)
.addComponent(button_refresh, GroupLayout.PREFERRED_SIZE, 42, GroupLayout.PREFERRED_SIZE)
.addGap(28)
.addComponent(button_search, GroupLayout.PREFERRED_SIZE, 45, GroupLayout.PREFERRED_SIZE)
.addGap(26)
.addComponent(button_edit, GroupLayout.PREFERRED_SIZE, 45, GroupLayout.PREFERRED_SIZE)
.addGap(30)
.addComponent(button_delete, GroupLayout.PREFERRED_SIZE, 44, GroupLayout.PREFERRED_SIZE)
.addGap(31)
.addComponent(button_add1, GroupLayout.PREFERRED_SIZE, 48, GroupLayout.PREFERRED_SIZE)
.addGap(27)
.addComponent(button_household, GroupLayout.PREFERRED_SIZE, 57, GroupLayout.PREFERRED_SIZE)
.addGap(34)
.addComponent(button_setBill, GroupLayout.PREFERRED_SIZE, 57, GroupLayout.PREFERRED_SIZE)
.addGap(34)
.addComponent(button_exit)
.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
gl_panel_2.setVerticalGroup(
gl_panel_2.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel_2.createSequentialGroup()
.addContainerGap()
.addGroup(gl_panel_2.createParallelGroup(Alignment.BASELINE)
.addComponent(button_refresh, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE)
.addComponent(button_search, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE)
.addComponent(button_edit, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE)
.addComponent(button_delete, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE)
.addComponent(button_add1, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE)
.addComponent(button_household, GroupLayout.PREFERRED_SIZE, 43, GroupLayout.PREFERRED_SIZE)
.addComponent(button_setBill, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE)
.addComponent(button_exit))
.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
panel_2.setLayout(gl_panel_2);
JLabel lblNewLabel = new JLabel("\u59D3\u540D\uFF1A");
textField_name = new JTextField();
textField_name.setColumns(10);
JLabel label_1 = new JLabel("\u8D26\u53F7\uFF1A");
textField_account.setColumns(10);
JLabel label_4 = new JLabel("\u7EA7\u522B\uFF1A");
JLabel label_5 = new JLabel("\u65F6\u95F4\uFF1A");
textField_5 = new JTextField();
textField_5.setEnabled(false);
textField_5.setEditable(false);
textField_5.setColumns(10);
comboBox.setModel(new DefaultComboBoxModel(new String[] {"\u4E00\u822C\u7BA1\u7406", "\u8D85\u7EA7\u7BA1\u7406"}));
JButton button_help = new JButton("");
button_help.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//操作提示
OperateAdminHelpDialog.main(null);
}
});
button_help.setToolTipText("操作提示");
button_help.setContentAreaFilled(false);
button_help.setIcon(new ImageIcon(AdminFrame.class.getResource("/resources/icon/help_32.png")));
GroupLayout gl_panel_1 = new GroupLayout(panel_1);
gl_panel_1.setHorizontalGroup(
gl_panel_1.createParallelGroup(Alignment.TRAILING)
.addGroup(gl_panel_1.createSequentialGroup()
.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel_1.createSequentialGroup()
.addGap(10)
.addComponent(lblNewLabel)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(textField_name, GroupLayout.DEFAULT_SIZE, 124, Short.MAX_VALUE))
.addGroup(gl_panel_1.createSequentialGroup()
.addContainerGap()
.addComponent(label_5)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(textField_5, GroupLayout.DEFAULT_SIZE, 124, Short.MAX_VALUE))
.addGroup(gl_panel_1.createSequentialGroup()
.addContainerGap()
.addComponent(button_help, GroupLayout.PREFERRED_SIZE, 56, GroupLayout.PREFERRED_SIZE))
.addGroup(gl_panel_1.createSequentialGroup()
.addContainerGap()
.addComponent(label_4)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(comboBox, 0, 124, Short.MAX_VALUE))
.addGroup(gl_panel_1.createSequentialGroup()
.addContainerGap()
.addComponent(label_1)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(textField_account, GroupLayout.DEFAULT_SIZE, 124, Short.MAX_VALUE)))
.addContainerGap())
);
gl_panel_1.setVerticalGroup(
gl_panel_1.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel_1.createSequentialGroup()
.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel_1.createSequentialGroup()
.addGap(13)
.addComponent(lblNewLabel))
.addGroup(gl_panel_1.createSequentialGroup()
.addContainerGap()
.addComponent(textField_name, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
.addGap(31)
.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
.addComponent(label_1)
.addComponent(textField_account, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(31)
.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
.addComponent(label_4)
.addComponent(comboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(39)
.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
.addComponent(label_5)
.addComponent(textField_5, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(18)
.addComponent(button_help)
.addContainerGap(32, Short.MAX_VALUE))
);
panel_1.setLayout(gl_panel_1);
contentPane.setLayout(gl_contentPane);
}
}
UI设计建议
老师建议先不用windowbuilder插件,但个人建议先用再说,不然太打击自信心了,敲半天代码表达不了自己想要的界面!
主窗口一般用JFrame,布局用boder布局就是分东西南北中那个,之后各个方位(如果用到)里用group布局这种布局可以任意拖动,比较简单好用。
实验总结
这次实验里学得比较多,对于面向对象编程,接口,多态得到质的理解。
面向对象:比如Admin。在UI里获取admin的部分属性,然后封装成Admin ad =Admin(,)里面的参数就是UI里的之后把ad交给下一层去处理,反过来也是这样。
多态:在编码时自己曾特别疑惑,searchAdmin(),这里的参数到底是什么呢?是Admin?还是Admin的一个或多个属性?
最后在searchAdmin(String account),searchAdmin(String account,String password)里豁然开朗。
这样理解,把传入的参数都看成是对象。记得Python好像就是所见皆为对象。