day9 主线游戏连接数据库,时间计算
package game;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.SimpleDateFormat;
public class pushbox extends JFrame {
int op;
gamepanel gp ;
long startTime = System.currentTimeMillis();//获取当前时间
pushbox(int op){
this.op=op;
gp = new gamepanel(op,this);
this.add(gp);
this.setSize(800, 800);
this.setTitle("推箱子(按ESC退出,按R返回上一步)");
this.setVisible(true);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setBackground(Color.white);
this.addKeyListener(gp);
}
@Override
protected void processWindowEvent(WindowEvent e) {
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
int time=(int) ((System.currentTimeMillis()-startTime)/1000);
int tot=gp.getTot();
//在这之后可以连接数据库
InputId id=new InputId(tot,time);//输入游戏玩家的信息
super.processWindowEvent(e);
}
}
}
号外
注意在创建数据库时将数据时间设置为 int型, 因为System.currentTimeMillis()与startTime 必须是都是 int型 才能除去毫秒,如果是 date 或者datetime 则基本会乱码,因为这两种运算都不能做加减乘除运算。