62,635
社区成员




while(true)
{
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("now:"
+ new SimpleDateFormat("yyyy-MM-dd hh:mm:ss")
.format(new Date()));
}
auto=new Button("显示上面的时间");
auto.addActionListener(this);
add(auto,BorderLayout.NORTH);
public class Clock {
JFrame jframe = new JFrame("clock");
JButton jbutton = new JButton();
public Clock(){
jframe.setLocation(500, 200);
jframe.setSize(300, 100);
jframe.add(jbutton);
Timer timer = new Timer(1000,new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
String time = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date());
jbutton.setText("Now:"+time);
}
});
timer.start();
jframe.setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Clock();
}
}
public class BalanceTimer{
String url = "jdbc:odbc:NetBarDataSource";
float NowBalance = 0;
float Price = 0;
int warning = 0;//发出余额不足警告的次数
public final static int ONE_MINUTE = 60000;
public BalanceTimer(final JFrame jframe,final String RecordID,final String ClientID,
final String ClientName,final String ComputerID,
final String BeginTime,final String BeginBalance){
//为计时器添加监听器,每分钟执行一次
Timer timer = new Timer(ONE_MINUTE, new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
try {
//每分钟更新一次Balance
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// 创建指定数据库的URL,StuInfo是建立的ODBC数据源
Connection con1 = DriverManager.getConnection(url, "sa",
"");
// 建立连接,url是前面的数据源地址,sa是用户名,密码为空
PreparedStatement stmt1 = con1.prepareStatement("select Balance,Price " +
"from Client,Rank where ID =? and Client.Rank = Rank.Rank");
stmt1.setString(1, ClientID);
ResultSet rs1 = stmt1.executeQuery();
if(rs1.next()){
NowBalance = Float.parseFloat(rs1.getString("Balance"));
Price = Float.parseFloat(rs1.getString("Price"));
}//获取上机前的Balance
Connection con2 = DriverManager.getConnection(url, "sa","");
NowBalance = NowBalance - Price;//每分钟扣掉price的金额
//用四舍五入处理Balance
BigDecimal bd = new BigDecimal(NowBalance);
NowBalance = (float) bd.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
PreparedStatement stmt2 = con2.prepareStatement("update Client " +
"set Balance = ? where ID = ?");//更新数据库中的Balance
stmt2.setFloat(1, NowBalance);
stmt2.setString(2, ClientID);
stmt2.executeUpdate();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
//每分钟判断一次Balance是否充足,如果不足会给出警告,但是只警告一次
if((NowBalance <= 0.5)&&(warning == 0)){
if(jframe != null && jframe.getState() == Frame.ICONIFIED)
{//如果消息框依附的框架不为空,并且该框架状态为图标化(最小化)
jframe.setState(Frame.NORMAL);
//设消息框依附的框架为正常状态(否则消息出现的地方是左上角,不好看)
}
JOptionPane.showMessageDialog(jframe, "余额不足,请速到到前台充值,以免影响使用",
"系统提示", JOptionPane.WARNING_MESSAGE);
warning = 1;//警告过一次后,将警告次数置为1,则不再发出警告
}
if(NowBalance <= 0){//余额为零时,强制退出系统,不需给出整个上机信息
SimpleDateFormat HMFromat = new SimpleDateFormat("yyyy/MM/dd/HH:mm");//时间输出的格式
String EndTime = new String(HMFromat.format(new Date()));
// //在强制下机的时候,获取当前时间为EndTime即可
// //计算Fee
float Fee = NowBalance - Float.parseFloat(BeginBalance);
BigDecimal bd = new BigDecimal(NowBalance);
Fee = (float) bd.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
//更新日营业额
new UpdateDayTurnover(Fee);
//更新月营业额
new UpdateMonthTurnover(Fee);
//生成用户使用计算机记录
new Record(RecordID,ClientID,ClientName,ComputerID,BeginTime,EndTime,Fee);
//改变用户登录状态和计算机使用状态为false
new ChangeState(false,ClientID,ComputerID);
System.exit(0);//退出系统
}
}
});
timer.start();//计时器监听器便写完后,立刻启动计时器
}
}