⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testa.java

📁 java制作学生查询系统
💻 JAVA
字号:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
public class testA extends Applet implements ActionListener
{ 		
    Image my;
	Label password=new Label("密码(先回车再输入密码)");
	TextField in=new TextField(6);
	Label user=new Label("用户名");
	Button OK=new Button("确定");
	TextField input=new TextField(6);
	Label prompt1=new Label("班级");
	TextField input1=new TextField(6);
	Label prompt2=new Label(" 姓名");
	TextField input2=new TextField(6);
	Label prompt3=new Label("学号");
	TextField input3=new TextField(6);
	Label prompt4=new Label("成绩");
	TextField input4=new TextField(6);
	Label prompt6=new Label("                                                                                          ");
	Button addrecord=new Button("增加记录");
	Button delete=new Button("删除记录");
	Button search=new Button("查找记录");
	Label s=new Label("                                        ");	
        String pw="";  //创建字符串对象
        String dy="";
        String ad="";
        String de="";
        String sh="";
        String msg=""; //查找结果信息  
        String ok="";
        Student [] message=new Student [100];//记录学生信息的数组
        int ComparePt[]=new int[100];//保存对分法的中间点
        int i;   //记录个数
        int j=0; //当前记录的个数
        int k;   //当前记录的位置
	public void init()        //初始化
	{	
	    my=getImage(getDocumentBase(),"1.gif");//图像显示
		add(user);add(in);
		add(password);
		add(input);
		add(OK);add(s);
		add(prompt1);
		add(input1);
		add(prompt2);		
		add(input2);
		add(prompt3);
		add(input3);
		add(prompt4);
		add(input4);
        add(prompt6);
		add(addrecord);
		add(delete);
		add(search);
    addrecord.setVisible(false);//初始值为false,开始不可见
	delete.setVisible(false);
	search.setVisible(false);
	prompt1.setVisible(false);
	input1.setVisible(false);
	prompt2.setVisible(false);
	input2.setVisible(false);
	prompt3.setVisible(false);
	input3.setVisible(false);
	prompt4.setVisible(false);		
	input4.setVisible(false);
		for(i=0;i<100;i++)            //添加记录的循环
              {message[i]=new Student("","","","");}
        input.addActionListener(this);
		addrecord.addActionListener(this);
		delete.addActionListener(this);
		search.addActionListener(this);
		OK.addActionListener(this);}
	public void actionPerformed(ActionEvent e)      //按钮的响应
	{
	     if(e.getActionCommand()=="确定")   
		{
			if(pw.equals("830113"))	              //当密码一致时其它信息变为可见
		{
		   addrecord.setVisible(true);
	       delete.setVisible(true);
	       search.setVisible(true);
	       prompt1.setVisible(true);
	       input1.setVisible(true);
	       prompt2.setVisible(true);
	       input2.setVisible(true);
	       prompt3.setVisible(true);
	       input3.setVisible(true);
	       prompt4.setVisible(true);	
           input4.setVisible(true);
		   repaint();								}       		
			else 
			{s.setText("密码输入错误,登陆失败!");
				  repaint();
				  }		
				  }
        if(e.getSource()==input)       //把密码用*号代替
		{		input.setEchoChar('*');
		        pw=input.getText();
		        repaint();
	        }
		if(e.getActionCommand()=="增加记录")   //增加记录
		{
			message[j].cn=input1.getText();
			message[j].n=input2.getText();
			message[j].sn=input3.getText();
			message[j].s=input4.getText();
			j++;
			dy="显示";                  //增加的同时显示记录
			repaint();		
			}
		if(e.getActionCommand()=="删除记录")   //自底向上删除记录
		{
			if(j>=1)
		{   j=--j;
			message[j].cn="";
			message[j].n="";
			message[j].sn="";
			message[j].s="";
			dy="显示";
			repaint();
		        }
		        else de="删除失败";			
		        dy="显示";
			repaint();	
				}
		if(e.getActionCommand()=="查找记录")   //查找记录
		{
			sh="查找";
			int k=BiSearch(input3.getText(),j);//按学号查找记录
			if(k==-1)                          //没找到
			msg="序列中没有匹配的数据";
			else
			msg="匹配的数据在序列的第"+k+"个记录中";//显示记录的位置
			dy="显示";
			repaint();
			}	
			}
	public void paint(Graphics g)
	{	  g.drawImage(my,100,450,this);//图像显示位置
	        if(pw.equals("830113"))
	        {
	        g.drawString("密码正确,登录成功,请最大化",60,430);
	        if(dy.equals("显示"))  //画出信息表
	        {
	        g.drawString("班级",100,100);
	        g.drawString("学号",300,100);
	        g.drawString("姓名",500,100);
	        g.drawString("成绩",700,100);
	        g.drawString("当前记录数:"+j,400,75);
	        for(k=0;k<j;k++)  //每添加一个记录所在的位置
	        {
	        g.drawString(message[k].cn,100,120+k*20);
	        g.drawString(message[k].n,300,120+k*20);
	        g.drawString(message[k].sn,500,120+k*20);
	        g.drawString(message[k].s,700,120+k*20);
	        }
	        if(de.equals("删除失败"))  
	        {
	        	g.drawString("已经没有记录了,删除失败",250,460);
	        }
	        if(sh.equals("查找"))
	        {
	        	g.drawString(msg,250,500);} } }}
int BiSearch(String Key,int high)//二分查找算法
{
	int low=0;
	int mid;
	int i=0;
	while(low<=high)
	{
		mid=(high+low)/2;
		ComparePt[i++]=Integer.parseInt(message[mid].sn);
		if((message[mid].sn).equals(Key))
		return mid+1;
		else if((message[mid].sn).compareTo(Key)==-1)
		low=mid+1;
		else
		high=mid-1;	
		}
	    return -1;
	    }	
	    }
class Student  //定义学生类
{
 public String cn;//班级
 public String n;//姓名
 public String sn;//学号 
 public String s;//成绩
 public Student(String classnumber,String name,String studentnumber,String score)
 {
 	cn=classnumber;
 	n=name;
 	sn=studentnumber;
 	s=score;}
 public String getcn()
 {
 	return cn; 
 }
 }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -