JAVA Swing ATM模拟

小编最近在复习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,"账户错误");
       }
    }



}

功能栏

  • 10
    点赞
  • 68
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
JavaATM模拟图形化可以通过Java Swing GUI库来实现。下面是一些实现的步骤: 1. 创建一个Java Swing应用程序。 2. 创建一个JFrame来承载ATM模拟器的用户界面。 3. 在JFrame中添加一个JPanel来承载ATM模拟器的主要用户界面元素。 4. 在JPanel中创建各种Swing组件,例如按钮、标签、文本框等,用于与用户交互。 5. 创建一个ATM类来模拟ATM的行为,例如验证用户的PIN码、显示余额、提取现金等。 6. 在JPanel中添加事件监听器,用于捕获用户与ATM模拟器的交互。 7. 在事件监听器中调用ATM类的方法来处理用户请求并显示相应的结果。 下面是一个简单的JavaATM模拟器的代码示例,供参考: ```java import javax.swing.*; import java.awt.*; import java.awt.event.*; public class JavaATM extends JFrame { private static final long serialVersionUID = 1L; private JPanel panel; private JTextField pinField; private JTextField amountField; private JPasswordField passwordField; private JTextArea resultArea; private JButton loginButton; private JButton withdrawButton; private JButton depositButton; private JButton balanceButton; private ATM atm; public JavaATM() { setTitle("Java ATM"); setSize(400, 300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new BorderLayout()); panel = new JPanel(); panel.setLayout(new GridLayout(5, 2)); add(panel, BorderLayout.CENTER); JLabel pinLabel = new JLabel("Enter PIN:"); panel.add(pinLabel); pinField = new JTextField(); panel.add(pinField); JLabel passwordLabel = new JLabel("Enter Password:"); panel.add(passwordLabel); passwordField = new JPasswordField(); panel.add(passwordField); JLabel amountLabel = new JLabel("Enter Amount:"); panel.add(amountLabel); amountField = new JTextField(); panel.add(amountField); loginButton = new JButton("Login"); panel.add(loginButton); loginButton.addActionListener(new LoginListener()); withdrawButton = new JButton("Withdraw"); panel.add(withdrawButton); withdrawButton.addActionListener(new WithdrawListener()); depositButton = new JButton("Deposit"); panel.add(depositButton); depositButton.addActionListener(new DepositListener()); balanceButton = new JButton("Balance"); panel.add(balanceButton); balanceButton.addActionListener(new BalanceListener()); resultArea = new JTextArea(); add(resultArea, BorderLayout.SOUTH); atm = new ATM(); } private class LoginListener implements ActionListener { public void actionPerformed(ActionEvent event) { String pin = pinField.getText(); String password = new String(passwordField.getPassword()); boolean success = atm.login(pin, password); if (success) { resultArea.setText("Login successful."); } else { resultArea.setText("Login failed."); } } } private class WithdrawListener implements ActionListener { public void actionPerformed(ActionEvent event) { String amount = amountField.getText(); boolean success = atm.withdraw(amount); if (success) { resultArea.setText("Withdrawal successful."); } else { resultArea.setText("Withdrawal failed."); } } } private class DepositListener implements ActionListener { public void actionPerformed(ActionEvent event) { String amount = amountField.getText(); boolean success = atm.deposit(amount); if (success) { resultArea.setText("Deposit successful."); } else { resultArea.setText("Deposit failed."); } } } private class BalanceListener implements ActionListener { public void actionPerformed(ActionEvent event) { String balance = atm.getBalance(); resultArea.setText("Balance: " + balance); } } public static void main(String[] args) { JavaATM atm = new JavaATM(); atm.setVisible(true); } } class ATM { private String pin; private String password; private double balance; public ATM() { pin = "1234"; password = "abcd"; balance = 1000.0; } public boolean login(String inputPin, String inputPassword) { if (inputPin.equals(pin) && inputPassword.equals(password)) { return true; } else { return false; } } public boolean withdraw(String amountString) { try { double amount = Double.parseDouble(amountString); if (amount > balance) { return false; } balance -= amount; return true; } catch (NumberFormatException e) { return false; } } public boolean deposit(String amountString) { try { double amount = Double.parseDouble(amountString); balance += amount; return true; } catch (NumberFormatException e) { return false; } } public String getBalance() { return String.format("%.2f", balance); } } ``` 上述代码实现了一个简单的ATM模拟器,用于演示如何利用Java Swing GUI库来创建ATM模拟器的用户界面。具体地,用户可以输入PIN码和密码来登录ATM账户,然后可以输入金额来进行取款、存款或查询余额。ATM类实现了ATM的核心逻辑,例如验证用户的PIN码、显示余额、提取现金等。事件监听器用于捕获用户与ATM模拟器的交互,并调用ATM类的方法来处理用户请求并显示相应的结果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值