T-ELTS Project Day04
====================
美文分享:
不开口, 没有人知道你想什么
不去做, 任何想法只在脑海里游泳
不迈出脚步, 永远找不到你前进的方向
1 窗口的居中处理
private void center(Window win){
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension screen = toolkit.getScreenSize();
int x = (screen.width - win.getWidth()) / 2;
int y = (screen.height - win.getHeight())/2;
win.setLocation(x, y);
}
public void show(){
center(welcomeWindow);
welcomeWindow.setVisible(true);
final Timer timer = new Timer();
timer.schedule(new TimerTask(){
public void run() {
welcomeWindow.setVisible(false);
center(loginFrame);
loginFrame.setVisible(true);
timer.cancel();
}
}, 2000);
}
2 考试时间提醒与超时交卷
1) 更新控制器 start方法添加: startTimer();
2) 更新控制器 添加
private Timer timer;
private void startTimer(){
timer = new Timer();
int timeout = examInfo.getTimeLimit()*60*1000;
final long end = System.currentTimeMillis() + timeout;
timer.schedule(new TimerTask(){
public void run() {
//show 是需要显示的剩余时间
long show = end - System.currentTimeMillis();
examFrame.updateTime(show);
}
}, 0, 1000);
timer.schedule(new TimerTask(){
public void run() {
gameOver();//提前交卷!
}
}, timeout);
}
public void gameOver() {
try {
timer.cancel();
int idx = currentQuestion.getQuestionIndex();
List<Integer> userAnswers =
examFrame.getUserAnswers();
examService.saveUserAnswers(idx, userAnswers);
int score = examService.examOver();
JOptionPane.showMessageDialog(
examFrame, "你的分数:"+score);
examFrame.setVisible(false);
menuFrame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(
examFrame, e.getMessage());
}
}
public void send() {
int val = JOptionPane.showConfirmDialog(examFrame, "交吗?");
if (val != JOptionPane.YES_OPTION) {
return;
}
gameOver();
}
3) 更新examFrame 添加显示时间方法
private JLabel timer;
public void updateTime(long show) {
long h = show/1000/60/60;
long m = show/1000/60%60;
long s = show/1000%60;
if(m<=5 && h==0){
timer.setForeground(Color.red);
}
timer.setText(h+":"+m+":"+s);
}
3 设置登录界面的默认按钮
更新LoginFrame方法: createBtnPane() 添加:
getRootPane().setDefaultButton(login);
4 更新考试界面按钮状态:
1)添加
private JButton next;
private JButton prev;
private void updateButton(int count , int index){
prev.setEnabled(index!=0);
next.setEnabled(index!=count-1);
}
2) 修改方法updateView() 增加:
updateButton(examInfo.getQuestionCount(),
questionInfo.getQuestionIndex());
3) 将next, prev 引用引用到界面按钮控件对象.
5 方法调用实现, Utils.java类
1) 反射方法调用
public static Response call(
Object obj, Request request){
Response res = new Response();
Class cls = obj.getClass();
try {
Method m = cls.getDeclaredMethod(
request.getMethod(), request.getTypes());
Object val = m.invoke(obj, request.getParams());
res.setValue(val);
return res;
} catch (InvocationTargetException e) {
e.printStackTrace();
//方法执行异常
Exception ex = (Exception) e.getTargetException();
res.setException(ex);
return res;
}catch(Exception e){
e.printStackTrace();
res.setException(e);
return res;
}
}
方法调用测试:
public class MethodCallTest {
public static void main(String[] args) {
Request request =
new Request("charAt",
new Class[]{int.class},
new Object[]{2});
Response res = Utils.call("ABCDE", request);
System.out.println(res);//C
ExamServiceImpl service = new ExamServiceImpl();
Config config = new Config("client.properties");
EntityContext entityContext = new EntityContext(config);
service.setEntityContext(entityContext);
//可以任意调用 service 对象的任何方法
request = new Request("login",
new Class[]{int.class, String.class},
new Object[]{1001, "1234"});
Response r = Utils.call(service, request);
System.out.println(r);
}
}
2) 请求与反馈对象序列化 与 远程调用
将Request和Response 对象利用网络流传输, 就可以实现网络远程
方法调用
6 无持续状态连接协议实现
1) 无连接协议原理: 建立连接-发送请求-接收响应-断开连接
优点: 充分重用服务器的网络服务能力,
缺点: 不能保持持久连接状态, 不能记住是否已经连接
客户端: 建立连接-发送请求-接收响应-断开连接
com.tarena.test.ClientDemo.java
服务器: 建立连接-接收请求-处理-发送响应-断开连接
com.tarena.elts.net.ExamServer.java
2) 无连接协议的状态保持:
状态保持目的: 识别是否已经来过
采用令牌机制实现: 每次发送请求都带着令牌发送, 服务器检查
令牌是否可用, 如果不可用创建新的令牌, 服务器每次发送响应
都包含令牌信息. 服务器通过比较令牌是否在以登记的集合中,来
确定是那个客户
3) 网络服务端实现:
/** 考试应用服务器 */
public class ExamServer {
private Config config;
/** 服务列表, 每个SID对应一个 ExamService 实例,
* 每个客户对应一个SID */
Map<String, ExamService> serviceMap =
new HashMap<String, ExamService>();
private EntityContext entityContext;
public void setEntityContext(EntityContext entityContext) {
this.entityContext = entityContext;
}
public ExamServer() {
}
public void setConfig(Config config) {
this.config = config;
}
public void start(){
int port = config.getInt("ServerPort");
try {
ServerSocket ss = new ServerSocket(port);
while(true){
//等待客户端的连接
Socket s = ss.accept();
new Service(s).start();
}
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
class Service extends Thread{
Socket s;
public Service(Socket s) {
this.s = s;
}
@Override
public void run() {
try {
// 接收请求
ObjectInputStream in =
new ObjectInputStream(s.getInputStream());
Request req = (Request)in.readObject();
//处理-
//String obj = "ABCD";
//根据用户请求req, 找到对应SessionID 的考试服务对象
//一个考试服务对象对应于一个考试客户端(一个考生)
//如果请求req 中 没有SessionID getService()方法会
//创建新的SessionID和ExamService 实力
ExamService service = getService(req);
Response res = Utils.call(service, req);
res.setSessionID(req.getSessionID());
//发送响应
ObjectOutputStream out =
new ObjectOutputStream(s.getOutputStream());
out.writeObject(res);
//断开连接
s.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
private ExamService getService(Request req) {
String sid = req.getSessionID();
if(sid==null){//如果没有SID 就创建新的
sid = UUID.randomUUID().toString();
req.setSessionID(sid);
}
ExamService service = serviceMap.get(sid);
if(service==null){//第一次访问服务, 需要创建新的service
ExamServiceImpl serviceImpl =
new ExamServiceImpl();
serviceImpl.setEntityContext(entityContext);
serviceMap.put(sid, serviceImpl);
service = serviceImpl;
}
return service;
}
/** 启动服务器 */
public static void main(String[] args) {
ExamServer server = new ExamServer();
Config config = new Config("server.properties");
EntityContext entity
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
在线考试系统完整代码,JAVA源码 (122个子文件)
ExamFrame.class 7KB
ClientContext.class 7KB
EntityContext.class 6KB
LoginFrame.class 5KB
ExamServiceImpl.class 4KB
MenuFrame.class 4KB
ExamServiceAgent.class 4KB
Question.class 4KB
ExamServer.class 3KB
Utils.class 3KB
QuestionInfo.class 2KB
ExamInfo.class 2KB
ExamServer$Service.class 2KB
Request.class 2KB
User.class 2KB
Response.class 2KB
ClientMain.class 2KB
Main.class 2KB
LoginTest$1.class 2KB
JFrameDemo.class 2KB
WelcomeWindow.class 2KB
PropertiesDemo.class 1KB
Config.class 1KB
LayoutDemo.class 1KB
LoginTest.class 1KB
NullLayoutDemo.class 1KB
ClientContext$1.class 1KB
ClientContext$2.class 1014B
LoginFrame$3.class 927B
MenuFrame$2.class 918B
LoginFrame$5.class 918B
LoginFrame$2.class 898B
MenuFrame$4.class 890B
MenuFrame$3.class 889B
ExamFrame$4.class 888B
ExamFrame$3.class 888B
ExamFrame$2.class 888B
LoginFrame$4.class 873B
LoginFrame$6.class 865B
LoginFrame$1.class 864B
MenuFrame$1.class 857B
ExamFrame$1.class 827B
ConfigTest.class 776B
ClientContext$3.class 763B
IdOrPwdException.class 756B
ExamFrame$Option.class 682B
NullDemo.class 617B
LoginFrameTest.class 613B
MenuFrameTest.class 612B
ExamFrameTest.class 612B
ExamService.class 537B
.classpath 232B
Thumbs.db 15KB
ExamFrame.java 7KB
ClientContext.java 7KB
LoginFrame.java 6KB
EntityContext.java 6KB
MenuFrame.java 4KB
ExamServiceImpl.java 4KB
ExamServiceAgent.java 3KB
ExamServer.java 3KB
Question.java 3KB
LoginTest.java 2KB
Utils.java 2KB
QuestionInfo.java 2KB
ExamInfo.java 1KB
User.java 1KB
ClientMain.java 1KB
Request.java 1KB
Main.java 1KB
LayoutDemo.java 1KB
JFrameDemo.java 1KB
Response.java 1KB
WelcomeWindow.java 923B
PropertiesDemo.java 905B
ExamService.java 900B
Config.java 718B
NullLayoutDemo.java 678B
IdOrPwdException.java 388B
LoginFrameTest.java 350B
NullDemo.java 330B
ExamFrameTest.java 322B
MenuFrameTest.java 322B
ConfigTest.java 279B
arch.PNG 231KB
title.png 44KB
title.png 44KB
menuFrame.png 42KB
login_layout.PNG 31KB
welcome.png 29KB
welcome.png 29KB
tarena.png 28KB
tarena.png 28KB
exmaFrame.png 24KB
整体结构.png 23KB
network.png 22KB
runtime_objects.png 21KB
exam_title.png 20KB
exam_title.png 20KB
loginFrame.png 8KB
共 122 条
- 1
- 2
wjbrobin
- 粉丝: 3
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页