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

📄 land.java

📁 考试系统 实现在线考试中的各项操作的源代码
💻 JAVA
字号:

package com.exam;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Land implements ActionListener
{
	private ManageUser newUser;
	private JFrame frame;
	private Container contentPane;
	//Land界面元素
	private JButton clearBtn,newBtn,okBtn,exitBtn;
	private JTextField userText;
	private JPasswordField pwdText;//显示密码
	
	//NewUser界面
	private JButton clearBtn1,okBtn1,exitBtn1;
	private JTextField userText1;
	private JPasswordField pwdText1,pwdText2;//显示密码
//—————————————————————————————————	
	public Land()
	{	
		frame=new JFrame("用户登陆");
		frame.setBounds(300,170,300,170);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//设置大小可不可以改变
		frame.setResizable(false);
		contentPane=frame.getContentPane();
		initGUI();
	}
//********************************************************
	public void initGUI()
	{
		contentPane.setLayout(new CardLayout());
		contentPane.add(getPanel(1),"");
	}
//————————————————————————————————
	public void selectPanel(int id)
	//图片管理及加载(到contentPane)
	{
		contentPane.add(getPanel(id),"");
		CardLayout c=(CardLayout)contentPane.getLayout();
		c.next(contentPane);
	}
//————————————————————————————————
	public JPanel getPanel(int id)
	//专门用于按照id生产图片的工厂
	{
		JPanel pAll=null;
		switch(id)
		{

//@@@@@
		//产生Land界面
		case 1:
		{
		pAll=new JPanel(new BorderLayout());
				//用户名
		userText=new JTextField(10);
		JPanel p1=new JPanel(new FlowLayout());
		p1.add(new JLabel("用户名:"));
		p1.add(userText);
		
		//密码
		pwdText=new JPasswordField(10);
		JPanel p2=new JPanel(new FlowLayout());
		p2.add(new JLabel("密   码 :"));
		p2.add(pwdText);
		
		//按钮
		clearBtn=new JButton("清除");
		newBtn=new JButton("注册");
		okBtn=new JButton("确定");
		exitBtn=new JButton("退出");
		JPanel p3=new JPanel(new FlowLayout());
		p3.add(clearBtn);
		p3.add(newBtn);	
		p3.add(okBtn);
		p3.add(exitBtn);

		JPanel p=new JPanel(new GridLayout(2,1));
		p.add(p1);
		p.add(p2);
		pAll.add(p,BorderLayout.CENTER);	
		pAll.add(p3,BorderLayout.SOUTH);
	
		clearBtn.addActionListener(this);
		newBtn.addActionListener(this);
		okBtn.addActionListener(this);
		exitBtn.addActionListener(this);

		userText.addActionListener(this);
		pwdText.addActionListener(this);
		}break;
//————————————————————————————————————————
//@@@@@
		//产生NewUser界面
		case 2:
		{
		pAll=new JPanel(new BorderLayout());
		//用户名
		userText1=new JTextField(10);
		JPanel p1=new JPanel(new FlowLayout());
		p1.add(new JLabel("用 户 名  :"));
		p1.add(userText1);
		//密码
		pwdText1=new JPasswordField(10);
		JPanel p2=new JPanel(new FlowLayout());
		p2.add(new JLabel(" 密       码 :"));
		p2.add(pwdText1);
		//密码验证
		pwdText2=new JPasswordField(10);
		JPanel p3=new JPanel(new FlowLayout());
		p3.add(new JLabel("密码验证 :"));
		p3.add(pwdText2);
		//按钮
		clearBtn1=new JButton("清除");
		okBtn1=new JButton("确定");
		exitBtn1=new JButton("返回");
		JPanel p4=new JPanel(new FlowLayout());
		p4.add(clearBtn1);	
		p4.add(okBtn1);
		p4.add(exitBtn1);

		JPanel p=new JPanel(new GridLayout(3,1));
		p.add(p1);
		p.add(p2);
		p.add(p3);

		pAll.add(p,BorderLayout.CENTER);	
		pAll.add(p4,BorderLayout.SOUTH);
	
		clearBtn1.addActionListener(this);
		okBtn1.addActionListener(this);
		exitBtn1.addActionListener(this);

		userText1.addActionListener(this);
		pwdText1.addActionListener(this);
		pwdText2.addActionListener(this);
		}break;
		}
		return pAll;
	}
//————————————————————————————————————————
	//事件处理类
	public void actionPerformed(ActionEvent e)
	{
		//对文本框进行监听,一回车进填写密码项
		if(e.getSource()==userText1)
		{
			if(!checkName())
				return;	
		}
		//当一回车时自动登陆
		if(e.getSource()==pwdText||e.getSource()==okBtn)
		{
			//如果登陆用户名为空的话直接返回
			if(!checkName())
				return;
			//若密码为空的话提示密码不能为空
			if(pwdText.getText().trim().length()==0)
			{
				(new InformFrame(1,"密码名不能为空")).go();
				pwdText.setText("");
				pwdText.grabFocus();
			}
			//若密码拥护名都不为空的话进行验证
			else
			{
				//用户名和密码的验证
				String name=userText.getText().trim();
				String pwd=pwdText.getText().trim();
				//若次用户名和密码存在的话就进入答题系统
				if(ManageUser.isFind(name,pwd))
				{
					//弹出question
					(new Question()).go();
				}
				//若不存在就提示输入不正确	
				else
				{
					//错误信息提示!
					(new InformFrame(1,"用户名或密码不正确!")).go();
				}
			}
		}

		//对按钮进行监听
		if(e.getSource()==clearBtn)
		{
			userText.setText("");
			pwdText.setText("");
			userText.grabFocus();//获得焦点,让光标返回到用户上
		}

		if(e.getSource()==newBtn)
		{
			selectPanel(2);
			userText1.grabFocus();
		}
	
		if(e.getSource()==exitBtn)
		{
			(new InformFrame(2,"真的要退出吗?")).go();
		}
//__________________________________________________________________________________________________
//__________________________________________________________________________________________________
                
		if(e.getSource()==pwdText1||e.getSource()==pwdText2||e.getSource()==okBtn1)
		{
			//用户名没有检查???????????????????????????
	
			//密码和验证密码不能为空
			if(pwdText1.getText().trim().length()==0||pwdText1.getText().trim().length()==0)
			{
				(new InformFrame(1,"密码名不能为空")).go();
				pwdText1.setText("");
				pwdText1.grabFocus();
			}
			//在都不为空的前提下判断两次输入的秘密是否一致
			else
			{   if(!(pwdText1.getText().trim()).equals(pwdText2.getText().trim()))
			    {
				(new InformFrame(1,"两次密码输入不一致,请重新输入")).go();
				pwdText1.setText("");
				pwdText2.setText("");
				pwdText1.grabFocus();
			    }
			    //若两次输入的一致判断是否注册的新用户为已经存在的用户
			    else
			    {
				//用户名和密码的验证
				String name=userText1.getText().trim();
				String pwd=pwdText1.getText().trim();
				//如果确实为新用户的话就创建一个新用户
				if(!ManageUser.isFind(name,pwd))
				{
					//创建新用户
					newUser.add(name,pwd);
					(new InformFrame(1,"创建成功")).go();
					(new Question()).go();
				}
				//如果新注册的用户已经存在就提示出错并且重新注册	
				else
				{
					//错误信息提示!
					frame.dispose();
					(new InformFrame(1,"用户名或密码已存在!")).go();
					frame.dispose();
					userText1.setText("");
					pwdText1.setText("");
					pwdText2.setText("");
					pwdText1.grabFocus();
				}
			    }
			}
		}
		if(e.getSource()==exitBtn1)
		{
			selectPanel(1);
			userText.grabFocus();
		}
	}
//***********************************************************
	public boolean checkName()
	{
		if(userText.getText().trim().length()==0)//判断用户名是否为空
		{
			(new InformFrame(1,"用户名不能为空")).go();
			userText.setText("");											userText.grabFocus();//若为空,则用户名重新输入
			return false;
		}
		else	
			pwdText.grabFocus();
			return true;
	}
	public void go()
	{
		frame.setVisible(true);
	}
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^上
	//专门用于生产用户提示的各种信息框
	class InformFrame
	{
		private JFrame frame;
		private Container contentPane;
		private JLabel lbl;
		private JButton okBtn,exitBtn;
		private int id;
		private String informText; 
		
		public InformFrame(int id,String informText)
		{
			frame=new JFrame("信息提示!");
			frame.setBounds(200,500,300,170);
			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			//设置大小可不可以改变
			frame.setResizable(false);
			contentPane=frame.getContentPane();
			this.id=id;
			this.informText=informText;
			initGUI();
		}
		public void initGUI()
		{
			contentPane.setLayout(new BorderLayout());
			lbl=new JLabel(informText);
			contentPane.add(lbl,BorderLayout.CENTER);
			contentPane.add(getPanel(id),BorderLayout.SOUTH);
				
		}
		//生产加载到contentPane中SOUTH区域的JPanel
		public JPanel getPanel(int id)
		{
			JPanel p=null;
			switch(id)
			{
				case 1:{
					okBtn=new JButton("确定");
					p=new JPanel(new FlowLayout());
					p.add(okBtn);
					okBtn.addActionListener(new ActionListener()
					{
						public void actionPerformed(ActionEvent e)
						{
						   frame.dispose();
						}
					});
					}break;
				case 2:{
					okBtn=new JButton("是");
					exitBtn=new JButton("否");
					p=new JPanel(new FlowLayout());
					p.add(okBtn);
					p.add(exitBtn);

					okBtn.addActionListener(new ActionListener()
					{
						public void actionPerformed(ActionEvent e)
						{
						   System.exit(0);
						}
					});
					exitBtn.addActionListener(new ActionListener()
					{
						public void actionPerformed(ActionEvent e)
						{
						   frame.dispose();
						}
					});
					}break;
			}
			return p;
		}
		public void go()
		{
			frame.setVisible(true);
		}
	}
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^下
	public static void main(String args[])
	{
		(new Land()).go();
	}
}

⌨️ 快捷键说明

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