Java图形化界面 -实现简易的ATM服务系统(含源码)


前言

  • 代码与实现效果比较潦草,仅供参考!

实现效果

  • 实现简单的登陆界面可以实现登录
    在这里插入图片描述

  • 提供增删改查的操作
    在这里插入图片描述

代码参考

- 主要类

package Newtest;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import java.util.Properties;
import java.util.Scanner;
import java.util.Vector;

public class ATMsever00 extends JFrame{
    public void init(){
        JFrame f1 = new JFrame("ATM服务界面");
        //DefaultMutableTreeNode d = new DefaultMutableTreeNode();

        JLabel jLabel0 = new JLabel();
        JLabel jLabel1 = new JLabel();
        JLabel jLabel2 = new JLabel();
        //添加 初始化监听类的 对象
        Listener1 listener01 = new Listener1();
        Listener2 listener02 = new Listener2();

        Container container = this.getContentPane();//获取一个内容面板给container
        container.setLayout(null);//绝对布局

        JTextField jTextField01 = new JTextField();/*新建组件*/
        JPasswordField jPasswordField = new JPasswordField();
        container.add(jTextField01);
        //container.add(jTextField02);
        container.add(jPasswordField);
        jTextField01.setBounds(100,90,120,40);
        jTextField01.addActionListener(listener01);
        //jTextField02.setBounds(40,50,100,30);
        jPasswordField.setBounds(100,140,120,40);
        jPasswordField.addActionListener(listener02);

        JButton button = new JButton("登录");
        container.add(button);
        button.setBounds(120,200,100,50);
        //container.setBounds(150,300,10,10);
        button.setBorderPainted(false);
        button.setFont(new  Font("华文行楷",  10,  24));
        button.setBorder(BorderFactory.createRaisedBevelBorder());/*设置凸起的按钮*/

        //设置窗口的基本属性
        setTitle("系统登录界面");
        jLabel0.setText("ATM服务登录界面");
        jLabel0.setForeground(Color.BLUE);
        jLabel0.setFont(new Font("黑体",12,36));
        container.add(jLabel0);
        jLabel0.setBounds(40,20,300,40);
        jLabel1.setText("账 户");///
        jLabel1.setForeground(Color.BLACK);
        jLabel1.setFont(new Font("黑体",12,24));
        container.add(jLabel1);
        jLabel1.setBounds(20,90,300,40);
        jLabel2.setText("密 码");//
        jLabel2.setForeground(Color.BLACK);
        jLabel2.setFont(new Font("黑体",12,24));
        container.add(jLabel2);
        jLabel2.setBounds(20,120,300,40);

        setBounds(60,60,800,800);
        setSize(500,500);
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //监听
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(StringUtil00.isEmpy(jTextField01.getText())){//判断null
                    JOptionPane.showMessageDialog(null, "账号不能为空");
                    return;
                }
                else if(StringUtil00.isEmpy(new String(jPasswordField.getPassword()))){
                    JOptionPane.showMessageDialog(null, "密码不能为空");
                    return;
                }
                else {
                    User00 u=new User00();
                    u.setUser(jTextField01.getText());
                    u.setPassword(new String(jPasswordField.getPassword()));

                    if (u.getUser().equals("root") == true&&u.getPassword().equals("123") == true){
                        System.out.println(u.getUser().equals("root"));
                        System.out.println(u.getPassword().equals("123"));
                    }else {
                        JOptionPane.showMessageDialog(null, "用户名或密码错误");
                        return;
                    }
                }
                if (e.getSource() == button) {
                    System.out.println("正在登录...");
                    new DialogDemo01();
                    System.out.println("欢迎 "+jTextField01.getText()+"会员");
                }
            }}
        );
    }

    public static void main(String[] args) {
        ATMsever00 atMsever = new ATMsever00();
        atMsever.init();
        //new count01().init();
    }
}
class DialogDemo01 extends JDialog{
    public DialogDemo01(){
        JMenu jMenu1 = new JMenu("待定1");
        JMenu jMenu2 = new JMenu("待定2");
        JMenu jMenu3 = new JMenu("待定3");
        JMenuBar jMenuBar = new JMenuBar();
        jMenuBar.add(jMenu1);
        jMenuBar.add(jMenu2);
        jMenuBar.add(jMenu3);

        //--获取、调用数据库连接
        Container container01 = this.getContentPane();
        container01.setLayout(null);
        Vector columnNames;
        columnNames = new Vector(3,5);
        columnNames.add("序列号");
        columnNames.add("用户名");
        columnNames.add("余额");

        JTable jTable = new JTable();
        DefaultTableModel newtablemodel = new DefaultTableModel();
        newtablemodel.setColumnIdentifiers(columnNames);

        container01.add(jTable);
        jTable.setBounds(10,220,500,300);
        container01.add(jMenuBar);
        jMenuBar.setBounds(600,50,200,50);
        JLabel jLabel1 = new JLabel();
        JLabel jLabel2 = new JLabel();
        JLabel jLabel3 = new JLabel();
        jLabel1.setText("欢迎使用本服务!");
        jLabel2.setText("注:现有用户-c1【余额:99999999】、c2【余额:100】");
        jLabel3.setText("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");

        jLabel1.setForeground(Color.RED);
        jLabel1.setFont(new java.awt.Font("黑体",12,36));
        container01.add(jLabel1);
        jLabel1.setBounds(40,20,300,40);

        jLabel2.setForeground(Color.BLUE);
        jLabel2.setFont(new java.awt.Font("黑体",12,18));
        container01.add(jLabel2);
        jLabel2.setBounds(40,100,500,40);

        jLabel3.setForeground(Color.RED);
        jLabel3.setFont(new java.awt.Font("黑体",12,24));
        container01.add(jLabel3);
        jLabel3.setBounds(40,65,500,20);

        JButton button01 = new JButton("新建账户");
        JButton button02 = new JButton("存款");
        JButton button03 = new JButton("取款");
        JButton button04 = new JButton("转账");
        JButton button05 = new JButton("查询 所有表 / 历史");
        JTextField jTextField1 = new JTextField("待输入...01");
        JTextField jTextField201 = new JTextField("待输入-用户名...02");
        JTextField jTextField202 = new JTextField("待输入-金额...02");
        JTextField jTextField301 = new JTextField("待输入用户名...03");
        JTextField jTextField302 = new JTextField("待输入-金额...03");
        JTextField jTextField401 = new JTextField("待输入-收款人...04");
        JTextField jTextField402 = new JTextField("待输入-取款数...04");
        JTextField jTextField5 = new JTextField("用户名...05");

        this.setBounds(60,60,800,800);
        this.setVisible(true);

        button01.addActionListener(new ActionListener() {//新建用户 按钮的监听367-449
            @Override
            public void actionPerformed(ActionEvent e) {

                JButton b1 = (JButton)e.getSource();
                b1.setText(jTextField1.getText());
                System.out.println("新建" + b1.getText());
                System.out.println("正在进行.."+e.getActionCommand());

                Connection conn = null; 
                PreparedStatement ps = null; 
                // 获取Driver实现类对象
                Driver driver = null;
                try {
                    driver = new com.mysql.cj.jdbc.Driver();//MySQL8 ↓
                    //driver = new com.mysql.jdbc.Driver();//MySQL5

                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
                String url = "jdbc:mysql://localhost:3306/demo";
                Properties info = new Properties();
                info.setProperty("user", "root");
                info.setProperty("password", "admin");

                try {
                    conn = driver.connect(url, info);
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
                System.out.println(conn);

                String sql = "insert into atmtest(aname) values(?)";
                try {
                    ps = conn.prepareStatement(sql);

                    ps.setString(1, button01.getText());

                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }

                try {
                    ps.execute();//执行

                    ps.close();
                    conn.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
                b1.setText("新建用户");
            }
        });

        button02.addActionListener(new ActionListener() {//存款
            @Override
            public void actionPerformed(ActionEvent e) {
                String s1 = "",s2 = "";
                s1=jTextField201.getText();
                s2=jTextField202.getText();

                System.out.println("存款" + button02.getText());
                Connection con = null;
                PreparedStatement ps = null;
                Driver driver = null;
                try {
                    driver = new com.mysql.cj.jdbc.Driver();//MySQL8 ↓
                    //driver = new com.mysql.jdbc.Driver();//MySQL5

                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
                String url = "jdbc:mysql://localhost:3306/demo";
                Properties info = new Properties();
                info.setProperty("user", "root");
                info.setProperty("password", "admin");
                Connection conn = null;
                try {
                    conn = driver.connect(url, info);
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
                System.out.println(conn);
                String sql = "update atmtest set anum=anum+? WHERE aname = ?";
                System.out.println("正在对存款" + s2 + "---存款" + s1 + "元/¥");
                try {
                    ps = conn.prepareStatement(sql);
                    ps.setObject(2, s1);
                    ps.setObject(1, s2);
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
                try {
                    ps.execute();//执行
                    ps.close();
                    conn.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        });

        button03.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String s1 = "",s2 = "";
                s1 = jTextField301.getText();
                s2 = jTextField302.getText();
                Connection con = null;
                PreparedStatement ps = null;
                // 获取Driver实现类对象
                Driver driver = null;
                try {
                    driver = new com.mysql.cj.jdbc.Driver();//MySQL8 ↓
                    //driver = new com.mysql.jdbc.Driver();//MySQL5
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }

                String url = "jdbc:mysql://localhost:3306/demo";
                Properties info = new Properties();
                info.setProperty("user", "root");
                info.setProperty("password", "admin");
                Connection conn = null;
                try {
                    conn = driver.connect(url, info);
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
                System.out.println(conn);

                String sql = "update atmtest set anum=anum-? WHERE aname = ?";
                try {
                    System.out.println("正在对取款" + s2 + "取款" + s1);
                    ps = conn.prepareStatement(sql);

                    ps.setObject(2, s1);
                    ps.setObject(1, s2);
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
                try {
                    ps.execute();//执行
                    ps.close();
                    conn.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        });

        button04.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String s1 = "",s2 = "";
                s1 = jTextField401.getText();
                s2 = jTextField402.getText();
                System.out.println("转账" + s2);
                Connection con = null;
                PreparedStatement ps = null;
                Driver driver = null;
                try {
                    driver = new com.mysql.cj.jdbc.Driver();//MySQL8 ↓
                    //driver = new com.mysql.jdbc.Driver();//MySQL5
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
                String url = "jdbc:mysql://localhost:3306/demo";
                // 将用户名和密码封装在Properties中
                Properties info = new Properties();
                info.setProperty("user", "root");
                info.setProperty("password", "admin");
                Connection conn = null;
                try {
                    conn = driver.connect(url, info);
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
                System.out.println(conn);

                String sql = "update atmtest set anum=anum-? WHERE aid = 1";
                try {

                    System.out.println("从" + s1 + "转账...");
                    System.out.println("转存至" + s2 );
                    ps = conn.prepareStatement(sql);

                    ps.setObject(1, s2);
                    //ps.setObject(1, s1);
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
                try {
                    ps.execute();//执行
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
                String sql0 = "update atmtest set anum=anum+? WHERE aname = ?";
                try {
                    System.out.println("取款" +  s1);
                    System.out.println("取款" +  s2);
                    ps = conn.prepareStatement(sql0);
                    ps.setObject(1, s2);
                    ps.setObject(2, s1);
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
                try {
                    ps.execute();
                    ps.close();
                    conn.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        });

        button05.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Vector rowData01,rowData02,rowData03, columnNames;
                columnNames = new Vector();
                columnNames.add("序列号");
                columnNames.add("用户名");
                columnNames.add("余额");
                ResultSet resultSet = null;
                Connection conn = null;
                PreparedStatement ps = null;

                Driver driver = null;
                try
                {
                    driver = new com.mysql.cj.jdbc.Driver();//MySQL8 ↓
                    //driver = new com.mysql.cj.jdbc.Driver();//MySQL5

                } catch(SQLException throwables)
                {
                    throwables.printStackTrace();
                }
                String url = "jdbc:mysql://localhost:3306/demo";
                Properties info = new Properties();
                info.setProperty("user","root");
                info.setProperty("password","admin");
                try
                {
                    conn = driver.connect(url, info);
                } catch(SQLException throwables)
                {
                    throwables.printStackTrace();
                }
                System.out.println("已连接数据库 - "+conn);

                try {
                    String sql;//where aname ="1"
                    if(jTextField5.getText()==""){
                        sql = "select aid,aname,anum from atmtest ";

                    }else {
                        sql = "select aid,aname,anum from atmtest WHERE aname = ?";

                    }
                    ps = conn.prepareStatement(sql);
                    ps.setString(1, jTextField5.getText());
                    resultSet = ps.executeQuery();

                    while (resultSet.next()) {
                        Vector hang01 = new Vector(3);
                        Vector hang02 = new Vector(3);
                        Vector hang03 = new Vector(3);
                        hang01.add(0,resultSet.getInt(1));
                        hang02.add(0,resultSet.getString(2));
                        hang03.add(0,resultSet.getDouble(3));//...
                        rowData01 = new Vector();
                        // 加入到rowData
                        rowData01.add(hang01);
                        rowData01.add(hang02);
                        rowData01.add(hang03);
                        DefaultTableModel newtablemodel = new DefaultTableModel();
                        newtablemodel.setColumnIdentifiers(columnNames);
                        newtablemodel.addRow(rowData01);
                        JTable table = new JTable(newtablemodel);
                        JScrollPane sc = new JScrollPane(table);
                        container01.add(sc);
                        sc.setBounds(10,220,500,300);
                    }
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
               }
                try {
                    ps.close();
                    conn.close();
                    resultSet.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        });
        jTextField5.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Scanner scan = new Scanner(System.in);
                int aid = scan.nextInt();
                TextFiledJD00 j1 = new TextFiledJD00();
                //j1.setText5();
                System.out.println("1 = "+j1.getjText5(aid));
            }
        });

        container01.add(button01);
        container01.add(button02);
        container01.add(button03);
        container01.add(button04);
        container01.add(button05);
        container01.add(jTextField1);
        container01.add(jTextField201);
        container01.add(jTextField202);
        container01.add(jTextField301);
        container01.add(jTextField302);
        container01.add(jTextField401);
        container01.add(jTextField402);
        container01.add(jTextField5);

        button01.setBounds(210,610,140,50);
        button02.setBounds(665,240,60,60);
        button03.setBounds(665,365,60,60);
        button04.setBounds(665,500,60,60);
        button05.setBounds(580,610,140,50);
        jTextField1.setBounds(80,600,120,60);
        jTextField201.setBounds(525,210,120,60);
        jTextField202.setBounds(525,260,120,60);
        jTextField301.setBounds(525,330,120,60);
        jTextField302.setBounds(525,390,120,60);
        jTextField401.setBounds(525,460,120,60);
        jTextField402.setBounds(525,520,120,60);
        jTextField5.setBounds(450,600,120,60);
    }
}

class Listener1 implements ActionListener{//文本框1
    @Override
    public void actionPerformed(ActionEvent e) {
        //System.out.println("user:");
    }
}
class Listener2 implements ActionListener{//文本框2
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println();
    }
}

- 判断登录信息是否为空

package Newtest;
public class StringUtil00 {
    public static Boolean isEmpy(String str) {
        if(str==null||"".equals(str.trim())) {
            return true;
        }
        else {
            return false;
        }
    }
    public static Boolean notIsEmpy(String str) {
        if(str!=null&&!"".equals(str.trim())) {
            return true;
        }
        else {
            return false;
        }
    }
}

- 接收要查询用户名

package Newtest;
public class TextFiledJD00 {
    private String text1;
    private String text2;
    private String text3;
    private String text4;
    private String text5;
    int i ;

    public String getjText5(int i) {
        return text5;
    }
    public void setText5(int i) {
        this.text5 = text5;
    }
}
  • 24
    点赞
  • 43
    收藏
    觉得还不错? 一键收藏
  • 23
    评论
评论 23
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值