T-ELTS Project Day04
2012/2/13
====================
美文分享:
不开口, 没有人知道你想什么
不去做, 任何想法只在脑海里游泳
不迈出脚步, 永远找不到你前进的方向
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");
EntityCon
达内corejava+Oracle+JDBC+XML笔记
5星 · 超过95%的资源 需积分: 4 45 浏览量
2012-05-09
16:05:13
上传
评论
收藏 81KB ZIP 举报
molinghai
- 粉丝: 2
- 资源: 53
最新资源
- ocr模型ch-ppocr-mobile-v2.0-cls-infer
- 计算两个地点之间的最短路线距离.php
- JavaWeb-学习笔记
- 一键安装docker环境:docker、docker-compose
- 使Unity开发的游戏支持热更新的解决方案
- 信息办公简易java开源订销管理系统-javainfo.zip
- 新闻文章NTsky新闻发布(提供JavaBean)v1.0-ntskynewsv1.0javabean.zip
- 搜索链接java(结合lucene)版的公交搜索系统-javaso.zip
- 毕业设计-基于Hadoop+Spark的大数据金融信贷风险控系统源码.zip
- 其他类别MeyboMail Web(Java)开源简化-meybomailweb.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈