java gui编程写atm机界面并且实现存储,取款等功能

时间: 2024-04-07 20:29:03 浏览: 20
好的,下面是一个Java GUI程序,实现了ATM机界面,并且实现了存储和取款功能: ```java import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class ATMGUI extends JFrame implements ActionListener{ private JLabel welcomeLabel; private JLabel idLabel; private JLabel passwordLabel; private JTextField idField; private JPasswordField passwordField; private JButton loginButton; private JButton cancelButton; private JLabel balanceLabel; private JLabel operationLabel; private JTextField operationField; private JButton depositButton; private JButton withdrawButton; private double balance; public ATMGUI() { init(); this.setTitle("ATM机界面"); this.setSize(400, 300); this.setLocationRelativeTo(null); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } public void init() { // 欢迎语 welcomeLabel = new JLabel("欢迎使用ATM机"); welcomeLabel.setFont(new Font("微软雅黑", Font.BOLD, 24)); welcomeLabel.setHorizontalAlignment(SwingConstants.CENTER); // 用户名和密码输入框 idLabel = new JLabel("账号:"); passwordLabel = new JLabel("密码:"); idField = new JTextField(20); passwordField = new JPasswordField(20); // 登录和取消按钮 loginButton = new JButton("登录"); cancelButton = new JButton("取消"); loginButton.addActionListener(this); cancelButton.addActionListener(this); // 余额信息 balanceLabel = new JLabel("余额:0.0元"); balanceLabel.setFont(new Font("微软雅黑", Font.BOLD, 16)); balanceLabel.setHorizontalAlignment(SwingConstants.CENTER); // 存款和取款按钮 operationLabel = new JLabel("请输入操作金额:"); operationField = new JTextField(20); depositButton = new JButton("存款"); withdrawButton = new JButton("取款"); depositButton.addActionListener(this); withdrawButton.addActionListener(this); // 设置布局 this.setLayout(new GridLayout(6, 2, 10, 10)); this.add(welcomeLabel); this.add(new JLabel("")); this.add(idLabel); this.add(idField); this.add(passwordLabel); this.add(passwordField); this.add(loginButton); this.add(cancelButton); this.add(balanceLabel); this.add(new JLabel("")); this.add(operationLabel); this.add(operationField); this.add(depositButton); this.add(withdrawButton); } public void actionPerformed(ActionEvent e) { if (e.getSource() == loginButton) { // 登录 String id = idField.getText(); String password = new String(passwordField.getPassword()); if (id.equals("123456") && password.equals("123456")) { JOptionPane.showMessageDialog(this, "登录成功", "提示", JOptionPane.INFORMATION_MESSAGE); balance = 1000.0; balanceLabel.setText("余额:" + balance + "元"); idField.setEditable(false); passwordField.setEditable(false); loginButton.setEnabled(false); cancelButton.setText("退出"); } else { JOptionPane.showMessageDialog(this, "账号或密码错误", "错误", JOptionPane.ERROR_MESSAGE); } } else if (e.getSource() == cancelButton) { // 取消或退出 if (cancelButton.getText().equals("取消")) { idField.setText(""); passwordField.setText(""); } else { System.exit(0); } } else if (e.getSource() == depositButton) { // 存款 String str = operationField.getText(); if (str.equals("")) { JOptionPane.showMessageDialog(this, "请输入操作金额", "错误", JOptionPane.ERROR_MESSAGE); } else { double amount = Double.parseDouble(str); balance += amount; balanceLabel.setText("余额:" + balance + "元"); operationField.setText(""); JOptionPane.showMessageDialog(this, "存款成功", "提示", JOptionPane.INFORMATION_MESSAGE); } } else if (e.getSource() == withdrawButton) { // 取款 String str = operationField.getText(); if (str.equals("")) { JOptionPane.showMessageDialog(this, "请输入操作金额", "错误", JOptionPane.ERROR_MESSAGE); } else { double amount = Double.parseDouble(str); if (amount > balance) { JOptionPane.showMessageDialog(this, "余额不足", "错误", JOptionPane.ERROR_MESSAGE); } else { balance -= amount; balanceLabel.setText("余额:" + balance + "元"); operationField.setText(""); JOptionPane.showMessageDialog(this, "取款成功", "提示", JOptionPane.INFORMATION_MESSAGE); } } } } public static void main(String[] args) { new ATMGUI(); } } ``` 这个程序会生成一个ATM机的登录界面,包含一个欢迎语、一个账号输入框、一个密码输入框以及一个登录按钮和一个取消按钮。登录成功后,会显示余额信息以及存款和取款按钮。你可以在代码中进一步完善其他功能。

相关推荐

最新推荐

recommend-type

Java的带GUI界面猜数字游戏的实现示例

主要介绍了Java的带GUI界面猜数字游戏的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
recommend-type

Java GUI编程实现在线聊天室

主要为大家详细介绍了Java GUI编程实现在线聊天室,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

如何进行Java GUI图形用户界面编程

AWT中文译为抽象窗口工具包,是Java提供的用来建立和设置Java的图形用户界面的基本工具。AWT由Java中的java.awt包提供,里面包含了许多可用来建立与平台无关的图形用户界面的类,这些类被称为组件。Java Swing是Java...
recommend-type

java GUI实现五子棋游戏

主要为大家详细介绍了java GUI实现五子棋游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

JAVA图形界面(GUI)之表格的示例代码

主要介绍了JAVA图形界面(GUI)之表格的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

spring添加xml配置文件

1. 创建一个新的Spring配置文件,例如"applicationContext.xml"。 2. 在文件头部添加XML命名空间和schema定义,如下所示: ``` <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。