📄 mainframe.java
字号:
package a2;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;
public class MainFrame extends JFrame{
JPanel top,subTop,bottom;
JButton b[],opb[],correct,cancle,certain;
JLabel hintLab,mainLab,hl1,hl2,hl3,hl4,hl5,hl6,hl7;//提示标签
JTextField inputText; //输入框
JButton moneyBut,cardBut;
Account account; //当前操作用户
static Font hitFont=new Font("宋体",10,15);
static int type=1; //界面初值 1、插卡界面 2、输入密码界面 3、取钱界面 4、查询界面 5、取钱结束
// -1、密码三次错误 6、选择如何操作界面 7、密码修改界面
String str=""; //打印凭条的记录变量
String strtemp="";//纪录输入的新密码
int readPass=1;
File file = new File( "C:/ATM_account.bin");
ObjectInputStream objectIn = null;
ObjectOutputStream objectOut = null;
public MainFrame(){
super("ATM模型");
setSize(400,500);
Container c = getContentPane();
c.setLayout(null);
top=new JPanel();
top.setLayout(null);
top.setBounds(5,3,383,185);
top.setBorder(new LineBorder(Color.BLACK));
c.add(top);
//左排按钮----------------------------------
b=new JButton[8];
b[0]=new JButton("===>");
b[1]=new JButton("===>");
b[2]=new JButton("===>");
b[3]=new JButton("===>");
b[0].setBounds(5,5,85,40);
top.add(b[0]);
b[1].setBounds(5,50,85,40);
top.add(b[1]);
b[2].setBounds(5,95,85,40);
top.add(b[2]);
b[3].setBounds(5,140,85,40);
top.add(b[3]);
//右排按钮------------------------------------
b[4]=new JButton("<===");
b[5]=new JButton("<===");
b[6]=new JButton("<===");
b[7]=new JButton("<===");
b[4].setBounds(293,5,85,40);
top.add(b[4]);
b[5].setBounds(293,50,85,40);
top.add(b[5]);
b[6].setBounds(293,95,85,40);
top.add(b[6]);
b[7].setBounds(293,140,85,40);
top.add(b[7]);
//屏幕------------------------------------------
subTop=new JPanel();
subTop.setBounds(95,5,193,175);
subTop.setBackground(Color.BLUE);
subTop.setLayout(null);
top.add(subTop);
hintLab=new JLabel("欢迎使用,请插卡");
hintLab.setHorizontalAlignment(JLabel.CENTER);
hintLab.setFont(hitFont);
hintLab.setForeground(Color.WHITE);
hintLab.setBounds(0,35,193,30);
subTop.add(hintLab);
inputText=new JTextField("");
inputText.setHorizontalAlignment(JTextField.RIGHT);
inputText.setBounds(20,120,150,30);
inputText.setVisible(false);
subTop.add(inputText);
//按钮提示------------------------------------------
hl1=new JLabel("取款");
hl2=new JLabel("查询");
hl3=new JLabel("修改密码");
hl4=new JLabel("是");
hl5=new JLabel("否");
hl6=new JLabel("退卡");
hl7=new JLabel("返回");
hl1.setBounds(5,10,60,30);
hl2.setBounds(160,10,60,30);
hl3.setBounds(5,140,60,30);
hl4.setBounds(170,100,60,30);
hl5.setBounds(170,140,60,30);
hl6.setBounds(5,100,60,30);
hl7.setBounds(5,55,50,30);
hl1.setForeground(Color.WHITE);
hl2.setForeground(Color.WHITE);
hl3.setForeground(Color.WHITE);
hl4.setForeground(Color.WHITE);
hl5.setForeground(Color.WHITE);
hl6.setForeground(Color.WHITE);
hl7.setForeground(Color.WHITE);
subTop.add(hl1);
subTop.add(hl2);
subTop.add(hl3);
subTop.add(hl4);
subTop.add(hl5);
subTop.add(hl6);
subTop.add(hl7);
//出入口******************************************************************
//Icon ico=new ImageIcon("temp.jpg");
moneyBut=new JButton("[____________取钱处____________]");
moneyBut.setBounds(5,195,250,50);
c.add(moneyBut);
cardBut=new JButton("[__插卡处__]");
cardBut.setBounds(265,195,123,50);
c.add(cardBut);
//操作板******************************************************************
bottom=new JPanel();
bottom.setLayout(null);
bottom.setBounds(5,255,383,185);
bottom.setBorder(new LineBorder(Color.BLACK));
c.add(bottom);
opb=new JButton[12];
for(int i=0;i<10;i++){
opb[i]=new JButton(i+"");
}
opb[10]=new JButton(".");
opb[11]=new JButton("00");
opb[7].setBounds(5,5,60,40);
opb[8].setBounds(70,5,60,40);
opb[9].setBounds(135,5,60,40);
bottom.add(opb[7]);
bottom.add(opb[8]);
bottom.add(opb[9]);
opb[4].setBounds(5,50,60,40);
opb[5].setBounds(70,50,60,40);
opb[6].setBounds(135,50,60,40);
bottom.add(opb[4]);
bottom.add(opb[5]);
bottom.add(opb[6]);
opb[3].setBounds(5,95,60,40);
opb[2].setBounds(70,95,60,40);
opb[1].setBounds(135,95,60,40);
bottom.add(opb[1]);
bottom.add(opb[2]);
bottom.add(opb[3]);
opb[10].setBounds(5,140,60,40);
opb[0].setBounds(70,140,60,40);
opb[11].setBounds(135,140,60,40);
bottom.add(opb[10]);
bottom.add(opb[0]);
bottom.add(opb[11]);
correct=new JButton("更正");
correct.setBounds(260,5,100,50);
bottom.add(correct);
cancle=new JButton("取消");
cancle.setBounds(260,67,100,50);
bottom.add(cancle);
certain=new JButton("确认");
certain.setBounds(260,130,100,50);
bottom.add(certain);
mainLab=new JLabel("西电第二课堂 08.06");
mainLab.setBounds(270,440,130,30);
c.add(mainLab);
show();
mainVisible();
ifFile();// 判断是否用户文件存在,如果不存在则建立
account=getAccount(); //从文件中读用户
//添加监听器
//屏幕按钮
for(int i=0;i<b.length;i++){
b[i].addActionListener(new bHander());
}
//取钱、查卡
moneyBut.addActionListener(new MoneyHander());
cardBut.addActionListener(new InCardHander());
//数字键
for(int i=0;i<opb.length;i++){
opb[i].addActionListener(new NumHander(opb[i].getText()));
}
//操作键
correct.addActionListener(new CorrectHander());
cancle.addActionListener(new CancleHander());
certain.addActionListener(new CertainHander());
}
public static void main(String args[]){
MainFrame awin=new MainFrame();
awin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
// 操作界面的显示变更-----------------------------------------
public void mainVisible(){
switch (type){
case 1://初始状态
hintLab.setText("欢迎使用,请插卡");
setBoolean(false,false,false,false,false,false,false,false);
break;
case 2://欢迎界面 输入密码
hintLab.setText("请输入密码");
setBoolean(true,false,false,false,false,false,false,false);
break;
case 6://密码正确选择操作
hintLab.setText("请选择");
setBoolean(false,true,true,true,false,false,true,false);
break;
case 3://取钱界面
hintLab.setText("请输入提取金额");
setBoolean(true,false,false,false,false,false,false,false);
break;
case 4://查询界面
hintLab.setText("你的余额是:"+888);/////////////////////
setBoolean(false,false,false,false,false,false,false,true);
break;
case 5://取前结束
hintLab.setText("是否打印凭条");
setBoolean(false,false,false,false,true,true,false,false);
break;
case 7://密码修改界面
hintLab.setText("请输入新密码");
setBoolean(true,false,false,false,false,false,false,true);
break;
case -1://密码三次错误
hintLab.setText("密码三次错误吞卡");
setBoolean(false,false,false,false,false,false,false,false);
break;
default:
break;
}
}
public void setBoolean(boolean t,boolean h1,boolean h2,boolean h3,boolean h4,boolean h5,boolean h6,boolean h7){
inputText.setVisible(t);
this.hl1.setVisible(h1);
this.hl2.setVisible(h2);
this.hl3.setVisible(h3);
this.hl4.setVisible(h4);
this.hl5.setVisible(h5);
this.hl6.setVisible(h6);
this.hl7.setVisible(h7);
}
//监听器的设定---------------------------------------
//屏幕旁按钮
class bHander implements ActionListener{
public void actionPerformed(ActionEvent e) {
if(e.getSource()==b[0]&&type==6){//选择取钱
type=3;
mainVisible();
}
if(e.getSource()==b[1]&&(type==7||type==4||type==3)){//选择返回
type=6;
mainVisible();
}
if(e.getSource()==b[2]&&type==6){//选择退卡
type=1;
mainVisible();
cardBut.setText("[__插卡处__]");
}
if(e.getSource()==b[3]&&type==6){//选择修改密码
type=7;
mainVisible();
}
if(e.getSource()==b[4]&&type==6){//选择修改密码
type=4;
mainVisible();
hintLab.setText("你的余额是:"+account.getMoney());
}
if(e.getSource()==b[6]&&type==5){//选择打印凭条
JOptionPane.showMessageDialog(null,str);
cardBut.setText("[__插卡处__]");
type=1;
mainVisible();
}
if(e.getSource()==b[7]&&type==5){//选择不打印凭条
cardBut.setText("[__插卡处__]");
type=1;
mainVisible();
}
}
}
//插卡按钮
class InCardHander implements ActionListener{
public void actionPerformed(ActionEvent e) {
type=2;
mainVisible();
cardBut.setText("[_卡已插入_]");
inputText.setText("");
readPass=1;
}
}
//取钱按钮
class MoneyHander implements ActionListener{
public void actionPerformed(ActionEvent e) {
moneyBut.setText("[____________取钱处____________]");
}}
//数字按钮
class NumHander implements ActionListener{
String temp;
public NumHander(String a){
temp=a;
}
public void actionPerformed(ActionEvent e) {
inputText.setText(inputText.getText()+temp);
}
}
//确定按钮
class CertainHander implements ActionListener{
public void actionPerformed(ActionEvent e) {
//确任密码
if(type==2){
if(Double.parseDouble(inputText.getText())==account.getPassword()){
type=6;
mainVisible();
}else if(readPass!=3){
inputText.setText("");
hintLab.setText("密码错误请重新输入");
readPass++;
}else{
hintLab.setText("三次密码输入错误,吞卡");
type=-1;
mainVisible();
}
}
//确认取钱
if(type==3){
double temp=Double.parseDouble(inputText.getText());
if(account.getMoney()>=temp){
downMoney(temp);//取钱操作
str="本次取款:"+temp+"\n"+account.toString();//做凭条纪录
moneyBut.setText("[__________钱出来了____________]");
type=5;
mainVisible();
}else{
type=3;
mainVisible();
hintLab.setText("余额不足,请重新输入");
}
}
//密码修改确认
if(type==7){
// while(true){
if(inputText.getText().length()==6){
if(strtemp.length()==0){//是否第一次输入密码
strtemp=inputText.getText();
hintLab.setText("请再次输入新密码");
}else if(Integer.parseInt(strtemp)==
Integer.parseInt(inputText.getText())){
setPassword(Integer.parseInt(strtemp));//调用修改密码方法
type=6;
mainVisible();
hintLab.setText("密码修改成功");
}else{
hintLab.setText("确认密码错误,重新输入");
strtemp="";
}
}else{
hintLab.setText("需要6位密码");
}
}
inputText.setText("");
}
}
//更正按钮
class CorrectHander implements ActionListener{
public void actionPerformed(ActionEvent e) {
inputText.setText("");
}
}
//取消按钮
class CancleHander implements ActionListener{
public void actionPerformed(ActionEvent e) {
if(type>2){
type=6;
mainVisible();
}
inputText.setText("");
}
}
//文件的操作---------------------------------------
public void ifFile(){
//判断密码文件是否存在,如果不存在则创建,并初始化密码为:666666
if(!file.exists()){
try{
objectOut = new ObjectOutputStream(
new BufferedOutputStream(
new FileOutputStream( file ) ) );
objectOut.writeObject(new Account("123456","张三",100000,666666));
}catch(Exception e){
System.out.println("异常");
e.printStackTrace();
}
JOptionPane.showMessageDialog(null, "由于系统文件丢失,密码初始化为:666666\n"+
" 余额初始化为:100000");
try{
objectOut.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
// 从硬盘中读用户
public Account getAccount(){
Account temp=null;
try{
objectIn = new ObjectInputStream(
new BufferedInputStream(
new FileInputStream( file ) ) );
temp=(Account)objectIn.readObject();
}catch(Exception e){
e.printStackTrace();
System.out.println("NO------intput");
}
try{
objectIn.close();
}catch(Exception e){
System.out.println("读用户失败");
e.printStackTrace();
}
return temp;
}
//往硬盘中写用户
public void setAccount(){
try{
objectOut = new ObjectOutputStream(
new BufferedOutputStream(
new FileOutputStream( file ) ) );
objectOut.writeObject(account);
}catch(Exception e){
e.printStackTrace();
}
try{
objectOut.close();
}catch(Exception e){
e.printStackTrace();
}
}
//取钱********************************************************
public void downMoney(double m){
account.setMoney(m);
//保存取钱后结果
setAccount();
}
//修改密码*****************************************************
public void setPassword(int m){
account.setPassword(m);
//保存密码更改后结果
setAccount();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -