利用GUI界面来编写ATM机

话不多说,上来就是一堆干货。这个是博主在学习过程中,不断学习到新的方法,来对ATM就行改进。其中,新增加的方法包括了方法的调用、GUI的设计、继承、事件触发、读取/写入数据文件等等一系列方法。是博主在现阶段中学习的一些精髓,希望各位看官在观看的过程中,能指点一二(请用本人现阶段能懂得方法讲解~~),谢谢观看~~



package ATM;

import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.FileReader; import java.io.FileWriter; import java.util.Properties;

import javax.swing.JFrame; import javax.swing.JOptionPane;

public class Atm extends JFrame { 

private MyJText Name =new MyJText("用户名",50,50,this); 

private MyJText Psw =new MyJText("密码",50,100,this); 

public static Properties pro =new Properties(); 

private int number;

 //静态块的使用!要看书174页!

 static{ try { pro.load(new FileReader("info")); } catch (Exception e) { System.out.println("文件读取错误"); System.exit(0); } }

public static void SaveProperties(){
	try {
		pro.store(new FileWriter("info"), null);
	} catch (Exception e) {
		System.out.println("文件未找到");
		System.exit(0);
	}
}

public Atm(){
	this.setLayout(null);
	
	MyButton but =new MyButton("登陆",200,200,this);
	but.addActionListener(new ActionListener() {
		
		@Override
		public void actionPerformed(ActionEvent e) {
			String newName = Name.getText();
			String newPsw = Psw.getText();
			
			String OldName = pro.getProperty("userName");
			String OldPsw = pro.getProperty("password");
			
			if(OldName.equals(newName)  && OldPsw.equals(newPsw)){
				JOptionPane.showMessageDialog(null, "登陆成功");
				Atm.this.dispose();
				new MyJFrame();
			}else{
				JOptionPane.showMessageDialog(null, "登录失败!");
				Name.setText("");
				Psw.setText("");
			}
			number++;
			
			if(number >=3){
				JOptionPane.showMessageDialog(null, "账号密码错误!");
				System.exit(0);
			}
		}
	});
	

	this.setSize(400,300);
	this.setVisible(true);
	this.setDefaultCloseOperation(3);
	this.setLocationRelativeTo(null);
}

public static  void main(String [] agrs){
	Atm a =new Atm();
}

}

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值