刚学JAVA不久,模拟了学校的校园卡系统,但是现在越来越发现自己的在面向对象这个思想上,自己也搞不清楚,到底自己是怎么想的,我把我写的那个代码附上,请大家指点下我的思路
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.awt.Graphics;
public class j03127e3Xiaoyuanka//Application主函数
{
public static void main(String args[])
{
new Denglu();
}
}class Student//定义一个学生类
{
static String id=new String("1103101127");
static String name=new String("伍鑫");
static String address=new String("湖北省武穴市枚川镇");
static String grade=new String("计031班");
static protected String password="1103101127";
static double money=200.0;} class Denglu extends Frame implements ActionListener//登录界面
{ Main Mainwindow;//声明 Label n1,n2,n3;
TextField txt1,txt2;
Button btn1,btn2;
Denglu()
{
super("校园卡管理系统");
Mainwindow=new Main();
setLayout(null);
n1=new Label("请输入学号:");
n1.setBounds(20,40,80,25);
add(n1);
txt1=new TextField(10);
txt1.setBounds(100,40,80,20);
add(txt1);
n2=new Label("请输入密码:");
n2.setBounds(20,70,80,25);
add(n2);
txt2=new TextField(10);
txt2.setBounds(100,70,80,20);
txt2.setEchoChar('*');
add(txt2); btn1=new Button("确定");
btn1.setBounds(40,100,50,20);
btn2=new Button("退出");
btn2.setBounds(110,100,50,20);
add(btn1);
add(btn2);
btn1.addActionListener(this);
btn2.addActionListener(this);
n3=new Label(" ");
n3.setBounds(20,120,150,25);
add(n3);
setSize(215,180);
setLocation(400,200);
show();
}
public void actionPerformed(ActionEvent e)//按纽事件
{
if(e.getSource()==btn2)//根据不同的按纽做不同的操作
{
setVisible(false);
}
else
{
if((txt1.getText().equals(Student.id))&&(txt2.getText().equals(Student.password)))//判断学号和密码是否一致
{
Mainwindow.show();
txt1.setText("");txt2.setText("");
}
else
n3.setText("请输入正确的学号和密码!");
}
}
}
class Main extends Frame implements ActionListener
{
Information Mywindow1;//声明
Changepassword Mywindow2;
Payment Mywindow3;
Sign_up Mywindow4;
Chongqian Mywindow5;
Button btn1,btn2,btn3,btn4,btn5,btn6;
Main()
{
super("操作窗口");
Mywindow1=new Information();
Mywindow2=new Changepassword();
Mywindow3=new Payment();
Mywindow4=new Sign_up();
Mywindow5=new Chongqian(); setLayout(new GridLayout(6,1,5,8));
btn1=new Button("基本信息");
add(btn1);
btn2=new Button("密码修改");
add(btn2);
btn3=new Button("消费");
add(btn3);
btn6=new Button("自助充值");
add(btn6);
btn4=new Button("考试报名");
add(btn4);
btn5=new Button("退出");
add(btn5);
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
btn4.addActionListener(this);
btn5.addActionListener(this);
btn6.addActionListener(this); setSize(300,300);
setLocation(400,200);
} public void actionPerformed(ActionEvent e)
{
if(e.getSource()==btn1)//根据不同的按纽做不相应的操作
{
Mywindow1.show();
}
if(e.getSource()==btn2)
{
Mywindow2.show();
}
if(e.getSource()==btn3)
{
Mywindow3.show();
}
if(e.getSource()==btn4)
{
Mywindow4.show();
}
if(e.getSource()==btn5)
{
dispose();
}
if(e.getSource()==btn6)
{
Mywindow5.show();
}
}
}
class Information extends Frame implements ActionListener
{
Label n1,n2;
TextField txt1,txt2;//,txt3;
TextArea p1; Button btn1,btn2;
Information()
{ super("基本信息");
setLayout(new FlowLayout(FlowLayout.LEFT)); n1=new Label("请输入学号: ");
add(n1);
txt1=new TextField(10);
add(txt1);
n2=new Label("请输入密码: ");
add(n2);
txt2=new TextField(10);
txt2.setEchoChar('*');
add(txt2); btn1=new Button("确定");
btn2=new Button("退出");
add(btn1);
add(btn2);
btn1.addActionListener(this);
btn2.addActionListener(this);
p1=new TextArea(10,30);
add(p1);
//p1.setText("学号:"+j03127e3Xiaoyuanka.id+"\n"+"姓名:"+j03127e3Xiaoyuanka.name+"\n"+"地址:"+j03127e3Xiaoyuanka.address+"\n"+"班级:"+j03127e3Xiaoyuanka.grade+"\n"+"卡内金额:"+j03127e3Xiaoyuanka.money);//这个是打算在这个界面不需要密码,当出现这个界面的时候就显示信息
setSize(300,300);
setBackground(Color.lightGray);
setVisible(false);
setLocation(400,200);
} public void actionPerformed(ActionEvent e)
{
if(e.getSource()==btn2)
{
dispose();
txt1.setText("");txt2.setText("");p1.setText("");
}
else if((txt1.getText().equals(Student.id))&&(txt2.getText().equals(Student.password)))//false
{p1.setText("学号:"+Student.id+"\n"+"姓名:"+Student.name+"\n"+"地址:"+Student.address+"\n"+"班级:"+Student.grade+"\n"+"卡内金额:"+Student.money);
p1.setEditable(false);txt1.setText("");txt2.setText("");}
else
p1.setText("你输入的信息有错误!");txt1.setText("");txt2.setText("");
}
}
(接下面)
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.awt.Graphics;
public class j03127e3Xiaoyuanka//Application主函数
{
public static void main(String args[])
{
new Denglu();
}
}class Student//定义一个学生类
{
static String id=new String("1103101127");
static String name=new String("伍鑫");
static String address=new String("湖北省武穴市枚川镇");
static String grade=new String("计031班");
static protected String password="1103101127";
static double money=200.0;} class Denglu extends Frame implements ActionListener//登录界面
{ Main Mainwindow;//声明 Label n1,n2,n3;
TextField txt1,txt2;
Button btn1,btn2;
Denglu()
{
super("校园卡管理系统");
Mainwindow=new Main();
setLayout(null);
n1=new Label("请输入学号:");
n1.setBounds(20,40,80,25);
add(n1);
txt1=new TextField(10);
txt1.setBounds(100,40,80,20);
add(txt1);
n2=new Label("请输入密码:");
n2.setBounds(20,70,80,25);
add(n2);
txt2=new TextField(10);
txt2.setBounds(100,70,80,20);
txt2.setEchoChar('*');
add(txt2); btn1=new Button("确定");
btn1.setBounds(40,100,50,20);
btn2=new Button("退出");
btn2.setBounds(110,100,50,20);
add(btn1);
add(btn2);
btn1.addActionListener(this);
btn2.addActionListener(this);
n3=new Label(" ");
n3.setBounds(20,120,150,25);
add(n3);
setSize(215,180);
setLocation(400,200);
show();
}
public void actionPerformed(ActionEvent e)//按纽事件
{
if(e.getSource()==btn2)//根据不同的按纽做不同的操作
{
setVisible(false);
}
else
{
if((txt1.getText().equals(Student.id))&&(txt2.getText().equals(Student.password)))//判断学号和密码是否一致
{
Mainwindow.show();
txt1.setText("");txt2.setText("");
}
else
n3.setText("请输入正确的学号和密码!");
}
}
}
class Main extends Frame implements ActionListener
{
Information Mywindow1;//声明
Changepassword Mywindow2;
Payment Mywindow3;
Sign_up Mywindow4;
Chongqian Mywindow5;
Button btn1,btn2,btn3,btn4,btn5,btn6;
Main()
{
super("操作窗口");
Mywindow1=new Information();
Mywindow2=new Changepassword();
Mywindow3=new Payment();
Mywindow4=new Sign_up();
Mywindow5=new Chongqian(); setLayout(new GridLayout(6,1,5,8));
btn1=new Button("基本信息");
add(btn1);
btn2=new Button("密码修改");
add(btn2);
btn3=new Button("消费");
add(btn3);
btn6=new Button("自助充值");
add(btn6);
btn4=new Button("考试报名");
add(btn4);
btn5=new Button("退出");
add(btn5);
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
btn4.addActionListener(this);
btn5.addActionListener(this);
btn6.addActionListener(this); setSize(300,300);
setLocation(400,200);
} public void actionPerformed(ActionEvent e)
{
if(e.getSource()==btn1)//根据不同的按纽做不相应的操作
{
Mywindow1.show();
}
if(e.getSource()==btn2)
{
Mywindow2.show();
}
if(e.getSource()==btn3)
{
Mywindow3.show();
}
if(e.getSource()==btn4)
{
Mywindow4.show();
}
if(e.getSource()==btn5)
{
dispose();
}
if(e.getSource()==btn6)
{
Mywindow5.show();
}
}
}
class Information extends Frame implements ActionListener
{
Label n1,n2;
TextField txt1,txt2;//,txt3;
TextArea p1; Button btn1,btn2;
Information()
{ super("基本信息");
setLayout(new FlowLayout(FlowLayout.LEFT)); n1=new Label("请输入学号: ");
add(n1);
txt1=new TextField(10);
add(txt1);
n2=new Label("请输入密码: ");
add(n2);
txt2=new TextField(10);
txt2.setEchoChar('*');
add(txt2); btn1=new Button("确定");
btn2=new Button("退出");
add(btn1);
add(btn2);
btn1.addActionListener(this);
btn2.addActionListener(this);
p1=new TextArea(10,30);
add(p1);
//p1.setText("学号:"+j03127e3Xiaoyuanka.id+"\n"+"姓名:"+j03127e3Xiaoyuanka.name+"\n"+"地址:"+j03127e3Xiaoyuanka.address+"\n"+"班级:"+j03127e3Xiaoyuanka.grade+"\n"+"卡内金额:"+j03127e3Xiaoyuanka.money);//这个是打算在这个界面不需要密码,当出现这个界面的时候就显示信息
setSize(300,300);
setBackground(Color.lightGray);
setVisible(false);
setLocation(400,200);
} public void actionPerformed(ActionEvent e)
{
if(e.getSource()==btn2)
{
dispose();
txt1.setText("");txt2.setText("");p1.setText("");
}
else if((txt1.getText().equals(Student.id))&&(txt2.getText().equals(Student.password)))//false
{p1.setText("学号:"+Student.id+"\n"+"姓名:"+Student.name+"\n"+"地址:"+Student.address+"\n"+"班级:"+Student.grade+"\n"+"卡内金额:"+Student.money);
p1.setEditable(false);txt1.setText("");txt2.setText("");}
else
p1.setText("你输入的信息有错误!");txt1.setText("");txt2.setText("");
}
}
(接下面)
{
Label n1,n2,n3,n4;
TextField txt1,txt2,txt3;
String str1,str2,str3;
Button btn1,btn2; Changepassword()
{
super("修改密码");
setLayout(null);
n1=new Label("原密码:");
n1.setBounds(80,40,60,20);
add(n1);
txt1=new TextField(10);
txt1.setBounds(150,40,60,20);
txt1.setEchoChar('*');
add(txt1);
n2=new Label("新密码:");
n2.setBounds(80,70,60,20);
add(n2);
txt2=new TextField(10);
txt2.setBounds(150,70,60,20);
txt2.setEchoChar('*');
add(txt2);
n3=new Label("请再次输入:");
n3.setBounds(80,100,60,20);
add(n3);
txt3=new TextField(10);
txt3.setBounds(150,100,60,20);
txt3.setEchoChar('*');
add(txt3);
n4=new Label(" ");
n4.setBounds(60,180,180,20);
add(n4);
btn2=new Button("确定");
btn2.setBounds(80,160,40,20);
add(btn2);
btn2.addActionListener(this);
btn1=new Button("关闭");
btn1.setBounds(160,160,40,20);
add(btn1);
btn1.addActionListener(this); setSize(300,300);
setLocation(400,200);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==btn1)
{
dispose();
txt1.setText("");
txt2.setText("");
txt3.setText("");
n4.setText(""); }
else
{
str1=txt1.getText();str2=txt2.getText();str3=txt3.getText();////
if((str1.equals(Student.password))&&(str2.equals(str3)))
{
if(str2.length()>=6)
{n4.setText("密码修改成功!");
Student.password=str2;
txt1.setText("");txt2.setText("");txt3.setText("");}
else
{n4.setText("密码长度不能小于6位!");txt2.setText("");txt3.setText("");}
}
else
n4.setText("你的输入有错,请再输入!");
}
}
}
class Payment extends Frame implements ActionListener//消费
{
Label n1,n2,n3;
TextField txt1;
double c=0.0;
Button btn1,btn2; Payment()
{
super("消费");
setLayout(null); n2=new Label("这次消费金额: ");
n2.setBounds(50,80,80,20);
add(n2);
txt1=new TextField(10);
txt1.setBounds(160,80,80,20);
add(txt1);
btn2=new Button("确定");
btn2.setBounds(90,120,40,20);
add(btn2);
btn2.addActionListener(this);
btn1=new Button("关闭");
btn1.setBounds(180,120,40,20);
add(btn1);
btn1.addActionListener(this); n1=new Label(" ");
n1.setBounds(80,140,160,20);
add(n1);
setSize(300,300);
setLocation(400,200);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==btn1)
{
dispose();
n1.setText("");
}
else
{
try{
c=Double.valueOf(txt1.getText()).doubleValue();
if(c>=0)
{
if(c<=Student.money)
{
n1.setText(""+(Student.money-c));
Student.money=Student.money-c;
txt1.setText("");
}
else
{txt1.setText("");
n1.setText("你卡内的金额不足,请充钱!");
}
}
else
n1.setText("输入金额不能为负数!");txt1.setText("");
}catch(Exception e1){n1.setText("输入含有非法字符!");txt1.setText("");}//异常处理,当输入不是数字的时候提示出错!
}
}
}
class Sign_up extends Frame implements ActionListener//交纳学费、等级考试费用
{ Label n1,n2;
TextField txt;
Button btn1,btn2;
Sign_up()
{
super("交纳费用");
setLayout(null);
n1=new Label("请选择你要交纳的费用:");
n1.setBounds(20,100,120,20);
add(n1);
txt=new TextField(10);
txt.setBounds(160,100,60,20);
add(txt);
btn1=new Button("确定");
btn1.setBounds(80,140,40,20);
add(btn1);
btn1.addActionListener(this);
btn2=new Button("退出");
btn2.setBounds(160,140,40,20);
add(btn2);
btn2.addActionListener(this);
n2=new Label(" ");
n2.setBounds(35,180,240,20);
add(n2);
setSize(300,300);
setLocation(400,200);
}
public void paint(Graphics g)
{
g.drawString("1.四、六级考试报名",80,50);
g.drawString("2.计算机等级考试报名",80,70);
g.drawString("3.学费",80,90);
} public void actionPerformed(ActionEvent e)
{
if(e.getSource()==btn2)
{
dispose();
n2.setText("");
n1.setText("");
}
else
{
try{
int i;
i=Integer.parseInt(txt.getText());
switch(i)
{
case 1:
if(Student.money>=30.0)
{
Student.money-=30.0;
n2.setText("报名成功!"+"\n"+"卡内余额:"+Student.money);
}
else
n2.setText("卡内金额不足!");
break;
case 2:
if(Student.money>40.0)
{
Student.money-=40.0;
n2.setText("报名成功!");
}
else
n2.setText("卡内金额不足!");
break;
case 3:
if(Student.money>4400.0)
{
Student.money-=4400.0;
n2.setText("交费成功!");
}
else
n2.setText(" 卡内金额不足!");
break;
default:
n2.setText("没有这项操作!");
}
}catch(Exception e2){n2.setText("输入含有非法字符,请确认你输入的为数字!");txt.setText("");}//错误信息处理
}
}
}
class Chongqian extends Frame implements ActionListener
{
Label n1,n2;
TextField txt1;
Button btn,btn1; Chongqian()
{
super("自助充值");
setLayout(null);
n1=new Label("请输入你的充值金额:");
n1.setBounds(20,40,120,25);
add(n1);
txt1=new TextField(10);
txt1.setBounds(140,40,60,20);
add(txt1);
btn=new Button("确定");
btn.setBounds(80,80,40,20);
add(btn);
btn1=new Button("取消");
btn1.setBounds(140,80,40,20);
add(btn1);
btn1.addActionListener(this);
btn.addActionListener(this);
n2=new Label(" ");
n2.setBounds(40,120,200,25);
add(n2);
setSize(300,300);
setLocation(400,200);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==btn1)
{
dispose();
n2.setText("");
}
else
if(e.getSource()==btn)
{
try{
double x;
x=Double.valueOf(txt1.getText()).doubleValue();
if(x>=0)
{Student.money=Student.money+x;
n2.setText("充值成功!卡内金额为"+Student.money);
}
else
n2.setText("金额不能为负数!");txt1.setText("");
}catch(Exception e4){n2.setText("操作中含有非法字符!请再输入!");txt1.setText("");}//错误信息处理 }
}
}最近是越来越迷糊了,看了好多JAVA方面的书也看了好多别人的帖子,自己也写了些,但是还是很迷糊
纠缠在一起就纠缠出问题来了,越看越烦