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