package text;
import java.awt.*;
import java.awt.event.*;
import java.util.regex.Pattern;
import javax.swing.*;
public class ATMSHAM extends Frame implements ActionListener
{
private JFrame frame;
private JPanel jp1,jp2,jp3,jp4,jp5;
private JTextField account;
private JTextField password;
private JButton start,select,cancel,save,out;
private JLabel jl1;
private JLabel jl2;
private double m=0;//设置初始金额
public ATMSHAM()
{
frame=new JFrame("自动取款机");
frame.setLocation(280, 280);
frame.setSize(140,60);
Container c=frame.getContentPane();
jl1=new JLabel("账号:");
account=new JPasswordField(18);//输出结果
jl2=new JLabel("密码:");
password=new JPasswordField(18);//输出结果
jl1.setFont(new Font("宋体",Font.BOLD, 20));//字体设置
account.setFont(new Font("宋体",Font.BOLD, 20));
jl2.setFont(new Font("宋体",Font.BOLD, 20));//字体设置
password.setFont(new Font("宋体",Font.BOLD, 20));
start=new JButton("开始");
start.addActionListener(this);
select=new JButton("查询余额");
select.addActionListener(this);
cancel=new JButton("结束");
cancel.addActionListener(this);
save=new JButton("存款");
save.addActionListener(this);
out=new JButton("取款");
out.addActionListener(this);
select.setEnabled(false);
save.setEnabled(false);
out.setEnabled(false);
jp1=new JPanel();//账号输入面板
jp2=new JPanel();//密码输入面板
jp3=new JPanel();//按钮面板
jp4=new JPanel();//按钮面板
jp5=new JPanel();//主面板
jp2.setLayout(new FlowLayout());//窗体布局
jp2.setBackground(Color.lightGray);
jp2.add(jl1);
jp2.add(account);
jp2.add(jl2);
jp2.add(password);
jp3.setLayout(new FlowLayout());
jp3.setBackground(Color.lightGray);
jp3.add(start);
jp3.add(select);
jp3.add(cancel);
jp4.add(save);
jp4.add(out);
jp5.setLayout(new GridLayout(3,1));
jp5.setBackground(Color.lightGray);
jp5.add(jp2);
jp5.add(jp3);
jp5.add(jp4);
c.add(jp5);
frame.pack();
frame.setVisible(true);
frame.setResizable(false);
account.requestFocusInWindow();//输入文本框获取焦点
password.requestFocusInWindow();//输入文本框获取焦点
}
public void actionPerformed(ActionEvent e)//按钮监听类
{
String s="";
if(e.getSource()==password)//密码
{
s=account.getText().toString();//获取账号
if(s.equals(""))//没有输入账号
{
JOptionPane.showMessageDialog(null, "你没有输入账号,请重新操作输入");//弹出一个消息提示框
account.requestFocusInWindow();//账号输入设置焦点
}
else if(Pattern.matches("^[0-9]{6}+$", s))//输入的是六位数字
{
account.setEnabled(false);//账号验证正确,不能再输入账号
password.setEnabled(true);//账号验证正确,可以输入密码
}
else//输入的不是六位数字,重新输入密码
{
JOptionPane.showMessageDialog(null, "你输入的账号格式有误,请重新输入");
account.setText("");
account.requestFocusInWindow();//账号输入设置焦点
}
}
if(e.getSource()==start)//开始
{
s=password.getText().toString();//获取密码
if(s.equals(""))//没有输入密码
{
JOptionPane.showMessageDialog(null, "你没有输入密码,请重新操作输入");//弹出一个消息提示框
password.requestFocusInWindow();//密码输入设置焦点
}
else if(Pattern.matches("^[0-9]{6}+$", s))//输入的是六位数字
{
password.setEnabled(false);//密码验证正确,不能再输入密码
select.setEnabled(true);//密码验证正确,可以查看余额
save.setEnabled(true);//密码验证正确,可以存款
out.setEnabled(true);//密码验证正确,可以取款
}
else//输入的不是六位数字,重新输入密码
{
JOptionPane.showMessageDialog(null, "你输入的密码格式有误,请重新输入");
password.setText("");
password.requestFocusInWindow();//密码输入设置焦点
}
}
else if(e.getSource()==select)//查看余额
{
JOptionPane.showMessageDialog(null, "你目前还有"+m+"元");
}
else if(e.getSource()==cancel)//退出
{
dispose();
System.exit(0);
}
else if(e.getSource()==save)//存款
{
String result=JOptionPane.showInputDialog(null, "请输入存入金额");
if(result==null)
return;
else if(result.equals(""))
JOptionPane.showMessageDialog(null, "你没有输入金额,请重新操作,以输入待存入金额");
else if(Pattern.matches("^[0-9]+$", result)||Pattern.matches("^[0-9]+\\.+[0-9]+$", result))
{
double re=Double.parseDouble(result);
m+=re;
}
else
{
JOptionPane.showMessageDialog(null, "你输入的金额格式有误,请重新输入");
}
}
else if(e.getSource()==out) //取款
{
String result=JOptionPane.showInputDialog(null, "请输入取款金额值");//弹出一个输入框并获取输入的值
if(result==null)//单击了输入对话框的取消按钮
return;
else if(result.equals(""))//没有输入金额
JOptionPane.showMessageDialog(null, "你没有输入金额,请重新操作,以输入要取出的金额值");
else if(Pattern.matches("^[0-9]+$", result)||Pattern.matches("^[0-9]+\\.+[0-9]+$", result))//输入不为空且输入正确
{
double re=Double.parseDouble(result);
if(m <=0)//没有可取金额
JOptionPane.showMessageDialog(null, "你已经没有金额可取,请存入金额");
else if(m-re<0)//有可取金额,但金额不足
JOptionPane.showMessageDialog(null, "你仅有"+m+"元,金额不足");
else if(m-re>=0)//金额足够
m-=re;
}
else//输入不为空但不正确
{
JOptionPane.showMessageDialog(null, "你输入的金额格式有误,请重新输入");
}
}
frame.addWindowListener(new WindowAdapter() //关闭窗口事件
{
public void WindowClosing(WindowEvent e)
{
dispose();
System.exit(0);
}
});
}
public static void main(String args [])//主方法入口
{
System.out.println("程序开始…");
new ATMSHAM();
}
}

- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-
