Java模拟ATM取款机(access数据库连接,设置背景图片)

Java swing模拟ATM取款机,pc程序(附带access数据库,设置背景图片)

如有转载请注明出处

  1. access数据库连接
  2. ATM主程序及代码,包括设置背景图片
  3. ATM功能主页程序及代码

具体实现

1、access数据库连接的前提是你电脑上有office自带的Access数据库软件,我用的是Access 2016,如下图:
在这里插入图片描述
首先创建一个后缀为.mdb的数据库,然后利用专用的access数据库连接,在此没有采用微软的桥接器。但是用专用的连接器需要引入包,为了大家方便,在此放出百度云链接: https://pan.baidu.com/s/1hknHpqmdHNobVf1L5y5mvA 提取码: ykge 让大家下载Access_JDBC30.jar包,
在这里插入图片描述
然后再项目里创建个lib文件夹,把包粘贴带文件夹里,然后右键jar包构建路径,就完成了包的引入。我把数据库文件放到了和连接数据库文件同一个包中。
在这里插入图片描述
连接数据库代码

package database;

import java.sql.*;
public class conn_db {
	Connection con;
	String url=null;
	public void connection() throws ClassNotFoundException{
		url="jdbc:Access://src/database/data.mdb";
		try {
			Class.forName("com.hxtt.sql.access.AccessDriver");
			con=DriverManager.getConnection(url);
		}catch(Exception e) {
			e.printStackTrace();
		}
	}

}

2、主程序代码
在这里插入图片描述
上边这个是主页面
下面是主页面代码

package interfaces;
import java.awt.Color;
import java.awt.Font;
import javax.swing.*;
import database.login_db;
public class mainface extends login_db{
	JFrame f;
	JTextField text1;
	JPasswordField text2;
	JLabel lab1,lab2,lab3,lab4;
	JButton but1;
	JPanel jpanel;
	ImageIcon background;
	public mainface() {
		f=new JFrame("ATM Platform");
		f.setLayout(null);
		lab1=new JLabel("username:",JLabel.RIGHT);
		lab2=new JLabel("password:",JLabel.RIGHT);
		lab1.setFont(new Font("黑体",Font.PLAIN,18));//设置字体大小,样式
		lab1.setForeground(Color.WHITE);//设置字体颜色
		lab2.setFont(new Font("黑体",Font.PLAIN,18));
		lab2.setForeground(Color.WHITE);
		jpanel=new JPanel();
		lab4=new JLabel("Welcome to use Anderson Bank",JLabel.CENTER);
		lab4.setFont(new Font("黑体",Font.ITALIC,35));
		lab4.setForeground(Color.blue);
		text1=new JTextField(20);
		text2=new JPasswordField(20);
		but1=new JButton("Sign in");
		lab1.setBounds(0, 190, 90, 30);
		lab2.setBounds(0, 220, 90, 30);
		lab4.setBounds(0, 0, 600, 100);
		text1.setBounds(100, 190,150, 30);
		text2.setBounds(100, 220, 150, 30);
		but1.setBounds(100, 250, 150,30);
		jpanel.add(lab1);
		jpanel.add(lab2);
		jpanel.add(lab4);
		jpanel.add(text1);
		jpanel.add(text2);
		jpanel.add(but1);
		jpanel.add(lab3);
		f.setVisible(true);
		f.setResizable(false);
		f.setSize(600, 350);
		f.setLocation(800, 400);
		but1.addActionListener(e->{
			if(text1.getText().equals(""))	
				JOptionPane.showMessageDialog(null, "Please input account");
			else if(text2.getPassword().equals(""))
				JOptionPane.showMessageDialog(null, "Please input password");
			else{
				String accountT = text1.getText().trim();
				String namesT = new String(text2.getPassword()).trim();
				try {
					connection(); 
					boolean com = compareWithSql(accountT,namesT);//验证登陆
					if(com==true)
						{
						JOptionPane.showMessageDialog(null, "Success");
						new functionface(accountT);//打开新窗口
						f.dispose();//关闭本窗口
						}
					else{
						JOptionPane.showMessageDialog(null, "account or password is incorrect");
						text1.setText("");
						text2.setText("");
					}
				} 
				catch (Exception e1) {
					e1.printStackTrace();
				}
			}
	});
	}
	public static void main(String[] args) {
		new mainface();
	}

}

3、功能程序及代码
在这里插入图片描述
图片里的功能函数都在login_db的文件中
功能函数function.java代码:

package interfaces;
import java.awt.Color;
import java.awt.Font;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import database.login_db;
public class functionface extends login_db{
	private static String cardNum;//传递的用户名,便于操作数据库里的客户,相当于你的银行卡号
	public static double surplus=1000;//设置保留最低金额
	JFrame f;
	JTextField text1,text2;
	JLabel lab1,lab2,lab3;
	JButton but1,but2,but3,but4;
	JPanel jpanel;
	ImageIcon background;
	public functionface(String cardNum){
		super();
		functionface.cardNum = cardNum;//构造器传递用户名
		f=new JFrame("ATM Function");
		f.setLayout(null);
		jpanel=new JPanel();
	
		
		lab1=new JLabel("run result:",JLabel.RIGHT);
		lab1.setFont(new Font("黑体",Font.PLAIN,15));
		lab1.setForeground(Color.yellow);
		lab2=new JLabel("input money:",JLabel.RIGHT);
		lab2.setFont(new Font("黑体",Font.PLAIN,15));
		lab2.setForeground(Color.yellow);
		text1=new JTextField(20);
		text2=new JTextField(20);
		but1=new JButton("query");
		but2=new JButton("take money");
		but3=new JButton("deposit");
		but4=new JButton("exit");
		jpanel.add(lab1);
		jpanel.add(lab2);
		jpanel.add(text1);
		jpanel.add(text2);
		jpanel.add(but1);
		jpanel.add(but2);
		jpanel.add(but3);
		jpanel.add(but4);
		lab1.setBounds(-30, 10, 90, 30);
		lab2.setBounds(80,70, 110, 30);
		text1.setBounds(70, 10, 250, 30);
		text2.setBounds(200, 70,100, 30);
		but1.setBounds(10, 110, 90, 30);
		but2.setBounds(200, 150, 110, 30);
		but3.setBounds(200, 110, 110, 30);
		but4.setBounds(10, 150, 90, 30);
		f.setVisible(true);
		f.setResizable(false);
		f.setSize(350, 300);
		f.setLocation(800, 400);
		but1.addActionListener(e->{
			double count = 0;
			try {
					connection(); 	
					count=query(functionface.cardNum);//查询余额
			}catch(Exception e1){
				e1.printStackTrace();
			}
			text1.setText(""+count);
		});
		but2.addActionListener(e->{
			if(!text2.getText().trim().equals(""))
			{
				double count =Double.parseDouble(text2.getText().trim());//把字符变成double类型
				try {
						connection(); 	
						if(query(functionface.cardNum)-functionface.surplus>0)//判断是否低于最低金额
							{
								takemoney(functionface.cardNum,count);//取钱
								text1.setText("the number of being taken is "+count+"  the last money is"+query(functionface.cardNum));//取完钱并输出取的数目和最终钱数
							}
							else
							JOptionPane.showMessageDialog(null, "the minimal money is "+functionface.surplus);
				}catch(Exception e1){
					e1.printStackTrace();
				}
			}
			else
				JOptionPane.showMessageDialog(null, "Please input money at textfield");
		});
		but3.addActionListener(e->{
			if(!text2.getText().trim().equals(""))
			{
				double count =Double.parseDouble(text2.getText().trim());
				try {
						connection(); 	
						deposit(functionface.cardNum,count);//存钱
						text1.setText("the number of depositing is "+count+"  the last money is"+query(functionface.cardNum));//存完钱并输出存的数目和最终钱数
				}catch(Exception e1){
					e1.printStackTrace();
				}
			}
			else
				JOptionPane.showMessageDialog(null, "Please input money at textfield");
		});
		but4.addActionListener(e->{
			f.dispose();//退出
			new mainface();//返回到主页面,相当于银行卡取出
		});
	}

}

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值