考试系统代码(简陋版)

该代码示例展示了使用JavaSwing构建的考试系统登录界面和答题功能。用户输入用户名和密码进行登录,登录信息从MySQL数据库验证。登录成功后,用户可以进行答题,系统有计时功能。答题界面包含单选题和答题操作按钮,如上一题、下一题和提交。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

该项目包括登录功能,答题功能和记时功能,答对题则可以加分.(只发了部分代码)

package E;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.*;
import java.util.Enumeration;

import shixun2.ExamJFrame;

import javax.swing.*;
import javax.swing.plaf.FontUIResource;
public class Login extends Frame implements ActionListener
{
Label lbuser,lbpwd,k;
Label lbinstrution=new Label("抱歉,该用户名不存在!");
Label lbinstrution1=new Label("抱歉,该用户口令不正确!");
TextField tfuser,tfpwd;
Button btnok,btnreg;
String s1,s2;//用来取文本框中的字符串.

public Login()
{
super("考生登录系统界面");
    setBounds(340,190,400,300);
    setLayout(null);
setVisible(true);
setBackground(Color.pink);
k=new Label("考生登陆界面");
k.setFont(new Font("华文行楷",Font.BOLD,20));
lbinstrution.setFont(new Font("华文行楷",Font.BOLD,14));
lbinstrution1.setFont(new Font("华文行楷",Font.BOLD,14));
lbuser=new Label("用户名",Label.CENTER);
lbuser.setFont(new Font("华文行楷",Font.BOLD,12));
lbpwd=new Label("口   令",Label.CENTER);
lbpwd.setFont(new Font("华文行楷",Font.BOLD,12));
tfuser=new TextField(16);
    tfpwd=new TextField(16);
    tfpwd.setEchoChar('*');
    btnok=new Button("登录");
    btnok.setForeground(Color.red);
    btnok.setFont(new Font("华文行楷",Font.BOLD,12));
    btnok.setBackground(new Color(213,219,246));
    btnreg=new Button("注册");
    btnreg.setForeground(Color.red);
    btnreg.setFont(new Font("华文行楷",Font.BOLD,12));
    btnreg.setBackground(new Color(213,219,246));
     addWindowListener(new WindowAdapter(){
   public void windowClosing(WindowEvent e){
    dispose();
    System.exit(0);
   }

    });
add(lbinstrution);add(lbinstrution1);
add( lbuser);add( lbpwd);
add(tfuser); add(tfpwd);//添加组件
add(btnok); add(btnreg);
add(k);
lbinstrution.setBounds(140,180,160,18);
lbinstrution.setForeground(Color.BLACK);
lbinstrution1.setBounds(140,180,160,18);
lbinstrution1.setForeground(Color.BLACK);;
k.setBounds(140, 50, 160, 50);
k.setForeground(Color.GREEN);
lbuser.setBounds(100, 100, 70, 20);
lbpwd.setBounds(100,150,65,17);
tfuser.setBounds(190,100,100,20);
tfpwd.setBounds(190,150,100,20);
btnok.setBounds(100,220,60,20);
btnreg.setBounds(260,220,60,20);
lbinstrution.setVisible(false);
lbinstrution1.setVisible(false);
btnok.addActionListener(this);//添加系列监听器
btnreg.addActionListener(this);//添加系列监听器
}
public static void main(String[] args)
    {
   Login f=new Login();
   }

private static void InitGlobalFont(Font font) {
	FontUIResource fontRes = new FontUIResource(font);
	for (Enumeration<Object> keys = UIManager.getDefaults().keys(); keys.hasMoreElements();) {
		Object key = keys.nextElement();
		Object value = UIManager.get(key);
		if (value instanceof FontUIResource) {
			UIManager.put(key, fontRes);
		}
	}
}
public void actionPerformed(ActionEvent e)
{
   if(e.getSource()==btnok)
   {if(tfuser.getText().equals("")&&tfpwd.getText().equals(""))
    JOptionPane.showMessageDialog(null, "用户名和密码不能为空");
    else{
    s1=tfuser.getText();s2=tfpwd.getText();
    try {
    	 Class.forName("com.mysql.jdbc.Driver");
    	 String url="jdbc:mysql://127.0.0.1:3306/mytest?useUnicode=true&characterEncoding=utf-8";
    		String user="root";
    		String password="123";
     Connection con=DriverManager.getConnection(url,user,password);
     Statement fsg=con.createStatement();
     ResultSet rsuser=fsg.executeQuery("select username,password from admin ");
     while(rsuser.next())
     {
      String rs=rsuser.getString("username");
      String rs2=rsuser.getString("password");
      if(s1.equals(rs)&&(s2.equals(rs2)))//只有用户名与密码都与之匹配才能进入考试
       {new ExamJFrame();
       System.out.println("successs");
       break;}

      if((s1.equals(""))&&(!(s2.equals(rs))))//提示你没有用户名.//取不等的方法直接在前面加"!".
       {lbinstrution.setVisible(true);}

      if((s1.equals(rs))&&(!s2.equals(rs2)))//提示你你的密码错误.
       {lbinstrution1.setVisible(true);
     
     }
     }
     rsuser.close();
     fsg.close();
     con.close();

     } catch (ClassNotFoundException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
     } catch (SQLException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
     }
    }
   }//if的结束标记符.
   if (e.getSource()==btnreg)
    new Register( );
     }
   }








package shixun2;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.plaf.FontUIResource;

/*
 * 主窗体
 */
public class ExamJFrame extends JFrame  implements Runnable {
	JMenuBar jmb;
	JMenu jm,jm2;
	JMenuItem jmi1,jmi2;
	JPanel jp1;
	JLabel jlb,lbtime;//题目
	JRadioButton jr1,jr2,jr3;//四个选项的单选按钮
	JButton jb1,jb2,jb3;//三个按钮,上一题、下一题、提交
	JPanel jp2;//用来放三个按钮
	ButtonGroup bg;//按钮组,实现单选按钮的排斥
	JTextArea jta;
  ArrayList<zsx> list;
  zsx c;
shixun4 v;
int index;
int totaltime=5400;
Thread f;
	public ExamJFrame(){
		setTitle("考试系统");
		setSize(700, 500);
		f=new Thread(this);
		setLocationRelativeTo(null);
		InitGlobalFont(new Font("黑体", Font.PLAIN, 20));
		init();
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setVisible(true);
		jmi1.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
                 try {
                	if(!f.isAlive())
                	{ f.start();}
                	
                	init2();
					list=v.find();
					index = 0;
	                 printjm(index);
	                 jb1.setEnabled(false);
				} catch (ClassNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}

			}
		});




		jmi2.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					init3();
				} catch (ClassNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}

			}
		});





	}
	//定义查询记录的界面
	public void init3() throws ClassNotFoundException, SQLException {
		jp1.removeAll();
		jp1.setLayout(new BorderLayout());
		jta=new JTextArea();
		JScrollPane jsp=new JScrollPane(jta);
		jp1.add(jta);
   ArrayList <recod> lis=v.findAll();
		for(int i=0;i<lis.size();i++){
		recod zx=lis.get(i);
		String str="姓名:"+zx.getname()+"   "+"成绩:"+zx.getscore()+"   ";
		jta.append(str);

		}
	}
	//设置初始界面
	public void init() {
		jmb=new JMenuBar();
		jm=new JMenu("考试");
		jm2=new JMenu("考试记录");
		jmi1=new JMenuItem("开始考试");
		jmi2=new JMenuItem("查询所有考试记录");
		lbtime=new JLabel("计时:");
		jm.add(jmi1);
		jm2.add(jmi2);
		jmb.add(jm);
		jmb.add(jm2);
		jmb.add(new JLabel("    "));
		jmb.add(lbtime);
		setJMenuBar(jmb);
		jp1=new JPanel();
		add(jp1);
		
		jmb.add(lbtime);
		lbtime.setForeground(Color.red);
	}

	//考试时候的组件布局
	public void init2(){
		jp1.removeAll();
		//设置面板的布局为网格布局,只有一列
		jp1.setLayout(new GridLayout(0,1));
		jlb=new JLabel("题目");
		jr1=new JRadioButton("1");
		jr2=new JRadioButton("2");
		jr3=new JRadioButton("3");


		//单选按钮要实现互相排斥,多选一,ButtonGroup按钮组
		bg=new ButtonGroup();
		bg.add(jr1);
		bg.add(jr2);
		bg.add(jr3);
		jp2=new JPanel();
		jb1=new JButton("上一题");
		jb2=new JButton("下一题");
		jb3=new JButton("提交");
		jp2.add(jb1);
		jp2.add(jb2);
		jp2.add(jb3);
		//中间的大面板jp1添加组件
		//滚动条面板添加jlb
		jp1.add(new JScrollPane(jlb));
		jp1.add(jr1);
		jp1.add(jr2);
		jp1.add(jr3);

		jp1.add(jp2);
		jb2.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
			
			   save(index);
			   index++;
                 printjm(index);
                 jb1.setEnabled(true);
                 if(index>=list.size()-1){
                	 jb2.setEnabled(false);
                 }
       

			}

		});

		jb1.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
		
				 save(index);
				   index--;
	                 printjm(index);
	                 jb2.setEnabled(true);
	                 if(index<1){
	                	 jb1.setEnabled(false);
	                 }

	  
				}
		});

		jb3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			save(index);
			int sum=0,u=0;
			String se = " ";
			for(int i=0;i<list.size();i++){
				if(list.get(i).getuseran()!=null)
				{
					u++;
				}
				else{
				se+=(i+1)+" ";
				}
			}
			if(u!=0){
				
			JOptionPane.showMessageDialog(null, "你的第"+se+"道题没有作答,请前去看看");
			}
			if(u==0){
			for(int i=0;i<list.size();i++){
				if(list.get(i).getuseran().equals(list.get(i).getanswer())){sum+=10;
				}
			}
			String name=JOptionPane.showInputDialog("请输入你的名字");
		JOptionPane.showMessageDialog(null, name+"你的成绩是"+sum+"分");
		recod re=new recod();
		re.setname(name);
		re.setscore(sum);
       v.addrecord(re);
			}
			}
		});
		this.validate();


	}




	/**
	 * 统一设置字体,父界面设置之后,所有由父界面进入的子界面都不需要再次设置字体
	 */
	private static void InitGlobalFont(Font font) {
		FontUIResource fontRes = new FontUIResource(font);
		for (Enumeration<Object> keys = UIManager.getDefaults().keys(); keys.hasMoreElements();) {
			Object key = keys.nextElement();
			Object value = UIManager.get(key);
			if (value instanceof FontUIResource) {
				UIManager.put(key, fontRes);
			}
		}
	}
	public void printjm (int index){
		c=list.get(index);
		jlb.setText(index+1+"."+c.gettitle());
		jr1.setText(c.getsd());
		jr2.setText(c.getsc());
		jr3.setText(c.getsa());
		String useran=c.getuseran();
		if(useran!=null){
			switch(useran){
			case "A":jr1.setSelected(true);break;
			case "B":jr2.setSelected(true);break;
			case "C":jr3.setSelected(true);break;
			}
		}

		validate();
	}
	public void save(int index) {
String useran=null;
if(jr1.isSelected()){useran="A";}
if(jr2.isSelected()){useran="B";}
if(jr3.isSelected()){useran="C";}
c.setuseran(useran);
list.set(index,c);
bg.clearSelection();
	}
   public static void main(String[] args) {
	   new ExamJFrame();
}
@Override
public void run() {
	  while(true)
	     {
	      totaltime--;
	     long hour=totaltime/3600%60;
	    long  minute=totaltime/60%60;
	  long   second=totaltime%60;
	       lbtime.setText("计时:"+hour+":"+minute+":"+second+"          "+"少年加油吧!");
	      try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	     }

}
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值