大二java课程设计举例

  • 时间:
  • 浏览:9
  • 来源:跟我学网络
/*-------Vecrates---------*/
import java.awt.BorderLayout;

public class ExamWindow extends JFrame {
	
	private JPanel contentPane;
	final JPanel panel = new JPanel();
	QuestionWindow qw;
	
	private final Action StartExam = new SwingAction();//开始考试按钮事件
	private final Action CloseFrame = new SwingAction_1();//退出考试事件

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ExamWindow frame = new ExamWindow();

					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ExamWindow() {
		qw = new QuestionWindow();
		setTitle("标准化考试系统1.0");
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		setBounds(100, 100, 500, 450);
		contentPane = new JPanel();
		contentPane.setBackground(Color.CYAN);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		
		panel.setBackground(SystemColor.activeCaption);
		panel.setBounds(0, 0, 484, 411);
		contentPane.add(panel);
		panel.setLayout(null);
		
		JButton btnNewButton_1 = new JButton("\u9000\u51FA\u7CFB\u7EDF");
		btnNewButton_1.setAction(CloseFrame);
		btnNewButton_1.setBounds(188, 221, 100, 30);
		panel.add(btnNewButton_1);
		
		JLabel lblNewLabel = new JLabel("\u8003\u8BD5\u7CFB\u7EDF");
		lblNewLabel.setBounds(168, 36, 120, 35);
		panel.add(lblNewLabel);
		lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 30));
		lblNewLabel.setForeground(Color.BLUE);
		
		JButton btnNewButton = new JButton("\u5F00\u59CB\u8003\u8BD5");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			}
		});
		btnNewButton.setBounds(188, 157, 100, 30);
		panel.add(btnNewButton);
		btnNewButton.setAction(StartExam);
	}
	
	//“开始考试”按钮的监听事件
	private class SwingAction extends AbstractAction {
		public SwingAction() {
			putValue(ACTION_COMMAND_KEY, "");
			putValue(NAME, "开始考试");
			putValue(SHORT_DESCRIPTION, "Some short description");
		}
		public void actionPerformed(ActionEvent e) {
			qw = new QuestionWindow();
			setVisible(false); //使ExamWindow不可见
			qw.setVisible(true); //使QuestionWindow可见
			new Thread(qw.new RemainTime()).start();//点击开始考试时同时计算剩余时间
			//读出第一个题目
			 try {
				qw.qArea.setText(new ReadQuestion(0).getStr());
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
	}
	
	//“退出考试”按钮的监听事件
	private class SwingAction_1 extends AbstractAction {
		public SwingAction_1() {
			putValue(NAME, "退出系统");
			putValue(SHORT_DESCRIPTION, "Some short description");
		}
		public void actionPerformed(ActionEvent e) {
			System.exit(0);
		}
	}
}
//ExamWindow
public class QuestionWindow extends JFrame {
	
	JLabel remain_time = new JLabel(); //显示剩余时间的标签
	private JPanel contentPane; 
	public JTextArea qArea; //显示问题的文本域
	//用于保存答案的数组
	String[] answer = new String[]{"A","B","C","D","A","B","C","D","A","B"}; 
	String tempAS;//保存单选框中选择的答案
	int i; //用于计分
	int  n = 1; //用于定位(读txt)文件中的第几行
	int socre = 0; //得分
	
	JLabel jl; //用于提示考试结束和给出分数的标签
	
	/**
	 * Create the frame.
	 */
	public QuestionWindow() {
		qArea = new JTextArea();
		setTitle("标准考试系统");
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		setBounds(100, 100, 500, 450);
		setVisible(false);//使其不可见
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBackground(SystemColor.activeCaption);
		panel.setBounds(0, 0, 484, 411);
		contentPane.add(panel);
		panel.setLayout(null);
		
		JButton nextQ = new JButton("\u4E0B\u4E00\u9898");
		nextQ.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});
		nextQ.setAction(nextq);
		nextQ.setFont(new Font("宋体", Font.BOLD, 12));
		nextQ.setBounds(397, 378, 77, 23);
		panel.add(nextQ);
		nextQ.setForeground(Color.GREEN);
		
		JLabel lblNewLabel = new JLabel("\u8003\u8BD5\u5269\u4F59\u65F6\u95F4\uFF1A");
		lblNewLabel.setBounds(0, 0, 114, 23);
		panel.add(lblNewLabel);
		lblNewLabel.setForeground(Color.RED);
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		
		JButton submitAS = new JButton("\u63D0\u4EA4\u6B64\u9898");
		submitAS.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});
		submitAS.setAction(submitItem);
		submitAS.setFont(new Font("宋体", Font.BOLD, 12));
		submitAS.setBounds(296, 378, 91, 23);
		panel.add(submitAS);
		submitAS.setForeground(Color.GREEN);
		
		//显示倒计时的标签
		remain_time.setBounds(99, 0, 106, 23);
		remain_time.setVisible(true);
		panel.add(remain_time);
		
		//ABCD单选按钮组件
		ButtonGroup group = new ButtonGroup();
		JPanel panel_1 = new JPanel();
		panel_1.setBackground(SystemColor.activeCaption);
		panel_1.setBounds(26, 370, 192, 31);
		panel.add(panel_1);
		panel_1.setLayout(null);
		JRadioButton a = new JRadioButton();
		a.setAction(ASA);
		a.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		a.setBounds(6, 7, 43, 21);
		a.setText("A");
		a.setForeground(Color.BLACK);
		panel_1.add(a);
		a.setBackground(SystemColor.activeCaption);
		a.setVerticalAlignment(SwingConstants.BOTTOM);
		group.add(a);
		JRadioButton b = new JRadioButton();
		b.setAction(ASB);
		b.setForeground(Color.BLACK);
		b.setBackground(SystemColor.activeCaption);
		b.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		b.setText("B");
		b.setBounds(51, 7, 50, 21);
		panel_1.add(b);
		group.add(b);
		JRadioButton c = new JRadioButton();
		c.setForeground(Color.BLACK);
		c.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		c.setBackground(SystemColor.activeCaption);
		c.setText("C");
		c.setBounds(103, 7, 44, 21);
		panel_1.add(c);
		group.add(c);
		JRadioButton d = new JRadioButton();
		d.setAction(ASD);
		d.setForeground(Color.BLACK);
		d.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		d.setBackground(SystemColor.activeCaption);
		d.setBounds(149, 7, 43, 21);
		panel_1.add(d);
		d.setText("D");
		group.add(d);
		qArea.setLineWrap(true);
		qArea.setEnabled(false);
		qArea.setEditable(false);
		qArea.setForeground(Color.GREEN);
		qArea.setFont(new Font("微软雅黑 Light", Font.BOLD, 20));
		
		
		
		//显示题目的文本域
		qArea.setBounds(10, 22, 464, 299);
		panel.add(qArea);
		
		JButton submitTest = new JButton("\u63D0\u4EA4\u8BD5\u5377");
		submitTest.setAction(EndTest);
		submitTest.setFont(new Font("宋体", Font.BOLD, 14));
		submitTest.setForeground(Color.GREEN);
		submitTest.setBounds(360, 323, 114, 31);
		panel.add(submitTest);
	}
	
	//选择A时
	private final Action ASA = new SwingAction();
	private class SwingAction extends AbstractAction {
		public SwingAction() {
			putValue(NAME, "SwingAction");
			putValue(SHORT_DESCRIPTION, "Some short description");
		}
		public void actionPerformed(ActionEvent e) {
			tempAS = "A";
		}
	}
	//选择B时
	private final Action ASB = new SwingAction_1();
	private class SwingAction_1 extends AbstractAction {
		public SwingAction_1() {
			putValue(NAME, "SwingAction_1");
			putValue(SHORT_DESCRIPTION, "Some short description");
		}
		public void actionPerformed(ActionEvent e) {
			tempAS = "B";
		}
	}
	//选择C时
	private final Action ASC = new SwingAction_2();
	private class SwingAction_2 extends AbstractAction {
		public SwingAction_2() {
			putValue(NAME, "SwingAction_2");
			putValue(SHORT_DESCRIPTION, "Some short description");
		}
		public void actionPerformed(ActionEvent e) {
			tempAS = "C";
		}
	}
	//选择D时
	private final Action ASD = new SwingAction_3();
	private class SwingAction_3 extends AbstractAction {
		public SwingAction_3() {
			putValue(NAME, "SwingAction_3");
			putValue(SHORT_DESCRIPTION, "Some short description");
		}
		public void actionPerformed(ActionEvent e) {
			tempAS = "D";
		}
	}
	
	//"提交此题"按钮监听事件
	private final Action submitItem = new SwingAction_4();
	private class SwingAction_4 extends AbstractAction {
		public SwingAction_4() {
			putValue(NAME, "提交此题");
			putValue(SHORT_DESCRIPTION, "Some short description");
		}
		public void actionPerformed(ActionEvent e) {
			if(answer[i++] == tempAS)
				socre += 10;
		}
	}
	
	//结束考试按钮的监听事件
	private final Action EndTest = new SwingAction_5();
	private class SwingAction_5 extends AbstractAction {
		public SwingAction_5() {
			putValue(NAME, "结束考试");
			putValue(SHORT_DESCRIPTION, "Some short description");
		}
		public void actionPerformed(ActionEvent e) {
			jl = new JLabel("考试结束,你的得分是:" + socre+"分");
			new Socre(jl).setVisible(true);
		}
	}
	
	//"下一题"按钮监听事件
	private final Action nextq = new SwingAction_6();
	private class SwingAction_6 extends AbstractAction {
		public SwingAction_6() {	
			putValue(NAME, "下一题");
			putValue(SHORT_DESCRIPTION, "Some short description");
		}
		public void actionPerformed(ActionEvent e) { //next question
			try {
				//题库中有10个题目
				if(n < 10){ 
				    qArea.setText(new ReadQuestion(n++).getStr());//读题目并显示到面板上
				    setVisible(true);
				}
				else{
					jl = new JLabel("你已完成答题,得分是:" + socre+"分");
					new Socre(jl).setVisible(true);
				}
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
	}
	
	
	
	//实现倒计时功能的子类
    private int time = 300;
	class RemainTime implements Runnable {
	
		public void run() {
			while(time >= 0){
				//当时间为0时结束考试
				if(time == 0){
					jl = new JLabel("考试时间到,你的得分是:" + socre+"分");
				    new Socre(jl).setVisible(true);
				    try {
						Thread.sleep(10000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				    System.exit(0);
				}
				else{
					time--;
					long minu = time / 60;
					long sec = time % 60;
				    remain_time.setText(minu + "分" + sec + "秒");
					try{
					    Thread.sleep(1000); 
					}catch (Exception e){
					    e.printStackTrace();
					}
				}
			 }
		 }
	}

}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;

public class ReadQuestion {
	private String str;
	
	public String getStr() {
		return str;
	}
	
	public void setStr(String str) {
		this.str = str;
	}
	
	 public ReadQuestion(int n) throws IOException{
		File txt = new File("D:/代码/java/java课程设计/src/考试系统题库.txt");
		FileReader fr = new FileReader(txt);
		BufferedReader inBuf = new BufferedReader(fr);
		String strline = null;
		for(int i = 0;i <= n; i ++)
		    strline = new String(inBuf.readLine()); //读取一行字符
		String[] newstr = strline.split(",");
		this.setStr(newstr[0]+"\n"+newstr[1]+"\n"+newstr[2]+"\n"+newstr[3]+"\n"+newstr[4]);
		inBuf.close();
	 }
}
import java.awt.BorderLayout;

public class Socre extends JDialog {
	
	private final Action ensure_action = new SwingAction();
	
	public Socre(JLabel JL) {
		this.setModal(true); //强制性窗口
		getContentPane().setLayout(null);
		setBounds(150,150, 384, 300);
		JL.setFont(new Font("宋体", Font.BOLD, 14));
		JL.setForeground(Color.RED);
		JL.setHorizontalAlignment(SwingConstants.CENTER);
		JL.setBounds(38, 27, 278, 84);
		getContentPane().add(JL);
		setUndecorated(true);
		
		//确定按钮
		JButton ensure = new JButton("\u786E\u5B9A");
		ensure.setAction(ensure_action);
		ensure.setBackground(Color.RED);
		ensure.setBounds(126, 213, 99, 38);
		getContentPane().add(ensure);
	}
	
	//"确定按钮"监听
	private class SwingAction extends AbstractAction {
		public SwingAction() {
			putValue(NAME, "确定");
			putValue(SHORT_DESCRIPTION, "Some short description");
		}
		public void actionPerformed(ActionEvent e) {
			System.exit(0);
		}
	}
}

考试系统题库形式为:

1、工笔是哪种绘画形式的技法,A.水彩画,B.油画,C.水粉画,D.国画,

2、“冰激凌”是从哪国传进的外来语,A.英国,B.法国,C.美国,D.俄国,

3、“席梦思”三个字源于什么,A.人名, B.地名,C.00  ,D.00, 

4、“八仙过海”中的八仙除铁拐李、张果老、吕洞宾、曹国舅外还有,A.韩非子、蓝采和、何翠姑、汉钟离,B.蓝采和、何仙姑、韩非子、汉钟离 ,C.韩湘子、蓝采和、何仙姑、红孩儿 ,D.韩湘子、蓝采和、何仙姑、汉钟离,

5、“无冕之王”的由来与下列哪一个有关,A.英国《泰晤士报》 ,B.法国《费加罗报》 ,C.美国《太阳报》 ,D.德国《明镜》周刊,

6、博士作为官名最早出现在:,A.秦 ,B.汉 ,C.唐,D.00,

7、“谬种流传”最早是宋朝人批评当时的,A.官场黑暗, B.科举制度 ,C.社会风气 ,D.诗词风格,

8、《在那遥远的地方》是哪里的民歌,A.四川民歌 ,B.江苏民歌 ,C.蒙古民歌 ,D.云南民歌, 

9、人体含水量百分比最高的器官是,A.肝 ,B.肾 ,C.眼球,D.0111,

10、人体最大的解毒器官是,A.胃 ,B.肾脏 ,C.肝脏 ,D.脾,