⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bamclient.java

📁 银行账户管理系统 简称BAM(项目介绍及源码)
💻 JAVA
字号:
package client;

import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import model.Account;
import model.Message;

public class BAMClient {
	CardLayout layout = new CardLayout();;
	JFrame bamFrame;
	MainPanel mainPanel = new MainPanel();// 主面板
	JPanel leftPanel = new JPanel();// 主面板中的---左面板
	JPanel rightPanel = new JPanel();// 主面板中的---右面板

	PicturePanel picturePanel=new PicturePanel();// 图片界面
	LoginPanel loginPanel = new LoginPanel();// 登陆界面
	RegisterPanel registerPanel = new RegisterPanel();// 注册界面
	BusinessPanel businessPanel = new BusinessPanel();// 银行账户操作界面
	LoginModifyPanel loginModifyPanel = new LoginModifyPanel();// 登陆修改界面
	ModifyPanel  modifyPanel = new ModifyPanel();// 修改用户信息界面
	

	Account account;// 定义账户

	ObjectOutputStream out;// 输出流
	ObjectInputStream in;// 输入流

	public BAMClient(){

		// 定义布局
		BorderLayout mainLayout = new BorderLayout();
		// 左布局
		leftPanel.add(mainPanel);
		leftPanel.setBackground(Color.WHITE);// 左边背影色

		// 右布局(有以下3个卡片)
		rightPanel.setLayout(layout);
		rightPanel.add("cardNull", picturePanel);// 卡片一 (空界面)
		rightPanel.add("cardRegister", registerPanel);// 卡片一 (注册界面)
		rightPanel.add("cardLogin", loginPanel);// 卡片二 (登陆界面)
		rightPanel.add("cardBusiness", businessPanel);// 卡片三 (操作界面)
		rightPanel.add("cardLoginModifyPanel", loginModifyPanel);// 卡片四 (登陆修改界面)
		rightPanel.add("cardModifyPanel", modifyPanel);// 卡片五 (修改用户信息界面)

		// 定义主窗体
		bamFrame = new JFrame("Bank Account Management System 银行账户管理系统");
		bamFrame.setLayout(mainLayout);
		bamFrame.setSize(650, 400);// 设定窗体大小
		// 给窗体加面板
		bamFrame.add(leftPanel, BorderLayout.WEST);
		bamFrame.add(rightPanel, BorderLayout.CENTER);
		addListeners();// 调用监听方法
		bamFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//bamFrame.pack();
		bamFrame.setVisible(true);
		
		Socket s;
		try {
			s = new Socket("127.0.0.1",9001);
			out=new ObjectOutputStream(s.getOutputStream());
			in=new ObjectInputStream(s.getInputStream());
			
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	// 窗体中部分对象监听方法
	private void addListeners() {
		// 在窗体的左容器上点《注册》按钮
		mainPanel.getRegisterButton().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				layout.show(rightPanel, "cardRegister");
			}

		});

		// 在窗体的左容器上点《登陆》按钮
		mainPanel.getLoginButton().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				layout.show(rightPanel, "cardLogin");
			}

		});
		
		// 在窗体的左容器上点《修改》按钮
		mainPanel.getModifyButton().addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent arg0) {
				layout.show(rightPanel, "cardLoginModifyPanel");
				
			}
			
		});
		// 在窗体的左容器上点《退出》按钮
		mainPanel.getExitButton().addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				System.exit(0);//退出系统
				
			}
			
		});
		// 在登录界面上点击《提交》按钮
		loginPanel.getOkButton().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				login();//登录
			}
		});
		
		// 在登录界面上点击《返回》按钮
		loginPanel.getBackButton().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// layout.show(rightPanel,"cardBusiness");
				layout.show(rightPanel, "cardNull");
			}
		});

		// 在操作界面上点击《返回》按钮
		registerPanel.getBackButton().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				layout.show(rightPanel, "cardNull");
			}
		});
		
		
		
		
		//************************************************************
		
		// 在开户界面上点击《确定》按钮
		registerPanel.getOkButton().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				register();//开户
			}
		});
		
		
		// 在交易界面上点击提交按钮
		businessPanel.getOkButton().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				business();//交易
			}
		});
		//在交易界面上点击返回按钮
		businessPanel.getBackButton().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				layout.show(rightPanel, "cardNull");
			}
		});
		
		//在登陆修改信息界面点返回
		 loginModifyPanel .getBackButton().addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent arg0) {
				layout.show(rightPanel, "cardNull");
				
			}			 
		 });
		 
			//在登陆修改信息界面点提交
		 loginModifyPanel .getOkButton().addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent arg0) {
				modifyLogin();
				
			}			 
		 });

	}
	
	//登陆修改账户信息界面
	private void modifyLogin(){
		// 判断ID, 和密码不能为空
		if ( loginModifyPanel.getAccountIdText().getText().equals(""))
			JOptionPane.showMessageDialog( loginModifyPanel, "ID不能为空");
		else if (String.valueOf( loginModifyPanel.getPasswordText().getPassword())
				.equals(""))
			JOptionPane.showMessageDialog( loginModifyPanel, "密码不能为空");
		else {
			// 从LoginPanel中得到信息
			String id =  loginModifyPanel.getAccountIdText().getText();
			String pass = String.valueOf( loginModifyPanel.getPasswordText()
					.getPassword());
			Message m = new Message();
			
			
			m.setType(2);
			m.put("Id", id);
			m.put("Password", pass);
			try {
				out.writeObject(m);
				out.flush();
				Object o = in.readObject();
				if (o instanceof Account) {
					account = (Account) o;

					JOptionPane.showMessageDialog( loginModifyPanel, "登录修改界面成功!~~");
					// 用来根据账户的类型设置组件,不需要的组件变灰
					modifyPanel.initComponent(account);
					//modifyPanel.setText(account);
					layout.show(rightPanel,"cardModifyPanel");
				} else {
					JOptionPane.showMessageDialog( loginModifyPanel, o);
				}

			} catch (Exception e) {
			}
		}
	}
	//修改账户信息
	private void modify(){
		
	}
	
	// 开户,注意设置account属性
	private void register() {

		// 从RegisterPanel中得到信息
		String pass1 = String.valueOf(registerPanel.getPassField1().getPassword());
		String pass2 = String.valueOf(registerPanel.getPassField2().getPassword());
		if (pass1.equals(pass2)){
				String name = registerPanel.getNameField().getText();
				String personId = registerPanel.getPersonIdField().getText();
				String email = registerPanel.getEmailField().getText();
				int type = registerPanel.getTypeBox().getSelectedIndex();
				type=type+1;
				Message m=new Message();
				m.setType(0);
				m.put("AccountType",type+"");
				m.put("Password1",pass1);
				m.put("Password2",pass2);
				m.put("Name",name);
				m.put("PersonId",personId);
				m.put("Email",email);
				
				try {
					out.writeObject(m);
					out.flush();
					Object o=in.readObject();
					if (o instanceof Account){
						account=(Account)o;
						JOptionPane.showMessageDialog(registerPanel, "注册成功!^_^");
						// 用来根据账户的类型设置组件,不需要的组件变灰
						businessPanel.initComponent(account);
						businessPanel.setText(account);
						layout.show(rightPanel,"cardBusiness");			
					}
					else if(o==null){
						JOptionPane.showMessageDialog(registerPanel, o);
					}
				} catch (Exception e) {
				}
		}else{
			JOptionPane.showMessageDialog(registerPanel, "晕,二次密码不一至!开户失败^_^");
		}
		// 把RegisterPane清空
		registerPanel.getPassField1().setText("");
		registerPanel.getPassField2().setText("");
		registerPanel.getNameField().setText("");
		registerPanel.getPersonIdField().setText("");
		registerPanel.getEmailField().setText("");
		registerPanel.getTypeBox().setSelectedIndex(0);

	}


	// 登录,注意设置account属性
	private void login() {
		// 判断ID, 和密码不能为空
		if (loginPanel.getAccountIdText().getText().equals(""))
			JOptionPane.showMessageDialog(loginPanel, "ID不能为空");
		else if (String.valueOf(loginPanel.getPasswordText().getPassword())
				.equals(""))
			JOptionPane.showMessageDialog(loginPanel, "密码不能为空");
		else {
			// 从LoginPanel中得到信息
			String id = loginPanel.getAccountIdText().getText();
			String pass = String.valueOf(loginPanel.getPasswordText()
					.getPassword());
			Message m = new Message();
			m.setType(1);
			m.put("Id", id);
			m.put("Password", pass);
			try {
				out.writeObject(m);
				out.flush();
				Object o = in.readObject();
				if (o instanceof Account) {
					account = (Account) o;

					JOptionPane.showMessageDialog(loginPanel, "登录成功!~~");
					// 用来根据账户的类型设置组件,不需要的组件变灰
					businessPanel.initComponent(account);
					businessPanel.setText(account);
					layout.show(rightPanel,"cardBusiness");
				} else {
					JOptionPane.showMessageDialog(loginPanel, o);
				}

			} catch (Exception e) {
			}
		}
	}
	
	// 交易
	private void business() {
		String action = businessPanel.getChoiceBox().getSelectedItem().toString();
		String money = businessPanel.getInputField().getText();
		if (money.length()==0) money="0";
		// 得到当前用户ID
		long id = account.getId();
		Message m=new Message();
		m.setType(2);
		m.put("Id",id+"");
		m.put("Money",money);
		if (action.equals("存款")){
			m.put("BusinessType","0");
		}
		else if (action.equals("取款")){
			m.put("BusinessType","1");
		}	
		else if (action.equals("设置透支额度")){
			m.put("BusinessType","2");			
		}
		else if(action.equals("申请贷款")){
			m.put("BusinessType","3");
		}
		else{
			m.put("BusinessType","4");
		}
		try{
			out.writeObject(m);
			out.flush();
			Object o=in.readObject();
			if (o instanceof Account){
				account = (Account)o;
				businessPanel.setText(account);
			}
			else{
				JOptionPane.showMessageDialog(loginPanel,o);
			}
		}
		catch(Exception e){}
	}
	public static void main(String[] args) {
		new BAMClient();
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -