基于java+mysql的swing+mysql物业收费系统(java+gui)
私信源码获取及调试交流
运行环境
Java≥8、MySQL≥5.7
开发工具
eclipse/idea/myeclipse/sts等均可配置运行
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
功能说明
基于java+mysql的Swing+MySQL物业收费系统(java+gui)
功能介绍:
用户登陆、用户注册、收费记录、生成业单、用户管理、查询缴费、历史账单、资料设置
clearButton.setForeground(Color.white);
clearButton.setBackground(new Color(130,100,64));
clearButton.setFont(font1);
add(yesButton);
add(clearButton);
}
public JComboBox<String> initBox() {
JComboBox<String> box=new JComboBox<String>();
l = getUserLists();
box.addItem(" ---请选择--- ");
for (User user : l) {
box.addItem(user.getUserName());
}
return box;
}
@SuppressWarnings("unchecked")
public List<User> getUserLists() {
return (List<User>)new UserDAOImp().selectAll();
}
public ItemListener getItemListener() {
@SuppressWarnings("static-access")
ItemListener itemListener=e -> {
if(e.getStateChange()==e.SELECTED) {
if(payBox.getSelectedIndex()==0) {
field01.setText("");
}else {
if(l.size()!=0) {
for(User u:l) {
if(u.getUserName().equals(payBox.getSelectedItem())) {
field01.setText("* 业主姓名:"+u.getName());
}
}
}
}
}
};
return itemListener;
}
public List<User> getL() {
return l;
}
public void setL(List<User> l) {
this.l = l;
}
List<User> selectOne = new UserDAOImp().selectOne(userNameString, phoneString);
if(selectOne.size()>0) {
int flag = new UserDAOImp().updateForForget(userNameString, phoneString, pwdString2);
if(flag<=0) {
JOptionPane.showMessageDialog(null, "数据库异常", "找回失败", JOptionPane.ERROR_MESSAGE);
}else {
JOptionPane.showMessageDialog(null, "密码找回成功,请尝试重新登录", "成功找回", JOptionPane.PLAIN_MESSAGE);
ForgetUI.dispose();
}
}else {
JOptionPane.showMessageDialog(null, "用户不存在或者此用户预留手机号码不正确", "找回失败", JOptionPane.ERROR_MESSAGE);
}
// }else {
// JOptionPane.showMessageDialog(null, "验证码错误或者无效", "找回失败", JOptionPane.ERROR_MESSAGE);
// }
// }else {
// JOptionPane.showMessageDialog(null, "请先获取验证码", "警告", JOptionPane.WARNING_MESSAGE);
// }
}else {
JOptionPane.showMessageDialog(null, "两次输入密码不一致", "找回失败", JOptionPane.ERROR_MESSAGE);
}
}
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==sendBtn) {
codeCheck();
}
if(e.getSource()==submitBtn) {
checkRight();
}
}
private void codeCheck() {
if(phoneField.getText().matches(regexCh)) {
timer.schedule(new TimerTask() {
int i = 60;
@Override
public void run() {
sendBtn.setForeground(Color.white);
sendBtn.setBackground(Color.DARK_GRAY);
sendBtn.setEnabled(false);
i--;
sendBtn.setText(i + "s");
//sendBtn.setHorizontalTextPosition(JButton.CENTER);
if (i <= 0) {
//设置按钮可点击 并且停止任务
sendBtn.setEnabled(true);
sendBtn.setHorizontalTextPosition(JButton.CENTER);
sendBtn.setFocusPainted(false);
sendBtn.setBorderPainted(false);
sendBtn.setBackground(new Color(0,90,171));
sendBtn.setFont(new Font("幼圆",Font.PLAIN,14));
sendBtn.setForeground(Color.white);
sendBtn.setBounds(290, 15, 80, 40);
jPanel04.add(jLabel04);
jPanel04.add(keyField);
jPanel04.add(sendBtn);
jPanel04.setBounds(0,200,this.getWidth(),50);
this.add(jPanel04);
// 学历+下拉列表
jPanel05=new JPanel(null);
jPanel05.setBackground(color);
JLabel jLabel=new JLabel("类型");
jLabel.setBounds(100, 15, 60, 40);
jLabel.setFont(font);
box=new JComboBox<String>();
box.addItem("常住");
box.addItem("租户");
box.setSelectedIndex(0);
box.setFont(new Font("幼圆", Font.PLAIN, 17));
box.setBounds(170, 25, 200, 30);
jPanel05.add(jLabel);
jPanel05.add(box);
jPanel05.setBounds(0, 250, this.getWidth(), 50);
this.add(jPanel05);
// 自我介绍+文本域
jPanel06=new JPanel();
jPanel06.setLayout(null);
jPanel06.setBackground(color);
JLabel jLabel05=new JLabel("门牌信息");
jLabel05.setBounds(100, 15, 60, 40);
jLabel05.setFont(font);
textArea=new JTextArea();
textArea.setLineWrap(true);
textArea.setFont(fontField);
textArea.setBorder(null);
jScrollPane=new JScrollPane(textArea);
jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
jScrollPane.setBounds(170, 25, 200, 80);
jPanel06.add(jLabel05);
jPanel06.add(jScrollPane);
jPanel06.setBounds(0, 300, this.getWidth(), 130);
this.add(jPanel06);
// 提交按钮
field.setFont(fontField);
field.setBorder(null);
jPanel01.add(jLabel01);
jPanel01.add(field);
jPanel01.setBounds(0, 50, this.getWidth(), 50);
this.add(jPanel01);
// 密码+密码框
jPanel02=new JPanel();
jPanel02.setLayout(null);
jPanel02.setBackground(color);
JLabel jLabel02=new JLabel("密码");
jLabel02.setBounds(100, 20, 60, 40);
jLabel02.setFont(font);
passwordField=new JPasswordField(20);
passwordField.setBounds(170, 20, 200, 40);
passwordField.setFont(fontField);
passwordField.setBorder(null);
jPanel02.add(jLabel02);
jPanel02.add(passwordField);
jPanel02.setBounds(0, 100, this.getWidth(), 50);
this.add(jPanel02);
// 手机号+文本框
jPanel03=new JPanel();
jPanel03.setLayout(null);
jPanel03.setBackground(color);
JLabel jLabel03=new JLabel("手机号");
jLabel03.setBounds(100, 20, 60, 30);
jLabel03.setFont(font);
phoneField=new JTextField();
phoneField.setBounds(170, 20, 200, 40);
phoneField.setFont(fontField);
phoneField.setBorder(null);
jPanel03.add(jLabel03);
jPanel03.add(phoneField);
jPanel03.setBounds(0, 150, this.getWidth(), 50);
this.add(jPanel03);
// 验证码+文本框
jPanel04=new JPanel();
ItemListener itemListener=new ItemListener() {
@SuppressWarnings("static-access")
@Override
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange()==e.SELECTED) {
if(willPayBox.getSelectedItem().toString().equals("暂无欠费业单")) {
payBtn.setBackground(Color.DARK_GRAY);
codeBtn.setBackground(Color.DARK_GRAY);
payBtn.setEnabled(false);// 没有欠费单的话 按钮无法使用
codeBtn.setEnabled(false);
fields[3].setText("您的物业费用已缴过费了!");
fields[4].setText("");
}else {
payBtn.setEnabled(true);
codeBtn.setEnabled(true);
String it=willPayBox.getSelectedItem().toString();
int payID=Integer.parseInt(it.substring(6, it.length()));
List<Payment> list = getNoPayList();
for(Payment np:list) {
if(np.getID()==payID) {
fields[3].setText("设备检查费:"+np.getCheckEXP()+"元 清洁费:"+np.getCleanEXP()+"元 停车费:"+np.getParkEXP());
fields[4].setText("总计:"+(np.getCheckEXP()+np.getCleanEXP()+np.getParkEXP())+"元");
}
}
}
}
}
};
return itemListener;
}
public JTextField[] getFields() {
return fields;
}
public void setFields(JTextField[] fields) {
this.fields = fields;
}
public JComboBox<String> getWillPayBox() {
return willPayBox;
}
public void setWillPayBox(JComboBox<String> willPayBox) {
this.willPayBox = willPayBox;
}
public List<Payment> getpList() {
return pList;
}
public void setpList(List<Payment> pList) {
this.pList = pList;
if(e.getSource()==sendBtn) {
// 点击注册按钮验证手机号
codeCheck();
}
}
private void codeCheck() {
if(phoneField.getText().matches(regexCh)) {
timer.schedule(new TimerTask() {
int i = 60;
@Override
public void run() {
sendBtn.setEnabled(false);
i--;
sendBtn.setText(i + "s");
sendBtn.setForeground(Color.white);
//sendBtn.setHorizontalTextPosition(JButton.CENTER);
if (i <= 0) {
//设置按钮可点击 并且停止任务
sendBtn.setEnabled(true);
sendBtn.setText("验证码");
//sendBtn.setHorizontalTextPosition(JButton.CENTER);
timer.cancel();
}
}
}, 0, 1000);
//验证手机号
String phonemsg = PhoneCode.getPhonemsg(phoneField.getText(),0);
codeString=PhoneCode.codeNum;
if(!phonemsg.equals("true")) {
JOptionPane.showMessageDialog(null, phonemsg);
}
}else {
JOptionPane.showMessageDialog(null,"电话号码不合法!","警告",JOptionPane.WARNING_MESSAGE);
}
}
public KeyAdapter getKeyAdapter() {
KeyAdapter keyAdapter=new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
int keyChar=e.getKeyChar();
if(adapterDAO instanceof PaymentDAOImp) {
PaymentDAOImp p=(PaymentDAOImp)adapterDAO;
p.delete(idList);
}else if(adapterDAO instanceof UserDAOImp){
UserDAOImp u=(UserDAOImp)adapterDAO;
u.delete(idList);
}
}
}
}
}
public class UserPaneSon1 extends JPanel{
private static final long serialVersionUID = -1804666L;
private JLabel[] labels=new JLabel[4];
private JTextField[] fields=new JTextField[5];
private JComboBox<String> willPayBox;
private List<Payment> pList;
private JButton payBtn,codeBtn;
private Font font=new Font("宋体",Font.PLAIN,22);
private Font fontField=new Font("宋体",Font.BOLD,18);
private User user;
public UserPaneSon1() {
super(null);
setBounds(0, 0, 900, 590);
setBackground(Color.WHITE);
labels=initJLabels(labels, labels.length);
for(JLabel jLabel:labels) {
add(jLabel);
}
fields=iniTextField(fields, fields.length);
for(JTextField f:fields) {
jPanel01=new JPanel();
jPanel01.setLayout(null);
jPanel01.setBackground(color);
JLabel jLabel01=new JLabel("用户名");
jLabel01.setBounds(100, 20, 60, 40);
jLabel01.setFont(font);
field=new JTextField(20);
field.setBounds(170, 20, 200, 40);
field.setFont(fontField);
field.setBorder(null);
jPanel01.add(jLabel01);
jPanel01.add(field);
jPanel01.setBounds(0, 50, this.getWidth(), 50);
this.add(jPanel01);
// 密码+密码框
jPanel02=new JPanel();
jPanel02.setLayout(null);
jPanel02.setBackground(color);
JLabel jLabel02=new JLabel("密码");
jLabel02.setBounds(100, 20, 60, 40);
jLabel02.setFont(font);
passwordField=new JPasswordField(20);
passwordField.setBounds(170, 20, 200, 40);
passwordField.setFont(fontField);
passwordField.setBorder(null);
jPanel02.add(jLabel02);
jPanel02.add(passwordField);
jPanel02.setBounds(0, 100, this.getWidth(), 50);
this.add(jPanel02);
// 手机号+文本框
jPanel03=new JPanel();
jPanel03.setLayout(null);
jPanel03.setBackground(color);
JLabel jLabel03=new JLabel("手机号");
jLabel03.setBounds(100, 20, 60, 30);
jLabel03.setFont(font);
phoneField=new JTextField();
phoneField.setBounds(170, 20, 200, 40);
phoneField.setFont(fontField);
phoneField.setBorder(null);
jPanel03.add(jLabel03);
add(headPanel);
}
private static String getDateStr() {
Calendar c=Calendar.getInstance();
Date d=new Date();
c.setTime(d);//设置指定时间
int year=c.get(Calendar.YEAR);
int month=c.get(Calendar.MONTH)+1; //默认是0-11,我国是1-12
int day=c.get(Calendar.DAY_OF_MONTH);
return year+"/"+month+"/"+day;
}
public JTabbedPane initJTabbedPane() {
allTabbedPane=new JTabbedPane();
jPanel01=new JPanel(null);
jPanel02=new JPanel(null);
jPanel03=new JPanel(null);
jPanel01.setBackground(color);
jPanel02.setBackground(color);
jPanel03.setBackground(color);
allTabbedPane.addTab("jPanel01", jPanel01); //添加选项卡容器,并且设置其中每个选项卡的标签以及其是否可启用
allTabbedPane.setEnabledAt(0, true);
allTabbedPane.setTitleAt(0, "收费记录");
allTabbedPane.addTab("jPanel02", jPanel02); //添加选项卡容器,并且设置其中每个选项卡的标签以及其是否可启用
allTabbedPane.setEnabledAt(1, true);
allTabbedPane.setTitleAt(1, "生成业单");
allTabbedPane.addTab("jPanel03", jPanel03); //添加选项卡容器,并且设置其中每个选项卡的标签以及其是否可启用
allTabbedPane.setEnabledAt(2, true);
allTabbedPane.setTitleAt(2, "用户管理");
allTabbedPane.setUI(new TabbedPaneDef());
allTabbedPane.setBounds(0, 60, 900, 640);
allTabbedPane.setFont(new Font("宋体", Font.BOLD, 18));
allTabbedPane.setForeground(new Color(255,255,255));
jPanel01.add(initScrollPane());
jPanel01.add(initBtnPane());
closeButton = new JButton();
// 设置按钮格式
minButton.setBounds(488, 0, 54, 50);
closeButton.setBounds(542, 0, 58, 50);
minButton.setFocusPainted(false);
closeButton.setFocusPainted(false);
minButton.setBorderPainted(false);
closeButton.setBorderPainted(false);
minButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
closeButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
minButton.setIcon(new ImageIcon("login_img/min.png"));
minButton.setRolloverIcon(new ImageIcon("login_img/min2.png"));
closeButton.setIcon(new ImageIcon("login_img/close.png"));
closeButton.setRolloverIcon(new ImageIcon("login_img/close2.png"));
closeButton.setPressedIcon(new ImageIcon("login_img/close3.png"));
//设置背景
JLabel bgImgJLabel=new JLabel(new ImageIcon("login_img/bg.gif"), JLabel.CENTER);
bgImgJLabel.setBounds(0, 0, 600, 315);
headPanel.add(minButton);
headPanel.add(closeButton);
headPanel.add(bgImgJLabel);
return headPanel;
}
private JPanel cenPanel() {
centerPanel=new JPanel(null);
centerPanel.setPreferredSize(new Dimension(600,0));
centerPanel.setBackground(new Color(190,230,240));
Font font=new Font("微软雅黑",Font.BOLD,24);
// 账号+输入框
JLabel jLabel=new JLabel("账 号");
userNameField=new JTextField();
jLabel.setBounds(170, 0, 65, 60);
userNameField.setBounds(238, 10, 220, 40);
jLabel.setFont(font);
userNameField.setFont(font);
centerPanel.add(jLabel);
centerPanel.add(userNameField);
// 密码+密码框
JLabel jLabel2=new JLabel("密 码");
passwordField=new JPasswordField();
jLabel2.setBounds(170,70,65,60);
passwordField.setBounds(238, 80, 220, 40);
jLabel2.setFont(font);
miniBtn.setBorder(null);
closeBtn.setBorder(null);
miniBtn.setBackground(null);
closeBtn.setBackground(null);
miniBtn.setIcon(new ImageIcon("mstUI_img/mini2.png"));
miniBtn.setRolloverIcon(new ImageIcon("mstUI_img/mini1.png"));
miniBtn.setPressedIcon(new ImageIcon("mstUI_img/mini3.png"));
closeBtn.setIcon(new ImageIcon("mstUI_img/close2.png"));
closeBtn.setRolloverIcon(new ImageIcon("mstUI_img/close1.png"));
closeBtn.setPressedIcon(new ImageIcon("mstUI_img/close3.png"));
headPanel.add(miniBtn);
headPanel.add(closeBtn);
JLabel showWhoJLabel=new JLabel();
showWhoJLabel.setBounds(20, 0, 600, 60);
showWhoJLabel.setFont(new Font("微软雅黑",Font.BOLD,20));
String textString="物业收费系统-居民模式 Hi! "+user.getName()+" ,今天是"+getDateStr();
//String textString="物业收费系统-居民模式 Hi! "+" ,今天是"+getDateStr();
showWhoJLabel.setText(textString);
headPanel.add(showWhoJLabel);
add(headPanel);
}
private static String getDateStr() {
Calendar c=Calendar.getInstance();
Date d=new Date();
c.setTime(d);//设置指定时间
int year=c.get(Calendar.YEAR);
int month=c.get(Calendar.MONTH)+1; //默认是0-11,我国是1-12
int day=c.get(Calendar.DAY_OF_MONTH);
return year+"/"+month+"/"+day;
}
public JTabbedPane initJTabbedPane() {
uTabbedPane=new JTabbedPane();
jPanel01=new JPanel(null);
jPanel02=new JPanel(null);
jPanel03=new JPanel(null);
jPanel01.setBackground(color);
jPanel02.setBackground(color);
jPanel03.setBackground(color);
uTabbedPane.addTab("jPanel01", jPanel01); //添加选项卡容器,并且设置其中每个选项卡的标签以及其是否可启用
uTabbedPane.setEnabledAt(0, true);
uTabbedPane.setTitleAt(0, "查询缴费");
public int loadToMysql(User user) throws SQLException {
List<User> list=new ArrayList<User>();
list.add(user);
int insertNum = new UserDAOImp().insertObj(list);
return insertNum;
}
public static void main(String[] args) {
new ForgetUI();
}
}
public class LoginUI extends JFrame{
private static final long serialVersionUID = -1804462002L;
private JTextField userNameField;
private JPasswordField passwordField;
private JComboBox<String> box;
private JButton logButton,minButton,closeButton;
private JPanel headPanel ;
private JPanel centerPanel;
private JPanel floorJPanel;
private JButton registButton;
private JButton forgetButton;
private LoginListener loginListener;
public LoginUI() throws HeadlessException {
this.column=column;
this.owner=owner;
dialog=new JDialog(owner, true);
dialog.setUndecorated(true);
dialog.setTitle("资料修改验证");
dialog.setSize(400, 300);
dialog.setResizable(false);
dialog.setLocationRelativeTo(null);
dialog.setLayout(null);
dialog.getContentPane().setBackground(Color.lightGray);
JLabel jLabel=new JLabel("手机号验证:");
jLabel.setBounds(75, 70, 100, 40);
jLabel.setFont(new Font("",Font.PLAIN,16));
String phone = ((UserUI)owner).getUser().getPhone();
field01=new JTextField();
field01.setBounds(185, 70, 140, 40);
field01.setText(phone);
field01.setEditable(false);
field01.setBackground(null);
field01.setFont(new Font("",Font.PLAIN,16));
JLabel jLabel2=new JLabel("获取验证码:");
jLabel2.setBounds(75, 130, 100, 40);
jLabel2.setFont(new Font("",Font.PLAIN,16));
field02=new JTextField();
field02.setBounds(185, 130, 70, 40);
field02.setBackground(null);
field02.setFont(new Font("",Font.PLAIN,16));
button01=new JButton("发送");
button01.setBounds(260, 130, 65, 40);
button01.setBorder(null);
button01.setFocusPainted(false);
button01.setBackground(new Color(133, 162, 190));
button01.setForeground(Color.white);
button02=new JButton("确 认");
button02.setBounds(80, 200, 95, 40);
button02.setBorder(null);
button02.setFocusPainted(false);
button02.setBackground(new Color(103, 162, 100));
button02.setForeground(Color.white);
button03=new JButton("取 消");
button03.setBounds(220, 200, 95, 40);
button03.setBorder(null);
jScrollPane.setBounds(170, 25, 200, 80);
jPanel06.add(jLabel05);
jPanel06.add(jScrollPane);
jPanel06.setBounds(0, 300, this.getWidth(), 130);
this.add(jPanel06);
// 提交按钮
jPanel07=new JPanel();
jPanel07.setLayout(null);
jPanel07.setBackground(color);
submitBtn=new JButton("注 册");
submitBtn.setFocusPainted(false);
submitBtn.setBorderPainted(false);
submitBtn.setBackground(new Color(0,90,171));
submitBtn.setFont(new Font("幼圆",Font.PLAIN,17));
submitBtn.setForeground(Color.white);
submitBtn.setBounds(220, 0, 78, 35);
jPanel07.add(submitBtn);
jPanel07.setBounds(0, 450, this.getWidth(), 50);
this.add(jPanel07);
}
public User getRegistUser() {
return registUser;
}
public void setRegistUser(User registUser) {
this.registUser = registUser;
}
public int loadToMysql(User user) throws SQLException {
List<User> list=new ArrayList<User>();
list.add(user);
int insertNum = new UserDAOImp().insertObj(list);
return insertNum;
}
}
public class UserDAOImp extends AdapterDAO{
DataSource dataSource=DruidJDBC.getDataSource();
JdbcTemplate jt=new JdbcTemplate(dataSource);
String sql;
@Override
public List<User> getRightPost(String userName, String password){
// TODO Auto-generated method stub
sql="select * from user where userName = ? and userPassword = ?";
List<User> user=null;
user = jt.query(sql, new BeanPropertyRowMapper<User>(User.class),userName,password);
return user;
}
@Override
public int insertObj(List<?> list) throws SQLException {
// TODO Auto-generated method stub
int count=0;
if(list.size()==0) {
return count;
}
sql="insert into user(userName,userPassword,name,phone,userType,advDeposit,place,available) values(?,?,?,?,?,?,?,?)";
try {
for(Object one:list) {
User user=(User) one;
PreparedStatement pS = DruidJDBC.getConnection().prepareStatement("select * from user where userName=? or phone=?");
pS.setString(1, user.getUserName());
pS.setString(2, user.getPhone());
ResultSet rs = pS.executeQuery();
if(rs.next()==true) {
return -1;
}else {
count+=jt.update(sql, user.getUserName(),user.getUserPassword(),user.getName(),user.getPhone(),
user.getUserType(),"10000",user.getPlace(),user.getAvailable());
}
}
}catch (DuplicateKeyException e) {
return -1;// 表示表中已存在字段唯一属性的记录 -> 标志用户已存在
}
return count;
}
@Override
public List<?> selectAll() {
public class RegistListener extends MouseAdapter implements ActionListener{
private RegistUI registUI;
private JTextField nameField;
private JTextField field;
private JTextField phoneField;
private JTextField keyField;
private JPasswordField passwordField;
private JTextArea textArea;
@SuppressWarnings("unused") // 暂时去掉小黄警告
private JButton submitBtn,sendBtn;
private JComboBox< String > box;
private User registUser;
private static String codeString="";
String regexCh = "^((13[0-9])|(14[0-9])|(15[0-9])|(17[0-9])|(18[0-9])|(19[0-9]))\\d{8}$";
Timer timer=new Timer();
public RegistListener(RegistUI registUI, JTextField nameField,JTextField field, JTextField phoneField, JTextField keyField,
JPasswordField passwordField, JTextArea textArea, JButton submitBtn,
JButton sendBtn, JComboBox<String> box) {
super();
this.registUI = registUI;
this.nameField=nameField;
this.field = field;
this.phoneField = phoneField;
this.keyField = keyField;
this.passwordField = passwordField;
this.textArea = textArea;
this.submitBtn = submitBtn;
this.sendBtn = sendBtn;
this.box = box;
}
@Override
public void mouseClicked(MouseEvent e) {
System.out.println("MasterListener中账单生成的SQL语句错误");
}
}
}
}
if(e.getSource()==masterPaneSon1.getClearButton()) {
masterPaneSon1.getPayBox().setSelectedIndex(0);
masterPaneSon1.getField02().setText("");
masterPaneSon1.getField03().setText("");
masterPaneSon1.getField04().setText("");
}
if(e.getSource()==masterPaneSon2.getDelButton()) {
delRows(masterPaneSon2.getDtm(), masterPaneSon2.getUserTable(), new UserDAOImp());
}
}
public void delRows(DefaultTableModel tableModel,JTable table,AdapterDAO adapterDAO) {
List<Integer> idList=new ArrayList<Integer>();
int selectRows=table.getSelectedRows().length;// 取得用户所选行的行数
int[] selectedRows = table.getSelectedRows();
if(selectRows<=0) {
JOptionPane.showMessageDialog(null, "删除前要选中行", "警告", JOptionPane.WARNING_MESSAGE);
}else {
// 确认是否删除
int get = JOptionPane.showOptionDialog(null, "请确认是否删除?", "友情询问", JOptionPane.YES_NO_OPTION,
JOptionPane.WARNING_MESSAGE, null, new Object[]{"是","否"}, "否");
if(get!=1) {
// 视觉上:倒着删避免数组下标越界
for(int i=selectRows-1;i>=0;i--) {
idList.add((Integer.parseInt(tableModel.getValueAt(selectedRows[i], 0)+"")));
tableModel.removeRow(selectedRows[i]);
}
// 数据库中:先获取选中行的第0列 -> 编号列,通过编号删除数据库中的数据
// 调用删除方法
if(adapterDAO instanceof PaymentDAOImp) {
PaymentDAOImp p=(PaymentDAOImp)adapterDAO;
p.delete(idList);
}else if(adapterDAO instanceof UserDAOImp){
UserDAOImp u=(UserDAOImp)adapterDAO;
u.delete(idList);
add(delButton);
this.reflashButton = initJButton(reflashButton,"数据刷新",600, 520);
add(reflashButton);
userList=getUsersList();
add(initScrollPane(userList));
dtm.addTableModelListener(new TableModelListener() {
@Override
public void tableChanged(TableModelEvent e) {
// 修改单元格中的数据并推送到数据库
int id=Integer.parseInt(userTable.getValueAt(userTable.getSelectedRow(), 0)+"");
int selectedColumn = userTable.getSelectedColumn();
String newValue = userTable.getValueAt(userTable.getSelectedRow(), selectedColumn)+"";
int updateForMaster = new UserDAOImp().updateForMaster(id, selectedColumn, newValue);
if(updateForMaster==1) {
JOptionPane.showMessageDialog(null, "修改成功");
masterPaneSon1.setL(masterPaneSon1.getUserLists()); // 刷新业单中的用户数据
Object uname = userTable.getValueAt(userTable.getSelectedRow(), 1);
Object rowName = userTable.getValueAt(userTable.getSelectedRow(), 2);
if(masterPaneSon1.getPayBox().getSelectedItem().equals(uname)&columnNames[selectedColumn].equals("业主姓名")) {
masterPaneSon1.getField01().setText("* 业主姓名:"+rowName);
}
}
}
});
}
private JButton initJButton(JButton button,String title,int x,int y) {
button=new JButton(title);
button.setBounds(x,y,100, 40);
button.setBorder(null);
button.setFocusPainted(false);
button.setBackground(new Color(235,135,112));
button.setForeground(Color.WHITE);
button.setFont(new Font("幼圆",Font.PLAIN,16));
return button;
}
public List<User> getUsersList() {
@SuppressWarnings("unchecked")
List<User> userAll = (List<User>)new UserDAOImp().selectAll();
if(userAll.size()<=0) {