在线考试系统
2011-08-07 16:02 shiyibin 阅读(908) 评论(0) 编辑 收藏 举报package com.tarena.exam.client.gui;
import javax.swing.JFrame;
import com.tarena.util.GuiUtil;
public class BaseFrame
extends JFrame {
@Override
public void setVisible(boolean b) {
GuiUtil.centerWindow(this);
super.setVisible(b);
}
}
package com.tarena.exam.client.gui;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import com.sun.java.swing.plaf.windows.resources.windows;
import com.tarena.exam.entity.ExamInfo;
import com.tarena.exam.entity.QuestionInfo;
import com.tarena.exam.entity.User;
import com.tarena.exam.service.ExamService;
import com.tarena.exam.service.NameOrPasswordException;
/**
* Context 上下文. 是当前环境, 是当前环境的控制器
* @author teacher
*/
//TODO Day02 添加类
public class ClientContext {
/**
* 控制器依赖于ExamService提供的服务
*/
private ExamService service;
private LoginFrame loginFrame;
//TODO Day03 1.a
private MenuFrame menuFrame;
//TODO Day04 3.b
private ExamFrame examFrame;
//TODO Day04 4.a
private ExamInfo examInfo ;
private QuestionInfo questionInfo ;
//TODO Day05 1.a
private WelcomeWindow welcome;
//TODO Day05 3.b
private User user;
/**
* 构造器注入
*/
public ClientContext(ExamService service) {
this.service = service;
}
/**
* 登录方法
* @param source 发出登录请求的界面
* @param userId
* @param passwd
*/
public void login(JFrame source,
String userId, String passwd) {
try{
int id = Integer.parseInt(userId);
User u = service.login(id, passwd);
//TODO Day03 1.b
//JOptionPane.showMessageDialog(
// source, "您好:"+ u.getName());
source.setVisible(false);
menuFrame.setVisible(true);
//TODO Day03 1.c
//centerWindow(menuFrame);
//TODO Day05 3.b
this.user = u;
//TODO Day05 3.a
menuFrame.updateView();
//TODO Day02 PM 加入数字ID检查
}catch(NumberFormatException e){
e.printStackTrace();
JOptionPane.showMessageDialog(
source, "ID必须输入数字!");
}catch(NameOrPasswordException e){
e.printStackTrace();
JOptionPane.showMessageDialog(
source, e.getMessage());
}
}
// //TODO Day03 1.c
// private void centerWindow(JFrame win){
// Toolkit toolkit =
// Toolkit.getDefaultToolkit();
// Dimension scr = toolkit.getScreenSize();
// int x = (scr.width-win.getWidth())/2;
// int y = (scr.height-win.getHeight())/2;
// win.setLocation(x,y);
// }
//TODO Day02 PM
public void exit(JFrame source){
int val = JOptionPane.showConfirmDialog(
source, "真的离开考试系统吗?");
if(val==JOptionPane.YES_OPTION){
System.exit(0);
}
}
public void show(){
//TODO Day03 1.c
loginFrame.setVisible(true);
//centerWindow(loginFrame);
//TODO Day05 1.b
welcome.setVisible(true);
new Thread(){
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
welcome.setVisible(false);
}
}.start();
}
public LoginFrame getLoginFrame() {
return loginFrame;
}
public void setLoginFrame(LoginFrame loginFrame) {
this.loginFrame = loginFrame;
}
public ExamService getService() {
return service;
}
public void setService(ExamService service) {
this.service = service;
}
//TODO Day03 1.a
public void setMenuFrame(MenuFrame menuFrame) {
this.menuFrame = menuFrame;
}
//TODO Day04 3.b
public void setExamFrame(ExamFrame examFrame) {
this.examFrame = examFrame;
}
//TODO Day04 3.b
public void start(MenuFrame source) {
try{
//开始考试, 获得详细的考试信息, 包括
//考生, 考试时间, 考试名称等
examInfo= service.start();//包含异常,考试已经结束了
questionInfo= service.getQuestion(0);
//切换界面, 到考试界面
examFrame.setVisible(true);
//更新考试界面. 显示考试信息, 和第一道题
examFrame.updateView();
//TODO Day05 2
examFrame.setAlwaysOnTop(true);
menuFrame.setVisible(false);
}catch(Exception e){
e.printStackTrace();
JOptionPane.showMessageDialog(
source, e.getMessage());
}
}
//TODO Day04 4.a
public ExamInfo getExamInfo() {
return examInfo;
}
//TODO Day04 4.a
public QuestionInfo getQuestionInfo() {
return questionInfo;
}
//TODO Day04 4.b.3
//界面答案更新了
public void answerChanged(List<Integer> ans) {
getQuestionInfo().getUserAnswers().clear();
getQuestionInfo().getUserAnswers().addAll(ans);
}
//TODO Day04 7.b
public void next(JFrame source){
try {
int idx =
questionInfo.getQuestionIndex();
//发送当前题目的答案到服务端
service.sendUserAnswers(idx,
questionInfo.getUserAnswers());
//获取下一个题目
idx++;
questionInfo =
service.getQuestion(idx);
//更新examFrame界面
examFrame.updateView();
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(source, e.getMessage());
}
}
//TODO Day04 7.b
public void prev(JFrame source){
try {
int idx =
questionInfo.getQuestionIndex();
//发送当前题目的答案到服务端
service.sendUserAnswers(idx,
questionInfo.getUserAnswers());
//获取下一个题目
idx--;
questionInfo =
service.getQuestion(idx);
//更新examFrame界面
examFrame.updateView();
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(source, e.getMessage());
}
}
//TODO Day04 8.b
public void commit(JFrame source) {
int val = JOptionPane.showConfirmDialog(
source, "你完了吗!");
if(val!=JOptionPane.YES_OPTION){
return ;
}
try {
//发送当前题的答案
int idx = questionInfo.getQuestionIndex();
service.sendUserAnswers(
idx, questionInfo.getUserAnswers());
int r = service.commit();
JOptionPane.showMessageDialog(
source, "你的分数:"+ r);
examFrame.setVisible(false);
menuFrame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(source, e.getMessage());
}
}
//TODO Day05 1.a
public void setWelcomeWindow(
WelcomeWindow welcome) {
this.welcome = welcome;
}
//Day05 3.c
public User getUser() {
return this.user;
}
//Day05 4.b
public void result(JFrame source) {
try {
int result = service.result();
JOptionPane.showMessageDialog(
source, "您的考试成绩:" + result);
exit(source);
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(source, e.getMessage());
}
}
}
package com.tarena.exam.client.gui;
import java.awt.BorderLayout;
import java.awt.Checkbox;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.border.EmptyBorder;
import com.tarena.exam.entity.ExamInfo;
import com.tarena.exam.entity.Question;
public class ExamFrame
extends BaseFrame{
//TODO Day04 4.b.1
private ClientContext context;
//TODO Day04 4.b.2
private JLabel infolabel;
//TODO Day04 4.b.3
private JLabel numberlabel;
//TODO Day04 4.b.3
private JTextPane questionTextPane;
//TODO Day04 4.b.3
private JButton prevButton;
private JButton nextButton;
private JPanel optionPane;
private List<OptionCheckBox> optionList=
new ArrayList<OptionCheckBox>();
public ExamFrame() {
init();
}
//TODO Day04 4.b.1
public ExamFrame(ClientContext context) {
init();
this.context = context;
}
private void init(){
setSize(600, 400);
setTitle("达内在线测评");
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
//TODO Day04 8.a
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
context.commit(ExamFrame.this);
}
});
setContentPane(createContentPane());
}
private JPanel createContentPane(){
JPanel p =
new JPanel(new BorderLayout());
p.add(BorderLayout.NORTH,
createTitle());
p.add(BorderLayout.CENTER,
createQuestionPane());
p.add(BorderLayout.SOUTH,
createToolsPane());
return p;
}
private JPanel createToolsPane() {
JPanel p = new JPanel(new BorderLayout());
//TODO Day04 4.b.3
numberlabel = new JLabel("题号:");
p.add(BorderLayout.WEST, numberlabel);
p.add(BorderLayout.CENTER, createBtnPane());
p.add(BorderLayout.EAST, new JLabel("剩余时间:"));
p.setBorder(new EmptyBorder(10,10,10,10));
return p;
}
private JPanel createBtnPane() {
JPanel p = new JPanel(new FlowLayout());
JButton prev = new JButton("<<前一个");
prevButton = prev;
//TODO Day04 7.a
prev.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
context.prev(ExamFrame.this);
}
});
p.add(prev);
JButton next = new JButton("后一个>>");
nextButton = next;
// TODO Day04 7.a
next.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
context.next(ExamFrame.this);
}
});
p.add(next);
JButton commit = new JButton("交卷");
p.add(commit);
// TODO Day04 8.a
commit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
context.commit(ExamFrame.this);
}
});
return p;
}
private JPanel createQuestionPane() {
JPanel p = new JPanel(new BorderLayout());
p.add(BorderLayout.NORTH,
createUserInfo());
p.add(BorderLayout.CENTER,
createQuestionText());
p.add(BorderLayout.SOUTH,
createOptionsPane());
return p;
}
private JPanel createOptionsPane() {
JPanel p = new JPanel(
new FlowLayout(FlowLayout.CENTER,20,0));
p.add(new Checkbox("A"));
p.add(new Checkbox("B"));
p.add(new Checkbox("C"));
p.add(new Checkbox("D"));
optionPane = p;
return p;
}
private JScrollPane createQuestionText() {
JScrollPane p = new JScrollPane();
JTextPane txt = new JTextPane();
txt.setText("进家门的第一件事情?\n"+
"A.上厕所.\n" +
"B.睡觉\n");
p.getViewport().add(txt);
txt.setEditable(false);
this.questionTextPane = txt;
return p;
}
private JLabel createUserInfo(){
JLabel l = new JLabel();
l.setText("姓名: 刘苍松 编号: 110 科目: 武林常识");
l.setHorizontalAlignment(JLabel.CENTER);
infolabel = l;
return l;
}
private JLabel createTitle(){
JLabel l = new JLabel();
ImageIcon ico = new ImageIcon(
getClass().getResource("exam_title.png"));
l.setIcon(ico);
l.setHorizontalAlignment(JLabel.CENTER);
return l;
}
//TODO Day04 4.b.3
//更新试题显示
public void updateView(){
//获得数据
ExamInfo info = context.getExamInfo();
infolabel.setText(info.toString());
//TODO 更新界面还没有全部完成...
updateQuestion();
updateButtons();
updateOptions();
updateQuestionNumber();
}
//TODO Day04 4.b.3
private void updateQuestionNumber() {
String txt = "题号:" +
context.getExamInfo().getQuestionCount()+
" 的 第"+(context.getQuestionInfo().getQuestionIndex()+1)
+ "题";
this.numberlabel.setText(txt);
}
//TODO Day04 4.b.3
private void updateOptions() {
List<Integer> userAanswer =
context.getQuestionInfo().getUserAnswers();
int size = context.getQuestionInfo()
.getQuestion().getOptionNums();
//清除所有选项
optionPane.removeAll();
//然后在重新添加新的
for(int i=0; i<size; i++){
//创建新选项
OptionCheckBox opt = createOptionBox(i);
optionPane.add(opt);
optionList.add(opt);
}
}
//TODO Day04 4.b.3
//创建一个选项
public OptionCheckBox createOptionBox(
int value){
String[] str = "A,B,C,D,E,F,G,H,I".split(",");
OptionCheckBox opt =
new OptionCheckBox(value, str[value]);
if(context.getQuestionInfo().getUserAnswers().contains(value)){
opt.setSelected(true);
}
//处理选项单击事件, 将选项的结果更新到
//ClientContext中
opt.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent e) {
//读取全部的选择结果
List<Integer> ans = new ArrayList<Integer>();
for(OptionCheckBox o:optionList){
if(o.isSelected()){
ans.add(o.value);
}
}
//保存到ClientContext中
context.answerChanged(ans);
}
});
return opt;
}
//TODO Day04 4.b.3
private void updateButtons() {
prevButton.setEnabled(true);
nextButton.setEnabled(true);
int idx = context.getQuestionInfo().getQuestionIndex();
if(idx==0){
prevButton.setEnabled(false);
}
if(idx == context.getExamInfo().getQuestionCount()-1){
nextButton.setEnabled(false);
}
}
//TODO Day04 4.b.3
private void updateQuestion() {
Question q =
context.getQuestionInfo().getQuestion();
questionTextPane.setText(q.toString());
}
//TODO Day04 4.b.3
public class OptionCheckBox
extends JCheckBox {
int value;
public OptionCheckBox(int value, String txt) {
this.value = value;
this.setText(txt);
}
}
}
package com.tarena.exam.client.gui;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
public class LoginFrame extends BaseFrame {
//TODO Day02 增加如下代码
private ClientContext context;
private JTextField userIdField;
private JPasswordField userPwd;
public LoginFrame() {
init();//初始化界面
}
//TODO Day02 增加构造器
/** 界面依赖于控制器提供的服务 */
public LoginFrame(ClientContext context) {
this();
this.context = context;
}
/** 初始化界面 */
private void init() {
this.setSize(300, 200);
setDefaultCloseOperation(
DO_NOTHING_ON_CLOSE);
setTitle("达内在线测评--登录");
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
//TODO Day02 PM
// int val =
// JOptionPane.showConfirmDialog(
// LoginFrame.this, "走呀!");
// if(val==JOptionPane.YES_OPTION){
// System.exit(0);//结束JVM
// }
context.exit(LoginFrame.this);
}
});
setContentPane(createContentPane());
}
/** 创建内容面板 */
private JPanel createContentPane(){
JPanel p = new JPanel();
p.setLayout(new BorderLayout());
p.add(BorderLayout.SOUTH,
createButtonPane());
p.add(BorderLayout.NORTH,
createUserPwdPane());
return p;
}
/** 创建按钮面板 */
private JPanel createButtonPane(){
JPanel p = new JPanel();
p.setLayout(new FlowLayout());
JButton login = new JButton("登录");
p.add(login);
login.addActionListener(
new ActionListener(){
public void actionPerformed(
ActionEvent e) {
//在内部类中引用外部实例:LoginFrame.this
// TODO Day02 增加如下代码
String userId = userIdField.getText();//?
String passwd = new String(userPwd.getPassword());//?
context.login(
LoginFrame.this,
userId, passwd);
}
});
//TODO Day02 PM
//p.add(new JButton("取消"));
JButton cancel = new JButton("取消");
cancel.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
context.exit(LoginFrame.this);
}
});
p.add(cancel);
return p;
}
private JPanel createUserPwdPane(){
JPanel p = new JPanel();
p.setLayout(new GridLayout(3, 1, 0, 8));
p.add(new JLabel("登录系统", JLabel.CENTER));
p.add(createUserIdPane());
p.add(createPwdPane());
p.setBorder(new EmptyBorder(10,10,10,10));
return p;
}
private JPanel createUserIdPane() {
JPanel p = new JPanel();
p.setLayout(new BorderLayout());
p.add(BorderLayout.WEST,
new JLabel("编号:"));
//TODO Day02 修改了以下代码
// p.add(BorderLayout.CENTER,
// new JTextField());
userIdField = new JTextField();
p.add(BorderLayout.CENTER,
userIdField);
return p;
}
private JPanel createPwdPane() {
JPanel p = new JPanel();
p.setLayout(new BorderLayout());
p.add(BorderLayout.WEST,
new JLabel("密码:"));
//TODO Day02 修改
// p.add(BorderLayout.CENTER,
// new JPasswordField());
this.userPwd = new JPasswordField();
p.add(BorderLayout.CENTER,
userPwd);
return p;
}
}
package com.tarena.exam.client.gui;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Menu;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.URL;
import javax.swing.AbstractButton;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import com.tarena.exam.entity.User;
public class MenuFrame extends BaseFrame {
//TODO Day03 2.a
private ClientContext context;
//TODO Day05 3.c
private JLabel userInfoLabel;
public MenuFrame() {
init();
}
//TODO Day03 2.a
public MenuFrame(ClientContext context) {
this();
this.context = context;
}
private void init(){
setSize(600, 400);
setTitle("达内在线测评");
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
context.exit(MenuFrame.this);
}
});
setContentPane(createContentPane());
}
private JPanel createContentPane(){
JPanel p = new JPanel(new BorderLayout());
URL url = getClass().getResource(
"title.png");
ImageIcon ico = new ImageIcon(url);
p.add(BorderLayout.NORTH,
new JLabel(ico, JLabel.CENTER));
p.add(BorderLayout.CENTER, createMenuPane());
p.add(BorderLayout.SOUTH,
new JLabel("版权所有, 盗版必杀! " +
"达内科技 2010", JLabel.RIGHT));
return p;
}
private JPanel createMenuPane(){
JPanel p = new JPanel(new BorderLayout());
userInfoLabel =
new JLabel("欢迎: XXXX 同学", JLabel.CENTER);
p.add(BorderLayout.NORTH,
userInfoLabel);
p.add(BorderLayout.CENTER,
createBtnPane());
return p;
}
private JPanel createBtnPane(){
JPanel p =
new JPanel(new FlowLayout());
JButton start =
createImgBtn("exam.png", "开始");
//TODO Day04 3.a
start.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
context.start(MenuFrame.this);
}
});
p.add(start);
JButton result =
createImgBtn("result.png", "考试成绩");
p.add(result);
//Day05 4.a
result.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
context.result(MenuFrame.this);
}
});
JButton msg =
createImgBtn("message.png", "考试规则");
p.add(msg);
JButton exit =
createImgBtn("exit.png", "离开");
exit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
context.exit(MenuFrame.this);
}
});
p.add(exit);
return p;
}
private JButton createImgBtn(
String img, String txt) {
ImageIcon ico = new ImageIcon(
getClass().getResource(img));
JButton button = new JButton(txt, ico);
button.setVerticalTextPosition(
JButton.BOTTOM);
button.setHorizontalTextPosition(
JButton.CENTER);
return button;
}
//TODO Day05 3.c
public void updateView() {
User u = context.getUser();
userInfoLabel.setText(
"欢迎:" + u.getName() + " 同学!");
}
}
package com.tarena.exam.client.gui;
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JWindow;
import javax.swing.border.LineBorder;
import com.tarena.util.GuiUtil;
public class WelcomeWindow
extends JWindow {
public WelcomeWindow() {
init();
}
private void init(){
setSize(430,300);
JPanel p = new JPanel(new BorderLayout());
JLabel l = new JLabel();
p.add(BorderLayout.CENTER, l);
setContentPane(p);
p.setBorder(new LineBorder(Color.gray));
ImageIcon ico = new ImageIcon(
getClass().getResource("welcome.png"));
l.setIcon(ico);
}
@Override
public void setVisible(boolean b) {
GuiUtil.centerWindow(this);
super.setVisible(b);
}
}