当前位置:文档之家› ATM机java代码

ATM机java代码

/*ATM机的设计
* 首先,为各组件布局
* 然后,为每个事件添加监听事件,即当点击本按钮时。能进入相应的界面,操作相应的功能;
* 最后,对数据的调用进行处理使其能进行正常的交互。
* 1.布局中的组件:
* 三个Panel、四个Box、二十五个Button、文本域一个TextArea、文本框两个TextField、标签;
* 思路:1、创建JFrame窗体;2、创建按钮(25个)
* */
package shiyan10;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

public class ATM {

JFrame f=new JFrame();
JButton btn1=new JButton("===>");
JButton btn2=new JButton("===>");
JButton btn3=new JButton("===>");
JButton btn4=new JButton("===>");

JButton btn5=new JButton("<===");
JButton btn6=new JButton("<===");
JButton btn7=new JButton("<===");
JButton btn8=new JButton("<===");

JButton btn9=new JButton("[_____"+"取钱处"+"_____]");
JButton btn0=new JButton("[____"+"插卡处"+"____]");

JButton btn10=new JButton("更正");
JButton btn11=new JButton("取消");
JButton btn12=new JButton("确认");

JPanel p0=new JPanel();
JPanel p1=new JPanel();
JPanel p2=new JPanel();
JPanel p3=new JPanel();
Box b0=Box.createVerticalBox();
Box b1=Box.createVerticalBox();
Box b2=Box.createVerticalBox();
Box b3=Box.createHorizontalBox();
JButton[] btn=new JButton[12];

JTextArea ta=new JTextArea(10,18);
JTextField fd0=new JTextField(25);
JTextField fd1=new JTextField();

JTextField fd2=new JTextField(25);
JTextField fd3=new JTextField(25);

JLabel lb=new JLabel("欢迎使用本系统,请插入卡!");
JLabel lb1=new JLabel("请输入密码");
JLabel lb2=new JLabel("取款");
JLabel lb3=new JLabel("查询");
JLabel lb4=new JLabel("退卡");
JLabel lb5=new JLabel("修改密码");
JLabel lb6=new JLabel("请选择");
JLabel lb7=new JLabel("您的余额是:");
JLabel lb8=new JLabel("返回");
JLabel lb9=new JLabel("请输入提取金额");
JLabel lb10=new JLabel("是否打印凭条");
JLabel lb11=new JLabel("是");
JLabel lb12=new JLabel("否");
JLabel lb13=new JLabel("请输入新密码");
// JLabel lb14=new JLabel("请再此输入新密码");
JLabel lb15=new JLabel("密码修改成功了!");
JLabel lb14=new JLabel("两次密码输入不一致请重新输入");

String str1="";
int di=0;
public void init(){
fd1.setBounds(50, 110, 100, 35);

f.addWindowListener(new MyListener());
// 设置开始界面并其添加标签Label
lb.setHorizontalAlignmen

t(SwingConstants.CENTER);
lb.setBounds(7, 10, 170, 70);
lb.setForeground(Color.WHITE);
lb.setBackground(Color.BLUE);
// lb.setPreferredSize(new Dimension(50,60));
lb.setVisible(true);
ta.setBackground(Color.BLUE);
ta.add(lb);
// 为屏幕左边键钮
b0.add(btn1);
b0.add((Box.createVerticalStrut(18)));
b0.add(btn2);
b0.add((Box.createVerticalStrut(18)));
b0.add(btn3);
b0.add((Box.createVerticalStrut(18)));
b0.add(btn4);
// 为屏幕邮编添加按钮
b1.add(btn5);
b1.add((Box.createVerticalStrut(18)));
b1.add(btn6);
b1.add((Box.createVerticalStrut(18)));
b1.add(btn7);
b1.add((Box.createVerticalStrut(18)));
b1.add(btn8);
// 添加中间中取钱和插卡组建
b3.add(btn9);
b3.add(Box.createHorizontalStrut(15));
b3.add(btn0);
// 添加应用按钮:更正、取消、确认等件;
b2.add(btn10);
b2.add((Box.createVerticalStrut(23)));
b2.add(btn11);
b2.add((Box.createVerticalStrut(23)));
b2.add(btn12);
// 将组件加入容器中:共设有三个容器p0、p1、p2;

p0.add(b0);
p0.add(ta);
p0.add(b1);
// 为数字区按钮添加名称
String str[] = { "7", "8", "9","4", "5", "6", "3",
"2", "1", "0", "00","."};
for(int i=0;i<12;i++){
btn[i]=new JButton(str[i]);
}
// 将取钱、插卡组建加入容器中
// p1.add((Box.createVerticalStrut(5)));
p1.add(b3);
// p1.add((Box.createVerticalStrut(20)));
// 将输入区加入到容器中
for(int i=0;i<12;i++){
p3.add(btn[i]);
}
p3.setLayout(new GridLayout(4,3,10,10));

p2.add(p3);
p2.add((Box.createHorizontalStrut(70)));
p2.add(b2);
// 为个按钮家监听事件 方法一:
// btn0.addActionListener(new ActionListener(){
// public void actionPerformed(ActionEvent e){
// lb.setVisible(false);
// lb.setText(null);
// lb1.setBounds(30, 10, 170, 70);
// lb1.setForeground(Color.WHITE);
// lb1.setBackground(Color.BLUE);
// lb.setVisible(true);
// ta.add(lb1);
// fd0.setHorizontalAlignment(SwingConstants.CENTER);
// fd0.setBounds(30, 110, 100, 20);
// fd0.setVisible(true);
// ta.add(fd0);
// }
// });
// 方法二:
btn0.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(lb.getText().equals("欢迎使用本系统,请插入卡!"))
{
lb.setText("请输入密码!");
fd0.setBounds(50, 79, 100, 30);
ta.add(fd0);
}
// else if((lb.getText().equals("欢迎使用本系统,请插卡!"))&&(lb )){
// lb.setText("请输入密码!");
// fd0.setBounds(50, 79, 100, 30);
// ta.add(fd0);
// }
}
});
btn12.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

if (
lb.getText().equals("请输入密码!")) {
fd0.setVisible(false);
lb2.setBounds(8, 8, 80, 20);
lb3.setBounds(155, 8, 84, 20);
lb4.setBounds(8, 105, 80, 20);

lb5.setBounds(8, 145, 100, 25);
lb.setText("请选择");

lb2.setForeground(Color.WHITE);
lb2.setVisible(true);

lb3.setForeground(Color.WHITE);
lb3.setVisible(true);

lb4.setForeground(Color.WHITE);
lb4.setVisible(true);

lb5.setForeground(Color.WHITE);
lb5.setVisible(true);

ta.add(lb2);
ta.add(lb3);
ta.add(lb4);
ta.add(lb5);
}
else
{
if (lb.getText().equals("请输入取款金额")) {
// 进入打印提示的方法有两种1.if (fd0.getText().length()!=0){执行语句}
// 2、if(!(fd0.getText().equals(null))){执行语句};
// if (fd0.getText().length()!=0)
if (!(fd0.getText().equals(null))) {
lb2.setVisible(false);
lb3.setVisible(false);
lb4.setVisible(false);
lb5.setVisible(false);
lb8.setVisible(false);
fd0.setVisible(false);
lb11.setBounds(155, 100, 80, 20);
lb12.setBounds(155, 125, 90, 20);
lb11.setForeground(Color.WHITE);
lb11.setVisible(true);

lb12.setVisible(true);
lb12.setForeground(Color.WHITE);
lb.setText("是否打印凭条?");

ta.add(lb11);
ta.add(lb12);

}
} else if((lb.getText().equals("请输入新密码!"))
&& (lb8.getText().equals("返回"))) {

lb8.setVisible(true);
fd0.setVisible(false);
fd1.setBounds(50, 79, 100, 30);
fd1.setVisible(true);
ta.add(fd1);
ta.add(lb8);
lb.setText("请再次输入新密码!");
}else if((lb.getText().equals("请再次输入新密码!"))&& (lb8.getText().equals("返回"))){
lb8.setVisible(true);
fd1.setVisible(false);
lb.setText("密码修改成功!");
}else if((lb.getText().equals("密码修改成功!"))&& (lb8.getText().equals("返回"))){
lb2.setVisible(true);
lb3.setVisible(true);
lb4.setVisible(true);
lb5.setVisible(true);
lb.setText("请选择");
ta.add(lb2);
ta.add(lb3);
ta.add(lb4);
ta.add(lb5);
}
}

}
});
btn5.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){


if((lb.getText().equals("请选择"))&&lb3.getText().equals("查询"))

{
fd0.setVisible(false);
lb2.setVisible(false);
lb3.setVisible(false);
lb4.setVisible(false);
lb5.setVisible(false);
lb.setText("您的余额是:"+0);

lb8.setBounds(8, 50, 80, 20);
lb8.setForeground(Color.WHITE);
lb8.setVisible(true);

ta.add(lb8);
}
}
});
btn2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if((lb.getText().equals("您的余额是:"+0))&&(lb8.getText().equals("返回"))){


lb8.setVisible(false);
lb.setText("请选择");

lb2.setForeground(Color.W

HITE);
lb2.setVisible(true);

lb3.setForeground(Color.WHITE);
lb3.setVisible(true);

lb4.setForeground(Color.WHITE);
lb4.setVisible(true);

lb5.setVisible(true);
lb5.setForeground(Color.WHITE);
}else if((lb.getText().equals("密码修改成功!"))&& (lb8.getText().equals("返回"))||(lb.getText().equals("请输入新密码!"))&& (lb8.getText().equals("返回"))){
lb2.setVisible(true);
lb3.setVisible(true);
lb4.setVisible(true);
lb5.setVisible(true);
lb.setText("请选择");
ta.add(lb2);
ta.add(lb3);
ta.add(lb4);
ta.add(lb5);
}else if((lb.getText().equals("请再次输入新密码!"))&& (lb8.getText().equals("返回"))){
lb8.setVisible(true);

fd1.setVisible(true);
ta.add(fd1);
ta.add(lb8);
lb.setText("请输入新密码!");
}

}
});
btn1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if((lb.getText().equals("请选择"))&&(lb2.getText().equals("取款"))){

lb2.setVisible(false);
lb3.setVisible(false);
lb4.setVisible(false);
lb5.setVisible(false);
lb.setText("请输入取款金额");
fd0.setVisible(true);
ta.add(fd0);
}
}
});
btn3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if ((lb.getText().equals("请选择"))
&& (lb4.getText().equals("退卡"))) {
lb2.setVisible(false);
lb3.setVisible(false);
lb4.setVisible(false);
lb5.setVisible(false);
lb.setText("欢迎使用本系统,请插入卡!");


}
}
});
btn4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if ((lb.getText().equals("请选择"))
&& (lb5.getText().equals("修改密码"))) {
lb2.setVisible(false);
lb3.setVisible(false);
lb4.setVisible(false);
lb5.setVisible(false);
lb.setText("请输入新密码!");
fd0.setVisible(true);
lb8.setBounds(8, 55, 80, 20);
lb8.setForeground(Color.WHITE);
lb8.setVisible(true);
ta.add(lb8);
ta.add(fd0);
}
}
});
// 位数字按钮添加监听事件
for(int i=0;ibtn[i].addActionListener(new JButtonListener());
}

// 设置JFrame窗体大小
f.setBounds(200, 200, 397, 425);
// 确定容器在窗体中的位置,为此进行设置
f.add(p0,BorderLayout.NORTH);
f.add(p1);
f.add(p2,BorderLayout.SOUTH);
// 设这窗体为可见的
f.setVisible(true);
}

public static void main(String[] args) {
new ATM().init();

}
public class JButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e){
for (int i = 0; i < btn.length; i++){
if(e.getSource()==btn[i]){

switch(i){
case 0:
str1 = str1 + 7;
if (str1.sta

rtsWith("0")) {
if (str1.indexOf(".") >= 0) {
fd0.setText(str1);
} else {
String s = str1.replaceFirst("0", "");
fd0.setText(s);
}
} else
fd0.setText(str1);

break;
case 1:
str1 = str1 + 8;
if (str1.startsWith("0")) {
if (str1.indexOf(".") >= 0) {
fd0.setText(str1);
} else {
String s = str1.replaceFirst("0", "");
fd0.setText(s);
}
} else
fd0.setText(str1);

break;
case 2:
str1 = str1 + 9;
if (str1.startsWith("0")) {
if (str1.indexOf(".") >= 0) {
fd0.setText(str1);
} else {
String s = str1.replaceFirst("0", "");
fd0.setText(s);
}
} else
fd0.setText(str1);

break;
case 3:

str1 = str1 + 4;
if (str1.startsWith("0")) {
if (str1.indexOf(".") >= 0) {
fd0.setText(str1);
} else {
String s = str1.replaceFirst("0", "");
fd0.setText(s);
}
} else
fd0.setText(str1);

break;
case 4:

str1 = str1 + 5;
if (str1.startsWith("0")) {
if (str1.indexOf(".") >= 0) {
fd0.setText(str1);
} else {
String s = str1.replaceFirst("0", "");
fd0.setText(s);
}
} else
fd0.setText(str1);

break;
case 5:

str1 = str1 + 6;
if (str1.startsWith("0")) {
if (str1.indexOf(".") >= 0) {
fd0.setText(str1);
} else {
String s = str1.replaceFirst("0", "");
fd0.setText(s);
}
} else
fd0.setText(str1);

break;
case 6:

str1 = str1 + 3;
if (str1.startsWith("0")) {
if (str1.indexOf(".") >= 0) {
fd0.setText(str1);
} else {
String s = str1.replaceFirst("0", "");
fd0.setText(s);
}
} else
fd0.setText(str1);

break;

case 7:

str1 = str1 + 2;
if (str1.startsWith("0")) {
if (str1.indexOf(".") >= 0) {
fd0.setText(str1);
} else {
String s = str1.replaceFirst("0", "");
fd0.setText(s);
}
} else
fd0.setText(str1);

break;
case 8:

str1 = str1 + 1;
if (str1.startsWith("0")) {
if (str1.indexOf(".") >= 0) {
fd0.setText(str1);
} else {
String s = str1.replaceFirst("0", "");
fd0.setText(s);
}
} else
fd0.setText(str1);

break;
case 9:
if (di == 0) {
if (str1.startsWith("0") && str1.length() == 1) {
str1 = str1 + ".";
fd0.setText(str1);
} else {
str1 = str1 + ".";
fd0.setText(str1);
}

} else {
fd0.setText(str1);
}
di++;
break;
case 10:
if (str1.startsWith("0") && str1.lengt

h() == 1) {
fd0.setText(str1);
} else {
str1 = str1 + "0";
fd0.setText(str1);
}
break;
case 11:
if (str1.startsWith("0") && str1.length() == 1) {
fd0.setText(str1+0);
} else {
str1 = str1 + "00";
fd0.setText(str1);
}
break;
}

}
}
}
}
class MyListener extends WindowAdapter{
public void windowClosed(WindowEvent e){
System.exit(0);
}
}

}

相关主题
文本预览
相关文档 最新文档