Article directory
- foreword
- 1. Introduction
- 2. Implementation principle
- code display
-
- Banks Controller
- UserController
- Admin Dao
- Banks Dao
- UserDao
- Admin
- Banks
- User
- BanksServiceImpl
- UsersServiceImpl
- BanksService interface
- UserService interface
- AppStar test class
- AddBanksMoneyView
- AddBanksView
- AddUsersMoneyView
- AddUserView
- MainView
- MoveMoneyView
- ShowBanksView
- ShowUserView
- UpdatePwdView
- UserLoginView
- UsersView
- Summarize
foreword
A small training project using IO streams, collections and JavaSwing
1. Introduction
The basic contents are:
-
log in
-
register
-
front page
-
personal information display
-
change Password
-
add bank card
-
Bank card view
-
recharge
-
withdraw
-
transfer
-
User Management
The main knowledge used is as follows:
Collection knowledge: List collection is used to transport temporary data.
Interface: The BankController interface and UserController interface are created to facilitate the invocation and creation of methods, and they are implemented by BankControllerimpl and UserControllerimpl classes respectively.
Create Entity Class: Use shortcuts for creating methods.
IO stream: Using the serialization and deserialization of object streams, the collection can be stored in and out of files multiple times, which is convenient for data updates and can effectively save user and bank card information. The classes that implement this code are AdminDao, BanksDao, and UserDao.
JavaSwing: Reasonably collocate and design GUI components, add them to the underlying container, display them in the window, then add listeners, handle events, and complete the functions of each interface. The application of this part of the technology is in the com.baizhi.view package
2. Implementation principle
log in page
The first thing to do is to design the login page, set up the mobile phone number and password as two fillable items, set up two buttons and set up a listener for them at the same time, click to log in to enter the main page, and click to register to jump to the registration page.
registration page
There are some user attributes set on the registration page. There is no format requirement. After filling in the information, click Submit, and the data will be stored in the file Admin.data through the AdminDao object flow. After the registration is completed, the page will be automatically closed. You can log in with this account when you log in again, and click Cancel Close the page.
front page
The main page contains a list of functions in the left navigation bar, displaying functions, and the functions can be displayed in the middle part of the interface. Click the home page to return to the home page. The corresponding interface is MainView.
User Info
There are user information displayed, including the amount. After the amount is modified, the amount here will also change accordingly. There are image labels in front of the text. The class of this page is ShowUserView.
change Password
This function is to re-set the password for the logged-in user, obtain all users through the UserDao class, and then access the user through the list collection, obtain the user's password by traversing the list, and then call setPwd to modify the user's password.
add bank card
Similar to registered users, it also saves the obtained bank card information in the list. If there is already a bank card, first save the content of the previous bank card in the list, and then save the newly added information, so that you can After saving, there is no need to consider the problem of overwriting the content of the information stored in the file. Click Submit to store the data. The class of this page is AddBanksView.
Bank card view
On this page, you can observe the bank card information owned by this account, and also obtain the list of bank card information owned by this mobile phone number through BankDao, and then print it out through traversal. This class is ShowBanksView.
recharge
Recharge is to recharge money from the bank card to the account to realize the amount exchange, pass the mony attribute obtained from the text box into the BanksServiceImpl class through the construction method, complete the collection in the back-end file, and then search, modify, and click recharge through traversal. Amount exchange will be realized. This class is AddUsersMoneyView.
withdraw
Withdrawing cash is to transfer the money in the account to the bank card.
Withdrawal and recharge are the same, and the two functions can be realized by rewriting the method and exchange of the two. This class is AddBanksMoneyView.
bank card transfer
First of all, you need to choose whether the transfer object is a bank card or an account, and then enter the card number and mobile phone number of the bank card or account respectively, and then traverse whether the given number exists
. Call the transfer-out and transfer-in methods at the same time. Realize money exchange.
This class is MoveMoneyView.
User Management
All registered users can be displayed here, which is the same as bank card viewing. It first gets a list of all user information, and then traverses and prints it out. This class is ShowUserView.
code display
Banks Controller
package com.baizhi.controller;
import com.baizhi.entity.Banks;
import com.baizhi.service.BanksService;
import com.baizhi.service.impl.BanksServiceImpl;
import java.util.List;
public class BanksController {
private BanksServiceImpl service =new BanksServiceImpl();
public boolean addBanks(Banks banks){
return service.addBanks(banks);
}
public List<Banks> showBanksByTel(String tel){
return service.showBanksByTel(tel);
}
public boolean deleteMoney(String banksNum,double money){
return service.deleteMoney(banksNum,money);
}
public boolean addMoney(String banksNum,double money){
return service.addMoney(banksNum,money);
}
public boolean moveMoney(String banksNum1,String banksNum2,double money){
return service.moveMoney(banksNum1,banksNum2,money);
}
public boolean chickNUm( String num) {
return service.chickNUm(num);
}
}
UserController
package com.baizhi.controller;
import com.baizhi.entity.Banks;
import com.baizhi.entity.User;
import com.baizhi.service.UserService;
import com.baizhi.service.impl.UsersServiceImpl;
import java.util.List;
// 用户管理
public class UserController {
private UserService userService =new UsersServiceImpl();
public User userLog(String tel,String pwd) {
return userService.findUSerByTelAdnpwd(tel,pwd);
}
// 注册
//String name, String cardNum, String tel, String pwd, String email, double money, String relName
public boolean addUser(String name, String cardNum, String tel, String pwd, String email, String relName){
User u =new User(name,cardNum,tel,pwd,email,0,relName);
return userService.addUsers(u);
}
public boolean isExistTel(String tel) {
return userService.isExistTel(tel);
}
public boolean isExistEmail(String email){
return userService.isExistEmail(email);
}
public boolean updatePwd(String tel,String odlPwd,String pwd){
return userService.updatePwd(tel,pwd);
}
public boolean addMoney(String tel,double money){
return userService.addMoney(tel,money);
}
public boolean deleteMoney(String tel,double money){
return userService.deleteMoney(tel,money);
}
public List<User> showUserTel(String tel){
return userService.showUserTel(tel);
}
public List<User> showUser(){
return userService.showUser();
}
public boolean chickNUm(String num) {
return userService.chickNUm(num);}
}
Admin Dao
package com.baizhi.dao;
import com.baizhi.entity.Admin;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
public class AdminDao {
public List<Admin> read(){
List<Admin> list =new ArrayList<>();
try{
ObjectInputStream IS=new ObjectInputStream(new FileInputStream("admin.data"));
list = (List<Admin>) IS.readObject();
IS.close();
}catch (Exception e){
e.printStackTrace();
}
return list;
}
public boolean write(List<Admin> list){
try {
ObjectOutputStream OS = new ObjectOutputStream(new FileOutputStream("admin.data"));
OS.writeObject(list);
OS.close();
return true;
}catch (Exception e){
e.printStackTrace();
System.out.println("写入失败");
return false;
}
}
public static void main(String[] args) {
List<Admin> list =new ArrayList<>();
list.add(new Admin("admin","123"));
AdminDao dao =new AdminDao();
dao.write(list);
List<Admin> lists =dao.read();
for (Admin ad : lists){
System.out.println(ad);
}
}
}
Banks Dao
package com.baizhi.dao;
import com.baizhi.entity.Banks;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
public class BanksDao {
public List<Banks> read(){
List<Banks> list =new ArrayList<>();
try {
FileInputStream file =new FileInputStream("Banks.data");
ObjectInputStream IS =new ObjectInputStream(file);
list = (List<Banks>) IS.readObject();
IS.close();
}catch (Exception e){
e.printStackTrace();
}
return list;
}
public boolean write(List<Banks> list){
try {
FileOutputStream file=new FileOutputStream("Banks.data");
ObjectOutputStream OS =new ObjectOutputStream(file);
OS.writeObject(list);
OS.close();
return true;
}catch (Exception e){
e.printStackTrace();
return false;
}
}
}
UserDao
package com.baizhi.dao;
import com.baizhi.entity.Admin;
import com.baizhi.entity.User;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
public class UserDao {
public List<User> read(){
List<User> list =new ArrayList<>();
try{
ObjectInputStream IS=new ObjectInputStream(new FileInputStream("User.data"));
list = (List<User>) IS.readObject();
IS.close();
}catch (Exception e){
e.printStackTrace();
}
return list;
}
public boolean write(List<User> list){
try {
ObjectOutputStream OS = new ObjectOutputStream(new FileOutputStream("User.data"));
OS.writeObject(list);
OS.close();
return true;
}catch (Exception e){
e.printStackTrace();
System.out.println("写入失败");
return false;
}
}
}
Admin
package com.baizhi.entity;
import java.io.Serializable;
public class Admin implements Serializable {
private String account;
private String pwd;
public Admin(String account, String pwd) {
this.account = account;
this.pwd = pwd;
}
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
@Override
public String toString() {
return "Admin{" +
"account='" + account + '\'' +
", pwd='" + pwd + '\'' +
'}';
}
}
Banks
package com.baizhi.entity;
import javax.swing.*;
import java.io.Serializable;
public class Banks implements Serializable {
private String bankNumber;
private double money;
private String bankName;
private String tel;
public Banks(String bankNumber, double money, String bankName, String tel) {
this.bankNumber = bankNumber;
this.money = money;
this.bankName = bankName;
this.tel = tel;
}
public String getBankNumber() {
return bankNumber;
}
public void setBankNumber(String bankNumber) {
this.bankNumber = bankNumber;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public String getBankName() {
return bankName;
}
public void setBankName(String bankName) {
this.bankName = bankName;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
@Override
public String toString() {
return "Banks{" +
"bankNumber='" + bankNumber + '\'' +
", money=" + money +
", bankName='" + bankName + '\'' +
", tel='" + tel + '\'' +
'}';
}
}
User
package com.baizhi.entity;
import java.io.Serializable;
public class User implements Serializable {
private String name;
private String cardNum;
private String tel;
private String pwd;
private String email;
private double money;
private String relName;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCardNum() {
return cardNum;
}
public void setCardNum(String cardNum) {
this.cardNum = cardNum;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public String getRelName() {
return relName;
}
public void setRelName(String relName) {
this.relName = relName;
}
public User(String name, String cardNum, String tel, String pwd, String email, double money, String relName) {
this.name = name;
this.cardNum = cardNum;
this.tel = tel;
this.pwd = pwd;
this.email = email;
this.money = money;
this.relName = relName;
}
public String toString() {
return "User{" +
"name='" + name + '\'' +
", cardNum='" + cardNum + '\'' +
", tel='" + tel + '\'' +
", pwd='" + pwd + '\'' +
", email='" + email + '\'' +
", money=" + money +
", relName='" + relName + '\'' +
'}';
}
}
BanksServiceImpl
package com.baizhi.service.impl;
import com.baizhi.dao.BanksDao;
import com.baizhi.entity.Banks;
import com.baizhi.entity.User;
import com.baizhi.service.BanksService;
import java.util.ArrayList;
import java.util.List;
public class BanksServiceImpl implements BanksService {
private BanksDao dao =new BanksDao();
private List<Banks> list;
@Override
public boolean addBanks(Banks banks) {
list =new ArrayList<>();
list= dao.read();
list.add(banks);
return dao.write(list);
}
@Override
public List<Banks> showBanksByTel(String tel) {
List<Banks> list=dao.read();
List<Banks> uList=new ArrayList<>();
for (Banks b:list){
if (b.getTel().equals(tel)) {
uList.add(b);
}
}
return uList;
}
@Override
public boolean deleteMoney(String banksNum, double money) {
List<Banks> list=dao.read();
for (Banks b:list){
System.out.println("长度"+list.size());
if (banksNum.equals(b.getBankNumber())){
double mo =b.getMoney();
if (mo-money<0){
return false;
}else {
b.setMoney(mo-money);
System.out.println("银行卡减后"+b.getMoney());
}
}
}
return dao.write(list);
}
@Override
public boolean moveMoney(String banksNum1, String banksNum2, double money) {
List<Banks> list =dao.read();
//System.out.println("出账户"+banksNum1+",入账户"+banksNum2);
for (Banks b: list) {
if (b.getBankNumber().equals(banksNum1) ) {
if (b.getMoney()-money>=0)
b.setMoney(b.getMoney() - money);
else return false;
}
}
for (Banks b:list){
if (b.getBankNumber().equals(banksNum2)){
b.setMoney(b.getMoney()+money);
}
}
return dao.write(list);
}
@Override
public boolean chickNUm(String num) {
List<Banks> list =dao.read();
for (Banks b:list){
if (b.getBankNumber().equals(num)){
return true;
}
}
return false;
}
public boolean addMoney(String banksNum, double money) {
List<Banks> list =dao.read();
for (Banks b:list){
if (b.getBankNumber().equals(banksNum)){
System.out.printf("银行余额(+):"+b.getMoney());
b.setMoney(b.getMoney()+money);
}
}
return dao.write(list);
}
}
UsersServiceImpl
package com.baizhi.service.impl;
import com.baizhi.dao.UserDao;
import com.baizhi.entity.Banks;
import com.baizhi.entity.User;
import com.baizhi.service.UserService;
import org.w3c.dom.ls.LSInput;
import java.util.ArrayList;
import java.util.List;
public class UsersServiceImpl implements UserService {
private UserDao dao =new UserDao();
@Override
public User findUSerByTelAdnpwd(String tel, String pwd) {
List<User> list =dao.read();
for (User U:list){
if(U.getTel().equals(tel)&&U.getPwd().equals(pwd)){
System.out.println("登录成功!");
return U;
}
}
return null;
}
@Override
public boolean addUsers(User user) {
List<User> list =dao.read();
list.add(user);
return dao.write(list);
}
@Override
public boolean isExistTel(String tel) {
List<User> list =dao.read();
for(User u:list){
if(u.getTel().equals(tel)){
return true;
}
}
return false;
}
@Override
public boolean isExistEmail(String email) {
List<User> list =dao.read();
for (User u:list){
if(u.getEmail().equals(email)){
return true;
}
}
return false;
}
@Override
public boolean updatePwd(String tel, String pwd) {
List<User> list =dao.read();
for (User user: list){
String uTel = user.getTel();
if ((uTel).equals(tel)){
user.setPwd(pwd);
break;
}
}
return dao.write(list);
}
@Override
public boolean addMoney(String tel, double money) {
List<User> list =dao.read();
for (User user:list){
if (user.getTel().equals(tel)){
user.setMoney(user.getMoney()+money);
System.out.println(user.getMoney());
}
}
return dao.write(list);
}
public boolean deleteMoney(String tel, double money) {
List<User> list=dao.read();
for (User user:list){
if (tel.equals(user.getTel())){
double mo =user.getMoney();
if (mo-money<0){
return false;
}else {
System.out.println(user.getMoney());
user.setMoney(mo-money);
System.out.println(user.getMoney());
}
}
}
return dao.write(list);
}
@Override
public List<User> showUserTel(String tel) {
return null;
}
@Override
public List<User> showUser() {
List<User> list=dao.read();
return list;
}
@Override
public boolean chickNUm(String num) {
List<User>list=dao.read();
for (User user:list){
if (user.getTel().equals(num)){
return true;
}
}
return false;
}
public List<User> showUserTel() {
List<User> list=dao.read();
List<User> uList=new ArrayList<>();
return uList;
}
}
BanksService interface
package com.baizhi.service;
import com.baizhi.entity.Banks;
import java.util.List;
public interface BanksService {
public boolean addBanks(Banks banks);
public List<Banks> showBanksByTel(String tel);
public boolean deleteMoney(String banksNum,double money);
boolean moveMoney(String banksNum1, String banksNum2, double money);
boolean chickNUm(String num);
}
UserService interface
package com.baizhi.service;
import com.baizhi.entity.Banks;
import com.baizhi.entity.User;
import java.util.List;
public interface UserService {
public User findUSerByTelAdnpwd(String tel,String pwd);
public boolean addUsers(User user);
public boolean isExistTel(String tel);
public boolean isExistEmail(String email);
public boolean updatePwd(String tel,String pwd);
public boolean addMoney(String tel,double money);
boolean deleteMoney(String tel, double money);
public List<User> showUserTel(String tel);
public List<User> showUser();
boolean chickNUm(String num);
}
AppStar test class
package com.baizhi.test;
import com.baizhi.view.UserLoginView;
public class AppStar {
public static void main(String[] args) {
UserLoginView u =new UserLoginView();
}
}
AddBanksMoneyView
package com.baizhi.view;
import com.baizhi.controller.BanksController;
import com.baizhi.controller.UserController;
import com.baizhi.entity.Banks;
import com.baizhi.entity.User;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;
public class AddBanksMoneyView extends JPanel implements MouseListener {
private JPanel top,bom;
private JLabel moneyJL,banksNumJL;
private JTextField moneyJF;
private JComboBox banksNumJB;
private JButton sub;
private User user;
public AddBanksMoneyView(User user, List<Banks> list){
this.user=user;
this.setLayout(new BorderLayout());
top =new JPanel();
top.setLayout(new GridLayout(2,2,20,20));
moneyJL=new JLabel("请输入提现金额:",JLabel.CENTER);
moneyJF=new JTextField(20);
top.add(moneyJL);
top.add(moneyJF);
banksNumJL =new JLabel("请选择提现银行卡号",JLabel.CENTER);
banksNumJB =new JComboBox();
banksNumJB.addItem("---请选择---");
for (Banks b: list){
banksNumJB.addItem(b.getBankNumber());
}
top.add(banksNumJL);
top.add(banksNumJB);
this.add(top,BorderLayout.CENTER);
bom =new JPanel();
sub =new JButton("提现");
sub.addMouseListener(this);
bom.add(sub);
this.add(bom,BorderLayout.SOUTH);
}
@Override
public void mouseClicked(MouseEvent e) {
String moneys=moneyJF.getText();
String banksNum=banksNumJB.getSelectedItem().toString();
// System.out.println("这是卡号"+banksNum+",提现"+);
if (moneys.equals("")){
JOptionPane.showMessageDialog(null,"金额不能为空!");
return;
}
else if("---请选择---".equals(banksNum)){
JOptionPane.showMessageDialog(null,"请选择银行卡!");
return;
}
try {
double money = Double.parseDouble(moneys);
if (money <= 0) {
JOptionPane.showMessageDialog(null, "充值金额要大于0!");
return;
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "请输入数字!");
return;
}
double money =Double.parseDouble(moneys);
BanksController banksController =new BanksController();
UserController userController=new UserController();
boolean flag= userController.deleteMoney(user.getTel(), money);
if(flag){
banksController.addMoney(banksNum,money);
JOptionPane.showMessageDialog(null,"提现成功");
return;
}else {
JOptionPane.showMessageDialog(null,"账户余额不足,提现失败!");
return;
}
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
}
AddBanksView
package com.baizhi.view;
import com.baizhi.controller.BanksController;
import com.baizhi.dao.BanksDao;
import com.baizhi.entity.Banks;
import com.baizhi.entity.User;
import jdk.nashorn.internal.scripts.JO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class AddBanksView extends JPanel implements MouseListener {
private User user;
private JPanel top,bom;
private JLabel bankNumJL, moneyJL, bankNameJL;
private JTextField bankNumJF,moneyJF,bankNameJF;
private JButton sub;
public AddBanksView(User user){
this.user=user;
this.setLayout(new BorderLayout());
top=new JPanel();
top.setLayout(new GridLayout(3,2,20,20));
bankNumJL =new JLabel("卡号:",JLabel.CENTER);
bankNumJF=new JTextField(20);
top.add(bankNumJL);
top.add(bankNumJF);
bankNameJL =new JLabel("开户行",JLabel.CENTER);
bankNameJF=new JTextField(20);
top.add(bankNameJL);
top.add(bankNameJF);
moneyJL=new JLabel("余额",JLabel.CENTER);
moneyJF=new JTextField(20);
top.add(moneyJL);
top.add(moneyJF);
this.add(top,BorderLayout.CENTER);
bom=new JPanel();
sub =new JButton("提交");
sub.setName("sub");
sub.addMouseListener(this);
bom.add(sub);
this.add(bom,BorderLayout.SOUTH);
}
@Override
public void mouseClicked(MouseEvent e) {
String bankNum=bankNumJF.getText();
String money =moneyJF.getText();
//double bankName =Double.parseDouble(bankNumJF.getText());
String bankName=bankNameJF.getText();
if (bankNum.equals("")){
JOptionPane.showMessageDialog(null,"卡号不能为空");
bankNameJF.setText("");
bankNumJF.setText("");
moneyJF.setText("");
}
if (money.equals("")){
JOptionPane.showMessageDialog(null,"余额不能为空");
bankNameJF.setText("");
bankNumJF.setText("");
moneyJF.setText("");
}
if (bankName.equals("")){
JOptionPane.showMessageDialog(null,"开户行不能为空");
bankNameJF.setText("");
bankNumJF.setText("");
moneyJF.setText("");
}
double mo=Double.parseDouble(money);
BanksController b =new BanksController();
Banks banks =new Banks(bankNum,mo,bankName,user.getTel());
boolean flag =b.addBanks(banks);
if (flag){
JOptionPane.showMessageDialog(null,"添加成功!");
bankNameJF.setText("");
bankNumJF.setText("");
moneyJF.setText("");
}else {
JOptionPane.showMessageDialog(null,"添加失败!");
bankNameJF.setText("");
bankNumJF.setText("");
moneyJF.setText("");
}
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
}
AddUsersMoneyView
package com.baizhi.view;
import com.baizhi.controller.BanksController;
import com.baizhi.controller.UserController;
import com.baizhi.entity.Banks;
import com.baizhi.entity.User;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;
public class AddUsersMoneyView extends JPanel implements MouseListener {
private JPanel top,bom;
private JLabel moneyJL,banksNumJL;
private JTextField moneyJF;
private JComboBox banksNumJB;
private JButton sub;
private User user;
public AddUsersMoneyView(User user, List<Banks> list){
this.user=user;
this.setLayout(new BorderLayout());
top =new JPanel();
top.setLayout(new GridLayout(2,2,20,20));
moneyJL=new JLabel("请输入充值金额:",JLabel.CENTER);
moneyJF=new JTextField(20);
top.add(moneyJL);
top.add(moneyJF);
banksNumJL =new JLabel("请选择充值账户",JLabel.CENTER);
banksNumJB =new JComboBox();
banksNumJB.addItem("---请选择---");
for (Banks b: list){
banksNumJB.addItem(b.getBankNumber());
}
top.add(banksNumJL);
top.add(banksNumJB);
this.add(top,BorderLayout.CENTER);
bom =new JPanel();
sub =new JButton("充值");
sub.addMouseListener(this);
bom.add(sub);
this.add(bom,BorderLayout.SOUTH);
}
@Override
public void mouseClicked(MouseEvent e) {
String moneys=moneyJF.getText();
String banksNum=banksNumJB.getSelectedItem().toString();
if (moneys.equals("")){
JOptionPane.showMessageDialog(null,"金额不能为空!");
return;
}
else if("---请选择---".equals(banksNum)){
JOptionPane.showMessageDialog(null,"请选择银行卡!");
return;
}
try {
double money = Double.parseDouble(moneys);
if (money <= 0) {
JOptionPane.showMessageDialog(null, "充值金额要大于0!");
return;
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "请输入数字!");
return;
}
double money =Double.parseDouble(moneys);
BanksController banksController =new BanksController();
UserController userController=new UserController();
boolean flag=banksController.deleteMoney(banksNum,money);
if(flag){
userController.addMoney(user.getTel(), money);
JOptionPane.showMessageDialog(null,"充值成功");
return;
}else {
JOptionPane.showMessageDialog(null,"银行卡余额不足,请更换银行卡!");
return;
}
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
}
AddUserView
package com.baizhi.view;
import com.baizhi.controller.UserController;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class AddUserView extends Dialog implements MouseListener {
private JPanel jp;
private JLabel telJL,pwdJL,nameJL,relnameJL,cardNumJL,emailJL;
private JTextField telJF,nameJF,relnameJF,cardNumJF,emailJF;
private JPasswordField pwdJF;
private JButton sub,clo;
public AddUserView (Frame owner){
super(owner);
jp= new JPanel();
jp.setBackground(Color.pink);
jp.setBorder(new EmptyBorder(20,20,20,20));
jp.setLayout(new GridLayout(10,2,20,20));
EmptyBorder job =new EmptyBorder(0,20,0,0);
EmptyBorder fbo =new EmptyBorder(0,0,0,0);
telJL =new JLabel("手机号",JLabel.LEFT);
telJL .setIcon(new ImageIcon("img/phone_icon.png"));
telJL.setBorder(job);
telJF =new JTextField(20);
telJF.setBorder(fbo);
jp.add(telJL);
jp.add(telJF);
pwdJL =new JLabel("密码",JLabel.LEFT);
pwdJL .setIcon(new ImageIcon("img/password_icon.png"));
pwdJL.setBorder(job);
pwdJF = new JPasswordField(20);
pwdJF.setBorder(fbo);
jp.add(pwdJL);
jp.add(pwdJF);
nameJL =new JLabel("昵称",JLabel.LEFT);
nameJL .setIcon(new ImageIcon("img/name_icon.png"));
nameJL.setBorder(job);
nameJF =new JTextField(20);
nameJF.setBorder(fbo);
jp.add(nameJL);
jp.add(nameJF);
relnameJL =new JLabel("真实姓名",JLabel.LEFT);
relnameJL .setIcon(new ImageIcon("img/username_icon.png"));
relnameJL.setBorder(job);
relnameJF =new JTextField(20);
relnameJF.setBorder(fbo);
jp.add(relnameJL);
jp.add(relnameJF);
cardNumJL =new JLabel("身份证号",JLabel.LEFT);
cardNumJL .setIcon(new ImageIcon("img/idc_icon.png"));
cardNumJL.setBorder(job);
cardNumJF =new JTextField(20);
cardNumJF.setBorder(fbo);
jp.add(cardNumJL);
jp.add(cardNumJF);
emailJL =new JLabel("邮箱",JLabel.LEFT);
emailJL .setIcon(new ImageIcon("img/msg_icon.png"));
emailJL.setBorder(job);
emailJF =new JTextField(20);
emailJF.setBorder(fbo);
jp.add(emailJL);
jp.add(emailJF);
sub =new JButton("提交");
sub.setName("sub");
sub.addMouseListener(this);
jp.add(sub);
clo=new JButton("取消");
clo.setName("clo");
clo.addMouseListener(this);
jp.add(clo);
this.add(jp);
this.setSize(400,500);
this.setIconImage(new ImageIcon("img/tubiao_qiaozhi.jpg").getImage());
this.setLocationRelativeTo(null); //居中
this.setVisible(true);
this.setTitle("百知转账管理系统-->用户注册");
this.setResizable(false);
// super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void mouseClicked(MouseEvent e) {
String str = e.getComponent().getName();
switch (str) {
case "sub":
// String name, String cardNum, String tel, String pwd, String email, double money, String relName
String tel =telJF.getText();
String pwd= new String(pwdJF.getPassword());
String name=nameJF.getText();
String relName=cardNumJF.getText();
String cardNUm=cardNumJF.getText();
String email=emailJF.getText();
if (tel.equals("")){
JOptionPane.showMessageDialog(null,"手机号不能为空!");
break;
}
else if (pwd.equals("")){
JOptionPane.showMessageDialog(null,"密码不能为空!");
break;
}
else if (name.equals("")){
JOptionPane.showMessageDialog(null,"昵称不能为空!");
break;
}
else if (relName.equals("")){
JOptionPane.showMessageDialog(null,"真实姓名不能为空!");
break;
}
else if (cardNUm.equals("")){
JOptionPane.showMessageDialog(null,"身份证号不能为空!");
break;
}
else if (email.equals("")){
JOptionPane.showMessageDialog(null,"邮箱不能为空!");
break;
}
UserController con =new UserController();
if (con.isExistEmail(tel)){
JOptionPane.showMessageDialog(null,"手机号已被注册");
break;
}
else if (con.isExistEmail(email)){
JOptionPane.showMessageDialog(null,"邮箱已被注册");
break;
}
else if (con.isExistEmail(cardNUm)){
JOptionPane.showMessageDialog(null,"身份证号被注册");
break;
}
boolean flag =con.addUser(tel,pwd,name,relName,cardNUm,email);
if (flag){
JOptionPane.showMessageDialog(null,"注册成功!");
dispose();
}else {
JOptionPane.showMessageDialog(null,"注册失败!");
}
break;
case "clo":
dispose();
break;
}
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
}
MainView
package com.baizhi.view;
import com.baizhi.controller.BanksController;
import com.baizhi.controller.UserController;
import com.baizhi.entity.Banks;
import com.baizhi.entity.User;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;
public class MainView extends JFrame implements MouseListener {
private User user;
private JPanel jpJP,topJP,leftJP,conJP,indexJP;
private JLabel logoJL,welJL,indexJL;
private JButton indexJB,showUserJB,pwdJB,banksJB,addMoneyJB,deleteMoneyJB,moveMoneyJB,updateUserJB,addBanksJB;
public MainView(User user){
jpJP=new JPanel();
jpJP.setLayout(new BorderLayout());
topJP =new JPanel();
logoJL=new JLabel();
//logoJL.setIcon(new ImageIcon("img/logo.png"));
ImageIcon lo =new ImageIcon("img/logo.png");
lo.setImage(lo.getImage().getScaledInstance(120,30,Image.SCALE_DEFAULT));
logoJL.setIcon(lo);
welJL=new JLabel("欢迎用户:"+user.getName());
topJP.add(welJL);
topJP.add(logoJL);
jpJP.add(topJP,BorderLayout.NORTH);
//左侧导航
leftJP=new JPanel();
leftJP.setLayout(new GridLayout(10,1,20,7));
EmptyBorder jb =new EmptyBorder(20,15,10,20);
indexJB =new JButton("首页");
indexJB.setForeground(Color.BLUE);
indexJB.setName("indexJB");
indexJB.setBorder(jb);
indexJB.addMouseListener(this);
leftJP.add(indexJB);
showUserJB =new JButton("用户信息");
showUserJB.setForeground(Color.BLUE);
showUserJB.setName("showUserJB");
showUserJB.setBorder(jb);
showUserJB.addMouseListener(this);
leftJP.add(showUserJB);
pwdJB =new JButton("修改密码");
pwdJB.setForeground(Color.BLUE);
pwdJB.setName("pwdJB");
pwdJB.setBorder(jb);
pwdJB.addMouseListener(this);
leftJP.add(pwdJB);
addBanksJB =new JButton("添加银行卡");
addBanksJB.setForeground(Color.BLUE);
addBanksJB.setName("addBankJB");
addBanksJB.setBorder(jb);
addBanksJB.addMouseListener(this);
leftJP.add(addBanksJB);
banksJB =new JButton("银行卡");
banksJB.setForeground(Color.BLUE);
banksJB.setName("banksJB");
banksJB.setBorder(jb);
banksJB.addMouseListener(this);
leftJP.add(banksJB);
addMoneyJB =new JButton("充值");
addMoneyJB.setForeground(Color.BLUE);
addMoneyJB.setName("addMoneyJB");
addMoneyJB.setBorder(jb);
addMoneyJB.addMouseListener(this);
leftJP.add(addMoneyJB);
deleteMoneyJB =new JButton("提现");
deleteMoneyJB.setForeground(Color.BLUE);
deleteMoneyJB.setName("deleteMoneyJB");
deleteMoneyJB.setBorder(jb);
deleteMoneyJB.addMouseListener(this);
leftJP.add(deleteMoneyJB);
moveMoneyJB =new JButton("转账");
moveMoneyJB.setForeground(Color.BLUE);
moveMoneyJB.setName("moveMoneyJB");
moveMoneyJB.setBorder(jb);
moveMoneyJB.addMouseListener(this);
leftJP.add(moveMoneyJB);
updateUserJB =new JButton("用户管理");
updateUserJB.setForeground(Color.BLUE);
updateUserJB.setName("updateUserJB");
updateUserJB.setBorder(jb);
updateUserJB.addMouseListener(this);
leftJP.add(updateUserJB);
jpJP.add(leftJP,BorderLayout.WEST);
conJP=new JPanel();
conJP.setBorder(new EmptyBorder(10,10,10,10));
indexJP =new JPanel();
indexJP.setSize(400,400);
indexJL=new JLabel();
ImageIcon icon =new ImageIcon("img/banner1.jpg");
icon.setImage(icon.getImage().getScaledInstance(450,350,Image.SCALE_DEFAULT));
indexJL.setIcon(icon);
indexJP.add(indexJL);
conJP.add(indexJP);
jpJP.add(conJP);
this.add(jpJP);
this.user=user;
this.setIconImage(new ImageIcon("img/tubiao_peiqi.jpg").getImage());
this.setSize(630,480);
this.setLocationRelativeTo(null); //居中
this.setVisible(true);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void updateView(JPanel jp){
conJP.removeAll();
conJP.add(jp);
conJP.validate();
conJP.updateUI();
}
@Override
public void mouseClicked(MouseEvent e) {
// JButton indexJB,showUserJB,pwdJB,banksJB,addMoneyJB,deleteMoneyJB,moveMoneyJB,updateUserJB;
String str =e.getComponent().getName();
switch (str){
case "indexJB":
updateView(indexJP);
break;
case "showUserJB":
UserController userController0 =new UserController();
List<User> list0 = userController0.showUser();
ShowUserView showUserView =new ShowUserView(user,list0);
updateView(showUserView);
break;
case "pwdJB":
//System.out.println("11111");
UpdatePwdView updatePwdView =new UpdatePwdView(user);
updateView(updatePwdView);
break;
case "addBankJB":
AddBanksView addBanksView=new AddBanksView(user);
updateView(addBanksView);
break;
case "banksJB":
BanksController banksController =new BanksController();
List<Banks> list = banksController.showBanksByTel(user.getTel());
ShowBanksView showBanksView=new ShowBanksView(list);
updateView(showBanksView);
break;
case "addMoneyJB":
BanksController banksController1 =new BanksController();
List<Banks> lists =banksController1.showBanksByTel(user.getTel());
AddUsersMoneyView addUsersMoneyView =new AddUsersMoneyView(user,lists);
updateView(addUsersMoneyView);
break;
case "deleteMoneyJB":
BanksController banksController2 =new BanksController();
List<Banks> listss =banksController2.showBanksByTel(user.getTel());
AddBanksMoneyView addBanksMoneyView =new AddBanksMoneyView(user,listss);
updateView(addBanksMoneyView);
break;
case "moveMoneyJB":
BanksController banksController3 =new BanksController();
List<Banks> listsss =banksController3.showBanksByTel(user.getTel());
MoveMoneyView moveMoneyView =new MoveMoneyView(listsss);
updateView(moveMoneyView);
break;
case "updateUserJB":
UserController userController =new UserController();
List<User> list1 = userController.showUser();
UsersView usersView =new UsersView(list1);
updateView(usersView);
break;
}
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
}
MoveMoneyView
package com.baizhi.view;
import com.baizhi.controller.BanksController;
import com.baizhi.controller.UserController;
import com.baizhi.entity.Banks;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;
public class MoveMoneyView extends JPanel implements MouseListener {
ImageIcon icon;
Image img;
private Box boxV,boxH,boxH1,boxH2,boxH3,boxH4;
private JLabel inMoneyJL,outMoneyJL,moneyJL,topJL,inMoneyNumJL,outMoneyNumJL;
private JTextField moneyJF,outMoneyJF,inMoneyJF;
private JButton stu;
private JComboBox inMoneyJC,outMoneyJC;
public MoveMoneyView(List<Banks> list) {
icon=new ImageIcon("img/bg2.png");
img=icon.getImage();
boxH=Box.createHorizontalBox();
boxH1=Box.createHorizontalBox();
boxH2=Box.createHorizontalBox();
boxH3=Box.createHorizontalBox();
boxH4=Box.createHorizontalBox();
boxV=Box.createVerticalBox();
topJL =new JLabel("银行卡转账");
topJL.setFont(new Font("微软雅黑", Font.BOLD, 26));
boxH.add(topJL);
outMoneyJL=new JLabel("请选择转出对象:");
outMoneyJC = new JComboBox();
outMoneyJC.addItem("---请选择---");
outMoneyJC.addItem("账户");
outMoneyJC.addItem("银行卡");
outMoneyNumJL=new JLabel("请输入转出的卡号或手机号:");
outMoneyJF =new JTextField();
boxH1.add(outMoneyJL);
boxH1.add(Box.createHorizontalStrut(10));
boxH1.add(outMoneyJC);
boxH1.add(Box.createHorizontalStrut(10));
boxH1.add(outMoneyNumJL);
boxH1.add(Box.createHorizontalStrut(10));
boxH1.add(outMoneyJF);
inMoneyJL=new JLabel("请选择转入对象:");
inMoneyJC = new JComboBox();
inMoneyJC.addItem("---请选择---");
inMoneyJC.addItem("账户");
inMoneyJC.addItem("银行卡");
inMoneyNumJL=new JLabel("请输入转出的卡号或手机号:");
inMoneyJF =new JTextField();
boxH2.add(inMoneyJL);
boxH2.add(Box.createHorizontalStrut(10));
boxH2.add(inMoneyJC);
boxH2.add(Box.createHorizontalStrut(10));
boxH2.add(inMoneyNumJL);
boxH2.add(Box.createHorizontalStrut(10));
boxH2.add(inMoneyJF);
moneyJL =new JLabel("请输入转帐金额:");
moneyJF=new JTextField(20);
moneyJF.setFont(new Font(null, Font.PLAIN, 20));
boxH3.add(Box.createHorizontalStrut(30));
boxH3.add(moneyJL);
boxH3.add(Box.createHorizontalStrut(18));
boxH3.add(moneyJF);
boxH3.add(Box.createHorizontalStrut(30));
stu =new JButton("确定");
stu.setName("stu");
stu.addMouseListener(this);
boxH4.add(stu);
boxV.add(boxH);
boxV.add(Box.createVerticalStrut(40));
boxV.add(boxH1);
boxV.add(Box.createVerticalStrut(40));
boxV.add(boxH2);
boxV.add(Box.createVerticalStrut(40));
boxV.add(boxH3);
boxV.add(Box.createVerticalStrut(40));
boxV.add(boxH4);
boxV.add(Box.createVerticalStrut(80));
this.add(boxV);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(img, 0, 0,this.getWidth(), this.getHeight(), this);
}
@Override
public void mouseClicked(MouseEvent e) {
String moneys = moneyJF.getText();
String outCla=outMoneyJC.getSelectedItem().toString();
String inCla=inMoneyJC.getSelectedItem().toString();
String outNum=outMoneyJF.getText();
String inNum=inMoneyJF.getText();
if (moneys.equals("")){
JOptionPane.showMessageDialog(null,"金额不能为空!");
return;
}
else if("---请选择---".equals(outCla)||"---请选择---".equals(inCla)){
JOptionPane.showMessageDialog(null,"请选择类型!");
return;
}
else if (outNum.equals("")||inNum.equals("")){
JOptionPane.showMessageDialog(null,"账户不能为空!");
}
try {
double money = Double.parseDouble(moneys);
if (money <= 0) {
JOptionPane.showMessageDialog(null, "充值金额要大于0!");
return;
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "请输入数字!");
return;
}
double money =Double.parseDouble(moneys);
BanksController banksController =new BanksController();
BanksController banksController1 =new BanksController();
UserController userController=new UserController();
boolean flag;
boolean a = false,b=false;
if (outCla.equals("银行卡")){
a=banksController.chickNUm(outNum);
if (inCla.equals("账户")){
b=userController.chickNUm(inNum);
}else if (inCla.equals("银行卡")){
b=banksController.chickNUm(inNum);
}
}else if (outCla.equals("账户")){
a=userController.chickNUm(outNum);
if (inCla.equals("账户")){
b=userController.chickNUm(inNum);
}else if (inCla.equals("银行卡")){
b=banksController.chickNUm(inNum);
}
}
flag =a&&b;
// boolean flag= banksController.moveMoney(banksNum,banksNum1,money);
if(flag){
//banksController.addMoney(banksNum,money);
int f=0;
if (outCla.equals("银行卡")){
System.out.println("1银行卡减钱了");
a=banksController.deleteMoney(outNum,money);
if (inCla.equals("账户")&&a){
b=userController.addMoney(inNum,money);
}else if (inCla.equals("银行卡")&&a){
System.out.println("二银行卡加钱了");
banksController1.addMoney(inNum,money);
}else {
JOptionPane.showMessageDialog(null,"账户余额不足,转账失败!");
inMoneyJF.setText("");
outMoneyJF.setText("");
moneyJF.setText("");
f=1;
}
}else if (outCla.equals("账户")){
a=userController.deleteMoney(outNum,money);
if (inCla.equals("账户")&&a){
userController.addMoney(inNum,money);
}else if (inCla.equals("银行卡")&&a){
System.out.println("2二银行卡加钱了");
banksController.addMoney(inNum,money);
}else {
JOptionPane.showMessageDialog(null,"账户余额不足,转账失败!");
inMoneyJF.setText("");
outMoneyJF.setText("");
moneyJF.setText("");
f=1;
}
}if (f==0) {
JOptionPane.showMessageDialog(null, "转账成功!");
inMoneyJF.setText("");
outMoneyJF.setText("");
moneyJF.setText("");
return;
}
}else {
JOptionPane.showMessageDialog(null,"账户错误,转账失败!");
inMoneyJF.setText("");
outMoneyJF.setText("");
moneyJF.setText("");
return;
}
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
}
ShowBanksView
package com.baizhi.view;
import com.baizhi.entity.Banks;
import javax.swing.*;
import java.util.List;
public class ShowBanksView extends JPanel {
List<Banks> List;
public ShowBanksView(List<Banks> list){
//System.out.println(list.size());
JScrollPane js =new JScrollPane();
Object[] clum = {
"卡号","开户行","余额"};
Object[][] data=new Object[list.size()][3];
for (int i=0;i<list.size();i++){
Banks b =list.get(i);
data[i][0]=b.getBankNumber();
data[i][1]=b.getBankName();
data[i][2]=b.getMoney();
}
JTable jTable =new JTable(data,clum);
js.setViewportView(jTable);
this.add(js);
}
}
ShowUserView
package com.baizhi.view;
import com.baizhi.entity.User;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.util.List;
public class ShowUserView extends JPanel {
private JPanel top,bom;
private JLabel telJL,pwdJL,nameJL,relnameJL,cardNumJL,emailJL,balJL;
private JTextField telJF,nameJF,relnameJF,cardNumJF,emailJF,balJF;
private JPasswordField pwdJF;
public ShowUserView(User user, List<User> list){
this.setLayout(new BorderLayout());
top=new JPanel();
JLabel topJL =new JLabel("个人信息展示");
topJL.setFont(new Font("微软雅黑", Font.PLAIN, 26));
top.add(topJL);
this.add(top,BorderLayout.NORTH);
//下边
bom=new JPanel();
bom.setLayout(new GridLayout(7,2,2,10));
EmptyBorder job =new EmptyBorder(0,0,20,20);
EmptyBorder fbo =new EmptyBorder(0,0,0,0);
telJL =new JLabel("手机号",JLabel.LEFT);
telJL .setIcon(new ImageIcon("img/phone_icon.png"));
telJL.setBorder(job);
telJF =new JTextField(20);
telJF.setText(user.getTel());
telJF.setBorder(fbo);
bom.add(telJL);
bom.add(telJF);
pwdJL =new JLabel("密码",JLabel.LEFT);
pwdJL .setIcon(new ImageIcon("img/password_icon.png"));
pwdJL.setBorder(job);
pwdJF = new JPasswordField(20);
pwdJF.setText(user.getPwd());
pwdJF.setBorder(fbo);
bom.add(pwdJL);
bom.add(pwdJF);
nameJL =new JLabel("昵称",JLabel.LEFT);
nameJL .setIcon(new ImageIcon("img/name_icon.png"));
nameJL.setBorder(job);
nameJF =new JTextField(20);
nameJF.setText(user.getName());
nameJF.setBorder(fbo);
bom.add(nameJL);
bom.add(nameJF);
relnameJL =new JLabel("真实姓名",JLabel.LEFT);
relnameJL .setIcon(new ImageIcon("img/username_icon.png"));
relnameJL.setBorder(job);
relnameJF =new JTextField(20);
relnameJF.setText(user.getRelName());
relnameJF.setBorder(fbo);
bom.add(relnameJL);
bom.add(relnameJF);
cardNumJL =new JLabel("身份证号",JLabel.LEFT);
cardNumJL .setIcon(new ImageIcon("img/idc_icon.png"));
cardNumJL.setBorder(job);
cardNumJF =new JTextField(20);
cardNumJF.setText(user.getCardNum());
cardNumJF.setBorder(fbo);
bom.add(cardNumJL);
bom.add(cardNumJF);
emailJL =new JLabel("邮箱",JLabel.LEFT);
emailJL .setIcon(new ImageIcon("img/msg_icon.png"));
emailJL.setBorder(job);
emailJF =new JTextField(20);
emailJF.setText(user.getEmail());
emailJF.setBorder(fbo);
bom.add(emailJL);
bom.add(emailJF);
balJL =new JLabel("余额",JLabel.LEFT);
balJL .setIcon(new ImageIcon("img/balance_icon.png"));
balJL.setBorder(job);
balJF =new JTextField(20);
for (User user1:list){
if (user1.getTel().equals(user.getTel())){
balJF.setText(String.valueOf(user1.getMoney()));
}
}
balJF.setBorder(fbo);
bom.add(balJL);
bom.add(balJF);
this.add(bom,BorderLayout.CENTER);
}
}
UpdatePwdView
package com.baizhi.view;
import com.baizhi.controller.UserController;
import com.baizhi.entity.User;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Objects;
public class UpdatePwdView extends JPanel implements MouseListener {
ImageIcon icon;
Image img;
private User user;
private Box boxV,boxH1,boxH2,boxH3,boxH,boxH4;
private JButton relJB,subJB;
private JLabel pwdJL,pwd1JL,topJL,pwd2JL;
private JPasswordField pwdJP,pwd1JP,pwd2JP;
public UpdatePwdView(User user){
icon=new ImageIcon("img/bg.png");
img=icon.getImage();
this.user=user;
boxV = Box.createVerticalBox();
boxH= Box.createHorizontalBox();
boxH1= Box.createHorizontalBox();
boxH2= Box.createHorizontalBox();
boxH3= Box.createHorizontalBox();
boxH4=Box.createHorizontalBox();
topJL=new JLabel("修改密码");
topJL.setFont(new Font("微软雅黑", Font.BOLD, 26));
boxH.add(topJL);
pwd2JL=new JLabel("请输入原密码:");
pwd2JP=new JPasswordField(20);
pwd2JL.setFont(new Font(null, Font.PLAIN, 18));
boxH4.add(Box.createHorizontalStrut(80));
boxH4.add(pwd2JL);
boxH4.add(pwd2JP);
boxH4.add(Box.createHorizontalStrut(80));
pwdJL=new JLabel("请输入新密码:");
pwdJP=new JPasswordField(20);
pwdJL.setFont(new Font(null, Font.PLAIN, 18));
boxH1.add(Box.createHorizontalStrut(80));
boxH1.add(pwdJL);
boxH1.add(pwdJP);
boxH1.add(Box.createHorizontalStrut(80));
pwd1JL =new JLabel("再次输入密码:");
pwd1JL.setFont(new Font(null, Font.PLAIN, 18));
pwd1JP =new JPasswordField(20);
boxH2.add(Box.createHorizontalStrut(80));
boxH2.add(pwd1JL);
boxH2.add(pwd1JP);
boxH2.add(Box.createHorizontalStrut(80));
subJB =new JButton("确定");
subJB.setName("subJB");
subJB.addMouseListener(this);
relJB =new JButton("重置");
relJB.setName("relJB");
relJB.addMouseListener(this);
boxH3.add(Box.createHorizontalStrut(40));
boxH3.add(subJB);
boxH3.add(Box.createHorizontalStrut(40));
boxH3.add(relJB);
boxV.add(boxH);
boxV.add(Box.createVerticalStrut(50));
boxV.add(boxH4);
boxV.add(Box.createVerticalStrut(40));
boxV.add(boxH1);
boxV.add(Box.createVerticalStrut(40));
boxV.add(boxH2);
boxV.add(Box.createVerticalStrut(40));
boxV.add(boxH3);
boxV.add(Box.createVerticalStrut(100));
this.add(boxV);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(img, 0, 0,this.getWidth(), this.getHeight(), this);
}
@Override
public void mouseClicked(MouseEvent e) {
String str=e.getComponent().getName();
String pwd2=new String(pwd2JP.getPassword());
String pwd= new String(pwdJP.getPassword());
String pwd1= new String(pwd1JP.getPassword());
if(str.equals("subJB")) {
if (pwd2.equals(user.getPwd())) {
if (pwd.equals(pwd1)) {
if (pwd2.equals("")||pwd.equals("") || pwd1.equals("")) {
JOptionPane.showMessageDialog(null, "密码不能为空!");
pwdJP.setText("");
pwd1JP.setText("");
pwd2JP.setText("");
} else {
user.setPwd(pwd);
UserController userController =new UserController();
boolean flag =userController.updatePwd(user.getTel(),pwd2,pwd);
if (flag){
JOptionPane.showMessageDialog(null, "修改成功!");
}
pwdJP.setText("");
pwd1JP.setText("");
pwd2JP.setText("");
}
} else {
JOptionPane.showMessageDialog(null, "修改失败,两次密码不一致!");
pwdJP.setText("");
pwd1JP.setText("");
pwd2JP.setText("");
}
}else {
JOptionPane.showMessageDialog(null, "修改失败,原密码错误!");
pwdJP.setText("");
pwd1JP.setText("");
pwd2JP.setText("");
}
}
if(str.equals("relJB")){
pwdJP.setText("");
pwd1JP.setText("");
}
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
}
UserLoginView
package com.baizhi.view;
import com.baizhi.controller.UserController;
import com.baizhi.entity.User;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class UserLoginView extends JFrame implements MouseListener {
private JPanel jp;
private JLabel top;
private JPanel bom;
private JLabel nameJL,pwdJL;
private JTextField nameJF;
private JPasswordField pwdJF;
private JButton login,reg;
public UserLoginView(){
jp =new JPanel();
jp.setLayout(new BorderLayout());
top=new JLabel();
ImageIcon icon =new ImageIcon("img/logo.png");
icon.setImage(icon.getImage().getScaledInstance(300,50, Image.SCALE_DEFAULT));
top.setIcon(icon);
top.setBorder(new EmptyBorder(20,70,20,30));
jp.add(top,BorderLayout.NORTH);
bom =new JPanel();
bom.setLayout(new GridLayout(3,2,20,20));
bom.setBorder(new EmptyBorder(0,40,30,40));
// jp.setLayout(new GridLayout(2,3,20,20));
nameJL =new JLabel("手机号:",JLabel.RIGHT);
nameJL.setIcon(new ImageIcon("img/username_icon.png"));
nameJL.setBorder(new EmptyBorder(0,0,0,20));
nameJF = new JTextField(20);
pwdJL=new JLabel("密 码:",JLabel.RIGHT);
pwdJL.setIcon(new ImageIcon("img/password_icon.png"));
pwdJL.setBorder(new EmptyBorder(0,0,0,20));
pwdJF=new JPasswordField(20);
login =new JButton("登录");
login.setName("login");
login.addMouseListener(this);
reg=new JButton("注册");
reg.setName("reg");
reg.addMouseListener(this);
bom.add(nameJL);
bom.add(nameJF);
bom.add(pwdJL);
bom.add(pwdJF);
bom.add(login);
bom.add(reg);
jp.add(bom,BorderLayout.CENTER);
this.add(jp);
this.setSize(450,300);
this.setLocationRelativeTo(null);
this.setVisible(true);
this.setTitle("百知转账管理系统");
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void mouseClicked(MouseEvent e) {
String str =e.getComponent().getName();
System.out.println(str);
if(str.equals("login")){
String tel =nameJF.getText();
String pwd =new String(pwdJF.getPassword());
if(tel.equals("")){
JOptionPane.showConfirmDialog(null,"手机号不能为空");
}else if(pwdJF.equals("")){
JOptionPane.showConfirmDialog(null,"密码不能为空");
}else{
UserController userController =new UserController();
User user=userController.userLog(tel,pwd);
if (user==null){
JOptionPane.showMessageDialog(null,"登录失败!");
}else {
System.out.println("成功进入主页面");
MainView mainView=new MainView(user);
dispose();
}
}
}else {
AddUserView ad=new AddUserView(this);
}
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
}
UsersView
package com.baizhi.view;
import com.baizhi.entity.Banks;
import com.baizhi.entity.User;
import javax.swing.*;
import java.awt.event.MouseListener;
import java.util.List;
public class UsersView extends JPanel {
// String name, String cardNum, String tel, String pwd, String email, double money, String relName
java.util.List<Banks> List;
public UsersView(List<User> list){
//System.out.println(list.size());
JScrollPane js =new JScrollPane();
Object[] clum = {
"昵称","身份证号","电话号码","邮箱","余额","真实姓名"};
Object[][] data=new Object[list.size()][6];
for (int i=0;i<list.size();i++){
User user =list.get(i);
data[i][0]=user.getName();
data[i][1]=user.getCardNum();
data[i][2]=user.getTel();
data[i][3]=user.getEmail();
data[i][4]=user.getMoney();
data[i][5]=user.getRelName();
}
JTable jTable =new JTable(data,clum);
js.setViewportView(jTable);
this.add(js);
}
}