java时间程序_Java时间记录程序(闲来无事做的)

packageorg.dreamtech;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileWriter;importjava.io.Writer;importjava.util.Timer;importjava.util.TimerTask;importjavax.swing.JButton;importjavax.swing.JFrame;importjavax.swing.JTextField;/*** 学习时间记录软件

*@authorXu YiQing

**/

public classMain {private staticJFrame f;private staticJButton startButton;private staticJButton endButton;private staticJTextField timeText;private static final String SAVE_PATH = "data/SAVE";private static intresult;private staticTimerTask task;public static voidmain(String[] args) {

f= new JFrame("学习");

f.setSize(400, 300);

f.setLocation(200, 200);

f.setLayout(null);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setVisible(true);

startButton= new JButton("开始");

startButton.setBounds(50, 100, 280, 30);

f.add(startButton);

startButton.addActionListener(newActionListener() {

@Overridepublic voidactionPerformed(ActionEvent e) {

startButtonClick();

}

});

endButton= new JButton("暂停");

endButton.setBounds(50, 150, 280, 30);

f.add(endButton);

endButton.addActionListener(newActionListener() {

@Overridepublic voidactionPerformed(ActionEvent e) {

endButtonClick();

}

});

timeText= newJTextField();

timeText.setEditable(false);

timeText.setHorizontalAlignment(JTextField.CENTER);

timeText.setBounds(50, 50, 280, 30);

f.add(timeText);

}private static voidsetText(String text) {

timeText.setText(text);

}protected static voidendButtonClick() {

end(result+"");

}protected static voidstartButtonClick() {

start();

}private static voidstart() {

String data= "";try{

File file= newFile(SAVE_PATH);if (!file.exists()) {

file.createNewFile();

}

FileInputStream fis= newFileInputStream(SAVE_PATH);byte[] b = new byte[1024];int len = 0;while ((len = fis.read(b)) != -1) {

data= new String(b, 0, len);

}

fis.close();

}catch(Exception e) {

e.printStackTrace();

}if (data == null || data.equals("")) {

result= 0;

}else{

result=Integer.parseInt(data);

}

task= newTimerTask() {

@Overridepublic voidrun() {

result+= 1;

setText(result+"");

}

};

Timer timer= newTimer();long delay = 0;long intevalPeriod = 1 * 1000;

timer.scheduleAtFixedRate(task, delay, intevalPeriod);

}private static voidend(String result) {try{if (task != null) {

task.cancel();

}

File file= newFile(SAVE_PATH);if (!file.exists()) {

file.createNewFile();

}else{

file.delete();

file.createNewFile();

}

Writer out= newFileWriter(file);

String data=result;

out.write(data);

out.close();

}catch(Exception e) {

e.printStackTrace();

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值