📄 test.java
字号:
import java.awt.*;
import java.io.*;
import java.awt.event.*;
import java.util.*;
//////////////学生类///////////////////////
class student
{
public String number;
public String name;
public int mathscore;
public int englishscore;
public int javascore;
public student()
{
number="";
name="无名氏";
mathscore=0;
englishscore=0;
javascore=0;
}
public student(String number1,String name1,int mathscore1,int englishscore1,int javascore1)
{
this.number=number1;
this.name=name1;
this.mathscore=mathscore1;
this.englishscore=englishscore1;
this.javascore=javascore1;
}
}
///////////////////用户类///////////////////////
class user
{
public String name;
public String secret;
public String email;
public String telephone;
public user()
{
name="无名氏";
secret=null;
email=" ";
telephone=null;
}
public user(String name1,String secret1,String email1,String telephone1)
{
this.name=name1;
this.secret=secret1;
this.email=email1;
this.telephone=telephone1;
}
}
/////////////////成绩系统类////////////////////
class Source_System extends Panel implements ActionListener
{
Button b1,b2,b3,b4;
static int n=0;
public static int geshu()
{ try
{
FileReader file=new FileReader("x.dat");
LineNumberReader in=new LineNumberReader(file);
while(in.readLine()!=null)
{
n=in.getLineNumber()/5;
}
}catch(IOException a){ };
return n;
}
Label L1,L2,L3,L4,L5;
TextField T1,T2,T3,T4,T5;
TextArea T6;
Panel draw;
public Source_System(Panel draw)
{
this.draw=draw;
L1=new Label("学号");
L2=new Label("姓名");
L3=new Label("数学");
L4=new Label("外语");
L5=new Label("java");
T1=new TextField(20);
T2=new TextField(20);
T3=new TextField(20);
T4=new TextField(20);
T5=new TextField(20);
T6=new TextArea(4,30);
b1=new Button("录入");
b2=new Button("计算每位学生的平均成绩");
b3=new Button("计算数学平均分");
b4=new Button("学生人数");
add(L1);add(T1);
add(L2);add(T2);
add(L3);add(T3);
add(L4);add(T4);
add(L5);add(T5);
add(b1);add(b2);
add(b3);add(b4);add(T6);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
String label=e.getActionCommand();
if(label.equals("录入"))
{
try
{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
RandomAccessFile out=new RandomAccessFile("x.dat","rw");
if(T1.getText().length()==0||T2.getText().length()==0||T3.getText().length()==0||T4.getText().length()==0||T5.getText().length()==0)
{
T6.setText("输入有错,请重新输入!");
}
else
{
student s1=new student(T1.getText(),T2.getText(),Integer.parseInt(T3.getText()),Integer.parseInt(T4.getText()),Integer.parseInt(T5.getText()));
out.seek(out.length());
out.writeBytes(s1.number+"\n");
out.writeBytes(s1.name+"\n");
out.writeBytes(s1.mathscore+"\n");
out.writeBytes(s1.englishscore+"\n");
out.writeBytes(s1.javascore+"\n");
T6.setText("录入成功!");
}
}catch (IOException a){ };
}
else if(label.equals("计算每位学生的平均成绩"))
{
int pingjun=0;
String no;
String na;
int maths;
int englishs;
int javas;
try
{
FileReader file=new FileReader("x.dat");
LineNumberReader in=new LineNumberReader(file);
/*while(in.readLine()!=null)
{
n=in.getLineNumber()/5;
}*/
boolean eof=false;
while(!eof)
{
T6.setText("计算每位学生的平均成绩为:"+"\n学号:");
for(int i=0;i<10;i++)
{
no=in.readLine();
if(no==null)
{
eof=true;
}
else
{
na=in.readLine();
maths=Integer.parseInt(in.readLine());
englishs=Integer.parseInt(in.readLine());
javas=Integer.parseInt(in.readLine());
pingjun=(maths+englishs+javas)/3;
T6.append(no+"\n姓名:"+na+"\n平均分:"+pingjun+"\n");
}
}
}
}catch(IOException a){ };
}
else if(label.equals("计算数学平均分"))
{
int mathsjun=0;//数学平均分
int count=0;
String no;
String na;
int maths;
int englishs;
int javas;
try
{
FileReader file=new FileReader("x.dat");
LineNumberReader in=new LineNumberReader(file);
boolean eof=false;
while(!eof)
{
for(int i=0;i<10;i++)
{
count++;
no=in.readLine();
if(no==null)
{
eof=true;
}
else
{
na=in.readLine();
maths=Integer.parseInt(in.readLine());
englishs=Integer.parseInt(in.readLine());
javas=Integer.parseInt(in.readLine());
mathsjun+=maths;
T6.append("数学平均成绩为:"+mathsjun);
}
}
}
}catch(IOException a){ };
T6.setText("数学平均成绩为:"+mathsjun/geshu());
}
else if(label.equals("学生人数"))
{
T6.setText("学生总人数为:"+geshu());
}
}
}
/////////////主类/////////////////////
public class test extends Frame
{
TextArea t1=new TextArea("恭喜你!注册成功!",5,5);
boolean ok=false;
Label username=new Label("用户名 ");
Label secreat=new Label("密码 ");
Label email=new Label("E-mail地址 ");
Label telephone=new Label("联系电话 ");
Label secreatqueit=new Label("密码不能少于6位");
//Button b2=new Button("密码检测");
Label emailqueit=new Label("E-mail地址要求包括@符号");
//Button b3=new Button("邮箱检测");
Label telephonequeit=new Label("联系电话不能有非数字");
// Button b4=new Button("联系电话检测");
TextField usernamefield=new TextField();
TextField secreatfield=new TextField();
TextField emailfield=new TextField();
TextField telephonefield=new TextField();
Button b1=new Button("注册");
Button b2=new Button("确定");
Button b3=new Button("登录");
Dialog my;
//Dialog Enter;//登录后的对话框
Label Gongxi=new Label("恭喜你!注册成功!");
boolean count1=false;//记录密码验证正确与否
boolean count2=false;//记录邮箱验证正确与否
boolean count3=false;//记录联系方式验证正确与否
/****对所要添加的组件进行设置****/
public void set()
{
setLayout(new GridBagLayout());
GridBagConstraints gridBag=new GridBagConstraints();
gridBag.fill=GridBagConstraints.HORIZONTAL;
gridBag.weightx=0;
gridBag.weighty=0;
username.setFont(new Font("Alias",Font.BOLD,16));
this.add(username,gridBag,0,0,1,1);
secreat.setFont(new Font("Alias",Font.BOLD,16));
this.add(secreat,gridBag,0,1,1,1);
email.setFont(new Font("Alias",Font.BOLD,16));
this.add(email,gridBag,0,2,1,1);
telephone.setFont(new Font("Alias",Font.BOLD,16));
this.add(telephone,gridBag,0,3,1,1);
telephone.setFont(new Font("Alias",Font.BOLD,16));
this.add(secreatqueit,gridBag,3,1,1,1);
telephone.setFont(new Font("Alias",Font.BOLD,16));
this.add(emailqueit,gridBag,3,2,1,1);
telephone.setFont(new Font("Alias",Font.BOLD,16));
this.add(telephonequeit,gridBag,3,3,1,1);
gridBag.weightx=100;
gridBag.weighty=0;
this.add(usernamefield,gridBag,1,0,2,1);
this.add(secreatfield,gridBag,1,1,2,1);
this.add(emailfield,gridBag,1,2,2,1);
this.add(telephonefield,gridBag,1,3,2,1);
//gridBag.fill=GridBagConstraints.BOTH;
gridBag.weightx=100;
gridBag.weighty=100;
this.add(b1,gridBag,1,5,1,1);
this.add(b3,gridBag,2,5,1,1);
//this.add(b2,gridBag,0,4,1,1);
//this.add(b3,gridBag,1,4,1,1);
//this.add(b4,gridBag,2,4,1,1);
setSize(500,500);
setVisible(true);
secreatfield.setEchoChar('*');
/*********对密码的合法与否进行检测********/
b1.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String s;
s=secreatfield.getText();
if(s.length()>=6)
{
count1=true;
secreatqueit.setText("密码合法!");
}
else
{
count1=false;
secreatfield.setText("");
secreatqueit.setText("密码少于六位,请重新输入!");
}
}
});
/********对邮箱格式的检测********/
b1.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String s;
s=emailfield.getText();
int n=s.length();
char a[]=new char[n];
for(int i=0;i<n;i++)
a[i]=s.charAt(i);
for(int i=0;i<n;i++)
{
if(a[i]=='@')
{
emailqueit.setText("邮箱地址合法");
count2=true;
break;
}
else
{
count2=false;
emailqueit.setText("邮箱地址不合法,请重新输入!");
}
}
}
});
/****************对联系电话的检测****************/
b1.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String s;
s=telephonefield.getText();
int n=s.length();
char a[]=new char[n];
for(int i=0;i<n;i++)
a[i]=s.charAt(i);
for(int i=0;i<n;i++)
{
if(!Character.isDigit(a[i]))
{
telephonefield.setText("");
telephonequeit.setText("联系方式不合法,请重新输入!");
count3=false;
break;
}
else
{
count3=true;
telephonequeit.setText("联系方式合法");
}
}
}
});
/***********编写弹出式对话框11***********/
b1.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(count1&&count2&&count3)
{
my=new Dialog(test.this,"恭喜对话框",true);
my.setLayout(new BorderLayout());
my.add("South",b2);
my.add("North",t1);
my.add("West",Gongxi);
my.setSize(200,200);
my.setVisible(true);
ok=true;
}
}
});
b2.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//***********编写对用户信息进行存储**************/
try
{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
RandomAccessFile out=new RandomAccessFile("y.dat","rw");
if(!ok)
{
user u1=new user(usernamefield.getText(),secreatfield.getText(),emailfield.getText(),telephonefield.getText());
out.seek(out.length());
out.writeBytes(u1.name+"\n");
out.writeBytes(u1.secret+"\n");
out.writeBytes(u1.email+"\n");
out.writeBytes(u1.telephone+"\n");
}
else
{
t1.setText("输入有错,请重新输入!");
}
}catch (IOException a){ };
//////清空文本框///////
usernamefield.setText("");
secreatfield.setText("");
emailfield.setText("");
telephonefield.setText("");
my.dispose();
}
});
/***************编写登录后的对话框**************/
b3.addActionListener(new java.awt.event.ActionListener()
{
String uname[]=new String[10];
String usecreat[]=new String[10];
String uemail[]=new String[10];
String utelephone[]=new String[10];
boolean ok=false;
public void actionPerformed(ActionEvent e)
{
/*******比较文本框中的用户是否已注册,是的话把ok置为true*****/
int h=0;//用户存在的标志位;
String una;
String usecreat;
String uemail;
String utelephone;
try
{
FileReader file=new FileReader("y.dat");
LineNumberReader in=new LineNumberReader(file);
boolean eof=false;
while(!eof)
{
for(int i=0;i<10;i++)
{
una=in.readLine();
if(una==null)
{
eof=true;
my=new Dialog(test.this,"无用户纪录",true);
my.setLayout(new BorderLayout());
my.add("South",b2);
my.add("North",t1);
t1.setText("对不起,该用户不存在!");
my.setSize(200,200);
my.setVisible(true);
}
else
{
// una=in.readLine();
usecreat=in.readLine();
uemail=in.readLine();
utelephone=in.readLine();
if((usernamefield.getText().startsWith(una))&&(secreatfield.getText().startsWith(usecreat)))
{
eof=true;
ok=true;
break;
}
else
{
my=new Dialog(test.this,"错误",true);
my.setLayout(new BorderLayout());
my.add("South",b2);
my.add("North",t1);
t1.setText("对不起,该用户不存在!");
my.setSize(200,200);
my.setVisible(true);
eof=true;
i=11;
}
}
}
}
}catch(IOException a){ };
if(ok)
{
Frame f=new Frame("学生成绩系统");
Panel draw=new Panel();
Source_System two=new Source_System(draw);
f.add(two);
f.setSize(250,350);
f.setVisible(true);
}
}
});
}
private void add(Component c, GridBagConstraints gbc, int x, int y, int w, int h)
{
gbc.gridx =x;
gbc.gridy =y;
gbc.gridheight = h;
gbc.gridwidth = w;
add(c, gbc);
}
public static void main(String args[])
{
test t1=new test();
t1.set();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -