小编最近在复习java基础,心血来潮学了下Swing(以前没学过),写了一个模拟ATM取款程序。
ATM模拟取款程序:
登录界面:
图片中小编还加入了一个 时间实时更新功能。
Login类(登录)
package swpu.edu;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* 功能描述: 登录界面
* @param:
* @return:
* @auther:
* @date:
*/
public class Login extends JFrame implements ActionListener{
private JPanel p1,p2,p3,p4; //界面元素对象
private JTextField username,userpassword;//接收界面账号、密码
private JButton login;//登录按钮
private Timer timer;//时间容器
private JLabel timelable;
public Login(){
//主界面
this.setTitle("ATM模拟");
this.setSize(500,300);
this.setLocation(500,200);//界面显示位置
this.setLayout(null);
this.setResizable(false);//界面不可缩放
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭界面,程序结束
//界面元素1:标题
p1=new JPanel();
p1.setBounds(140,30,200,30);//界面位置、大小
p1.setBackground(new Color(55,100,97));
this.add(p1);
p1.add(new JLabel("万氏银行"));
//界面元素2:账号
p2=new JPanel();
p2.setBounds(80,80,300,30);
this.add(p2);
p2.add(new JLabel("账号: "));
username=new JTextField(20);
p2.add(username);
//界面元素3:密码
p3=new JPanel();
p3.setBounds(80,120,300,30);
this.add(p3);
p3.add(new JLabel("密码: "));
userpassword=new JPasswordField(20);
p3.add(userpassword);
//界面元素4:登录按钮
login=new JButton("登录");
login.addActionListener(this);//触发监听事件,验证用户信息
p4=new JPanel();
p4.setBounds(90,160,300,30);
this.add(p4);
p4.add(login);
//时间显示
timelable=new JLabel("");
timelable.setBounds(300,220,200,30);
timer=new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
timelable.setText(new SimpleDateFormat("yyyy年MM月dd日:hh:mm:ss")
.format(new Date()));
}
});
timer.start();
this.add(timelable);
this.setVisible(true);//界面可见
}
//测试
public static void main(String[] args) {
new Login();
}
@Override
public void actionPerformed(ActionEvent e) {
new Account();//调用Account构造器,读取文件信息
System.out.println(username.getText().toString());
System.out.println(Account.userid);
if(username.getText().toString().equals(Account.userid)) {
if (userpassword.getText().toString().equals(Account.userpassword)) {
this.dispose();//释放当前界面资源,关闭当前界面
new Menu();
}
else if(userpassword.getText().toString().equals("")){
JOptionPane.showMessageDialog(this,"账户或密码不能为空");
}else{
JOptionPane.showMessageDialog(this,"密码错误");
}
}else if(username.getText().toString().equals("")){
JOptionPane.showMessageDialog(this,"账户或密码不能为空");
}else{
JOptionPane.showMessageDialog(this,"账户错误");
}
}
}
package swpu.edu;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* @Auther: wj
* @Date: 2019/9/3
* @Description: ATM功能栏
* @version: 1.0
*/
public class Menu extends JFrame implements ActionListener{
private JPanel backgroud1,backgroud2;//背景设置
private JButton saving,withdrawal,select,transfer,resetpassword,back;
// 存款 取款 查询 转账 修改密码 退出
public Menu(){
this.setTitle("万氏银行");
this.setSize(500,300);//界面大小
this.setLocation(500,200);//界面显示位置
this.setLayout(null);
this.setResizable(false);//界面不可缩放
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭界面,程序结束
//背景设置1:标题
backgroud1=new JPanel();
backgroud1.setBackground(new Color(222,210,218));
backgroud1.setBounds(0,0,500,30);
this.add(backgroud1);
backgroud1.add(new JLabel("请选择你需要的服务"));
//背景设置2:内容
backgroud2=new JPanel();
backgroud2.setBackground(new Color(222,156,104));
backgroud2.setBounds(0,30,500,270);
this.add(backgroud2);
//实例化按钮
saving=new JButton("存款");
withdrawal=new JButton("取款");
transfer=new JButton("转账");
select =new JButton("查询");
resetpassword=new JButton("修改密码");
back=new JButton("退出");
//按钮位置设置
/*saving.setBounds(25,30,100,30);
withdrawal.setBounds(360,30,100,30);
select.setBounds(25,90,100,30);
transfer.setBounds(360,90,100,30);
resetpassword.setBounds(25,150,100,30);
back.setBounds(360,150,100,30);*/
//将按钮添加到内容界面中
backgroud2.add(saving);
backgroud2.add(withdrawal);
backgroud2.add(transfer);
backgroud2.add(select);
backgroud2.add(resetpassword);
backgroud2.add(back);
//按钮增加监听事件
saving.addActionListener(this);
withdrawal.addActionListener(this);
select.addActionListener(this);
transfer.addActionListener(this);
resetpassword.addActionListener(this);
back.addActionListener(this);
this.setVisible(true);//界面可视化
}
// public static void main(String[] args) {
// new Menu();
// }
//监听事件处理
@Override
public void actionPerformed(ActionEvent e) {
Object source=e.getSource();
if(source==saving){
this.dispose();//关闭当前界面,释放资源
new Saving();//跳转账户存储界面
}else if(source==withdrawal){
this.dispose();
new WithDrawal();//跳转账户取款界面
}else if(source==select){
this.dispose();
new Select();//跳转记录页面
}else if(source==transfer){
JOptionPane.showMessageDialog(this,"私人账户,无法转账");
}else if(source==resetpassword){
this.dispose();
new Resetpassword();
}else if(source==back){
this.dispose();
new Login();
}
}
}
package swpu.edu;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* @Auther: wj
* @Date: 2019/9/5
* @Description: 存款界面操作
* @version: 1.0
*/
public class Saving extends JFrame implements ActionListener{
private JPanel pan1,pan2,pan3;
private JTextField saving;
private JButton login,back;
public Saving(){
this.setTitle("存款");
this.setSize(500,300);
this.setLocation(500,200);
this.setLayout(null);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//账户id显示栏
pan1=new JPanel();
pan1.setBounds(20,20,500,30);
pan1.setLayout(new FlowLayout(FlowLayout.LEFT));
pan1.add(new JLabel(" 账户名:"+Account.userid));
this.add(pan1);
//账户余额显示栏
pan2=new JPanel();
pan2.setBounds(20,50,500,30);
pan2.setLayout(new FlowLayout(FlowLayout.LEFT));
pan2.add(new JLabel("账户余额:"+Account.money));
this.add(pan2);
//用户操作栏
pan3=new JPanel();
pan3.setBounds(20,120,400,30);
pan3.add(new JLabel("金额(元):"));
saving=new JTextField(20);
pan3.add(saving);
this.add(pan3);
//提交按钮
login=new JButton("存款");
login.addActionListener(this);
login.setBounds(150,180,80,30);
this.add(login);
//返回按钮
back=new JButton("返回");
back.addActionListener(this);
back.setBounds(250,180,80,30);
this.add(back);
this.setVisible(true);
}
public static void main(String[] args) {
new Saving();
}
@Override
public void actionPerformed(ActionEvent e) {
Object source=e.getSource();
if(source==login){
Account.saving(saving);
JOptionPane.showMessageDialog(this,"存款成功");
Account.in(saving);//录入存款记录
this.dispose();
new Menu();
}else if(source==back){
this.dispose();
JOptionPane.showMessageDialog(this,"返回成功");
new Menu();
}
}
}
取款和存款大同小异,就不放图片了,直接上代码
WithDrawal.java(取款类)
package swpu.edu;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* @Auther: wj
* @Date: 2019/9/7
* @Description: 取款
* @version: 1.0
*/
public class WithDrawal extends JFrame implements ActionListener{
private JPanel pan1,pan2,pan3;
private JTextField withDrawal;
private JButton login,back;
public WithDrawal(){
this.setTitle("取款");
this.setSize(500,300);
this.setLocation(500,200);
this.setLayout(null);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//账户id显示栏
pan1=new JPanel();
pan1.setBounds(20,20,500,30);
pan1.setLayout(new FlowLayout(FlowLayout.LEFT));
pan1.add(new JLabel(" 账户名:"+Account.userid));
this.add(pan1);
//账户余额显示栏
pan2=new JPanel();
pan2.setBounds(20,50,500,30);
pan2.setLayout(new FlowLayout(FlowLayout.LEFT));
pan2.add(new JLabel("账户余额:"+Account.money));
this.add(pan2);
//用户操作栏
pan3=new JPanel();
pan3.setBounds(20,120,400,30);
pan3.add(new JLabel("金额(元):"));
withDrawal=new JTextField(20);
pan3.add(withDrawal);
this.add(pan3);
//提交按钮
login=new JButton("取款");
login.addActionListener(this);
login.setBounds(150,180,80,30);
this.add(login);
//返回按钮
back=new JButton("返回");
back.addActionListener(this);
back.setBounds(250,180,80,30);
this.add(back);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
Object source=e.getSource();
if(source==login){
int flag=Account.withDrawal(withDrawal);
if(flag==0){
this.dispose();
new WithDrawal();
}else{
JOptionPane.showMessageDialog(this,"取款成功");
Account.out(withDrawal);//录入取款记录
this.dispose();
new Menu();
}
}else if(source==back){
JOptionPane.showMessageDialog(this,"返回成功");
this.dispose();
new Menu();
}
}
}
package swpu.edu;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* @Auther: wj
* @Date: 2019/9/8
* @Description: 查询
* @version: 1.0
*/
public class Select extends JFrame implements ActionListener{
private JPanel pan1,pan2;
private JScrollPane pan3;
private JTextArea select;
private JButton back;
public Select(){
this.setTitle("查询");
this.setSize(500,300);
this.setLocation(500,200);
this.setLayout(null);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//账户id显示栏
pan1=new JPanel();
pan1.setBounds(20,20,500,30);
pan1.setLayout(new FlowLayout(FlowLayout.LEFT));
pan1.add(new JLabel(" 账户名:"+Account.userid));
this.add(pan1);
//账户余额显示栏
pan2=new JPanel();
pan2.setBounds(20,50,500,30);
pan2.setLayout(new FlowLayout(FlowLayout.LEFT));
pan2.add(new JLabel("账户余额:"+Account.money));
this.add(pan2);
//记录录入到Area中
select=new JTextArea("用户操作记录如下:\n"+Account.select());
pan3=new JScrollPane(select);
pan3.setBounds(0, 80, 500, 130);
select.setEditable(false);
this.add(pan3);
//返回菜单
back=new JButton("返回");
back.setBounds(180,210,80,30);
back.addActionListener(this);
this.add(back);
this.setVisible(true);
}
public static void main(String[] args) {
new Select();
}
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(this,"返回成功");
this.dispose();
new Menu();
}
}
最后一个,用户后台操作
Account.java(用户类)
package swpu.edu;
import javax.swing.*;
import java.io.*;
/**
* @Auther: wj
* @Date: 2019/9/3
* @Description: 用户信息读取
* @version: 1.0
*/
public class Account extends JFrame{
static String userid;//用户账号
static String userpassword;//用户密码
static String money;//用户存款
static FileReader fr;//文件输出流
static FileWriter fw;//文件输入流
public static String[] len=new String[20];// 储存读取的信息
public Account(){
Read();
}
//读取账户信息
public static void Read() {
try {
fr=new FileReader("D://PeopleInfo.txt");
BufferedReader bfr=new BufferedReader(fr); //缓存读取,加快读取速度
for(int i=0;i<3;i++){
while((len[i]=bfr.readLine())=="/n"){
continue;
}
}
userid=len[0];
userpassword=len[1];
money=len[2];
// System.out.println(userid+":"+userpassword+":"+money);
} catch (Exception e) {
e.printStackTrace();
}
}
//存取账户信息
public static void write(){
len[2]=money;
len[1]=userpassword;
len[0]=userid;
try {
fw=new FileWriter("D://PeopleInfo.txt");
for (int i=0;i<3;i++){
fw.write(len[i]+"\n");
}
fw.flush();//刷新数据,进入文档
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//记录存款操作
public static void in(JTextField in){
try {
fw=new FileWriter("D://info.txt",true);
fw.write("用户:"+userid+"存款:"+in.getText().toString()+"元\n");
fw.flush();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//记录取款操作
public static void out(JTextField out){
try {
fw=new FileWriter("D://info.txt",true);
fw.write("用户:"+userid+"取款:"+out.getText().toString()+"元\n");
fw.flush();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//存款操作
public static void saving(JTextField saving){
money= String.valueOf((Integer.parseInt(money)+
Integer.parseInt(saving.getText().toString())));
write();
}
//取款操作
public static int withDrawal(JTextField withDrawal){
if(Integer.parseInt(withDrawal.getText().toString())>Integer.parseInt(money)){
JOptionPane.showMessageDialog(new WithDrawal(), "金额错误,请重新输入");
return 0;
}
else{
money= String.valueOf((Integer.parseInt(money)-
Integer.parseInt(withDrawal.getText().toString())));
write();
}
return 1;
}
//查询操作
public static String select(){
try {
fr=new FileReader("D://info.txt");
BufferedReader bfr=new BufferedReader(fr);
StringBuffer sb=new StringBuffer();
String str1,str2;
while((str1=bfr.readLine())!=null){
sb.append(str1+"\n");
}
str2=sb.toString();
return str2;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
//密码修改
public static int resetPassword(JTextField pwd1,JTextField pwd2){
if(pwd1.getText().toString().equals(pwd2.getText().toString())){
userpassword=pwd1.getText().toString();
write();
return 1;
}else{
return 0;
}
}
}
一个良好的编码风格就是要有详细注释,小编在代码中是由详细注释的,希望每个读者都能看懂代码表达的含义.
如果有什么问题,大家一起交流呀.