利用GUI界面来编写ATM机

来源:互联网 发布:数据信号避雷器 编辑:程序博客网 时间:2024/05/19 06:17

话不多说,上来就是一堆干货。这个是博主在学习过程中,不断学习到新的方法,来对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() {@Overridepublic 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();}

}




package ATM;

import java.awt.event.ActionEvent; import java.awt.event.ActionListener;

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

public class ChangeFrame extends JFrame { 

private MyJText psw1 = new MyJText("请输入新密码", 50, 50, this);

 private MyJText psw2 = new MyJText("确认新密码", 50, 100, this);

 private int number;

public ChangeFrame(){this.setLayout(null);MyButton but =new MyButton("改密", 200, 200, this);but.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {String str = Atm.pro.getProperty("password");String Str1 =psw1.getText();String Str2 =psw2.getText();if(Str1.equals(Str2)){str=Str1;JOptionPane.showMessageDialog(null, "改密成功");Atm.pro.setProperty("password", str);Atm.SaveProperties();ChangeFrame.this.dispose();new MyJFrame();}else{JOptionPane.showMessageDialog(null, "两次密码不一致,请修改");psw1.setText("");psw2.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){ChangeFrame c =new ChangeFrame();}

}




package ATM;

import java.awt.event.ActionEvent; import java.awt.event.ActionListener;

import javax.swing.JFrame; import javax.swing.JLabel;

public class FindFrame extends JFrame{

 public FindFrame(){ this.setLayout(null); 

String moneyTxt =Atm.pro.getProperty("money"); 

JLabel jl =new JLabel("余额"+moneyTxt);

 jl.setBounds(100, 100, 120, 30);

 this.add(jl);

MyButton but =new MyButton("返回",180,190,this);but.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {FindFrame.this.dispose();new MyJFrame();}});this.setSize(400, 300);this.setVisible(true);this.setDefaultCloseOperation(3);this.setLocationRelativeTo(null);}public static void main(String [] agrs){FindFrame f =new FindFrame();}

}




package ATM;

import java.awt.event.ActionEvent; import java.awt.event.ActionListener;

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

public class GetFrame extends JFrame{

 private MyJText jt =new MyJText("请输入取款金额",100,100,this); 

public GetFrame(){ this.setLayout(null);

MyButton but =new MyButton("取款",230,150,this);but.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {String OldMoney = Atm.pro.getProperty("money");int a = Integer.parseInt(OldMoney);String newMoney =jt.getText();int b =Integer.parseInt(newMoney);if(b<a){a-=b;JOptionPane.showMessageDialog(null, "取款成功!");Atm.pro.setProperty("money", a+"");Atm.SaveProperties();GetFrame.this.dispose();new FindFrame();}else{JOptionPane.showMessageDialog(null, "取款超过存款!");jt.setText("");}}});this.setSize(400,300);this.setVisible(true);this.setDefaultCloseOperation(3);this.setLocationRelativeTo(null);}public static void main(String [] agrs){GetFrame s=new GetFrame();}

}







package ATM;

import java.awt.event.ActionEvent; import java.awt.event.ActionListener;

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

public class MyJFrame extends JFrame { 

public MyJFrame() { 

this.setLayout(null);

MyButton FindFrame = new MyButton("查询", 20, 70, this);MyButton ChangeFrame = new MyButton("改密", 20, 170, this);MyButton SetFrame = new MyButton("存款", 220, 60, this);MyButton GetFrame = new MyButton("取款", 220, 120, this);MyButton ExitFrame = new MyButton("取卡", 220, 180, this);FindFrame.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {MyJFrame.this.dispose();new FindFrame();}});ChangeFrame.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {MyJFrame.this.dispose();new ChangeFrame();}});SetFrame.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {MyJFrame.this.dispose();new SetFrame();}});GetFrame.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {MyJFrame.this.dispose();new GetFrame();}});ExitFrame.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {

// MyJFrame.this.dispose(); 

// new ExitFrame();

 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) {MyJFrame jf = new MyJFrame();}

}




package ATM;

import java.awt.event.ActionEvent; import java.awt.event.ActionListener;

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

public class SetFrame extends JFrame { 

private MyJText jt =new MyJText("请输入存款金额",100,100,this); 

public SetFrame(){ 

this.setLayout(null);

MyButton but =new MyButton("存款",230,150,this);but.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {String OldMoney = Atm.pro.getProperty("money");int a = Integer.parseInt(OldMoney);String newMoney =jt.getText();int b =Integer.parseInt(newMoney);a+=b;JOptionPane.showMessageDialog(null, "存款成功!");Atm.pro.setProperty("money", a+"");Atm.SaveProperties();SetFrame.this.dispose();new FindFrame();}});this.setSize(400,300);this.setVisible(true);this.setDefaultCloseOperation(3);this.setLocationRelativeTo(null);}public static void main(String [] agrs){SetFrame s=new SetFrame();}

}



0 0
原创粉丝点击